Skip to content

Instantly share code, notes, and snippets.

@manero6
Last active October 19, 2023 09:17
Show Gist options
  • Save manero6/87436287a64f32032f0b8d66aad35a23 to your computer and use it in GitHub Desktop.
Save manero6/87436287a64f32032f0b8d66aad35a23 to your computer and use it in GitHub Desktop.
simple script based on xclip to filter out stuff from the clipboard
#!/bin/bash
# put current clipboard in a variable
XCLIP_CLIPBOARD="$(xclip -o -selection clipboard)"
XCLIP_SELECTION=$XCLIP_CLIPBOARD
if [ $# -ge 1 ]
then
# print the initial string aka the current clipboard
echo "=> 1st string: $XCLIP_CLIPBOARD"
echo
# Double quotes on "$@" are needed, else strings like 'a ' will be filtered as 'a'
for FILTER in "$@"
do
echo "=> filter out: '$FILTER'"
echo "=> old string: '$XCLIP_SELECTION'"
# filtering stuff out the bash way
XCLIP_SELECTION="${XCLIP_SELECTION/$FILTER/}"
echo "=> new string: '$XCLIP_SELECTION'"
echo
done
# copy the result to selection
xclip <<< "$XCLIP_SELECTION"
echo "=> clipboard: '$XCLIP_CLIPBOARD'"
echo "=> selection: '$XCLIP_SELECTION'"
else
echo "=> Please provide at least one string to filter out!"
fi
@manero6
Copy link
Author

manero6 commented Jul 4, 2023

Initial string:

xclip -o -selection clipboard
https://gist.github.com/manero6/87436287a64f32032f0b8d66aad35a23

Filtering out strings:

clip-cleaner.sh https:// manero6/ gist. github.com/
=> 1st string: https://gist.github.com/manero6/87436287a64f32032f0b8d66aad35a23

=> filter out: https://
=> old string: https://gist.github.com/manero6/87436287a64f32032f0b8d66aad35a23
=> new string: gist.github.com/manero6/87436287a64f32032f0b8d66aad35a23

=> filter out: manero6/
=> old string: gist.github.com/manero6/87436287a64f32032f0b8d66aad35a23
=> new string: gist.github.com/87436287a64f32032f0b8d66aad35a23

=> filter out: gist.
=> old string: gist.github.com/87436287a64f32032f0b8d66aad35a23
=> new string: github.com/87436287a64f32032f0b8d66aad35a23

=> filter out: github.com/
=> old string: github.com/87436287a64f32032f0b8d66aad35a23
=> new string: 87436287a64f32032f0b8d66aad35a23

After that printing xclip clipboard (which should remain unchanged) and selection (which should contain the final filtered string)

xclip -o selection clipboard
https://gist.github.com/manero6/87436287a64f32032f0b8d66aad35a23
xclip
87436287a64f32032f0b8d66aad35a23

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment