Shell – Filter the clipboard content without using an intermediate file

clipboardshelltext processingx11

I'm trying to modify the text that is in my clipboard, remove certain lines of that contains some strings and then get the output for further manipulation (I prefer coping to my clipboard only if I need it). I was thinking something like this:

cat > swapfile
##Paste all
grep -v string swapfile
## Read all
rm swapfile

I try to prevent the file creation and doing everything in the shell stdin/out itself. I tried using pipes, redirections, grep/sed from the input, but none resulted in printing the input without the undesired pattern.

Best Answer

xsel -o -p  | grep -v string

The above uses the xsel utility to capture from the clipboard and send the current primary selection to stdout. You can then modify the output with grep (or sed or awk) as you please.

If you don't want the primary selection, replace "-p" with "-s" for the secondary selection, or "-c" for the clipboard selection.

On a debian or similar distribution, you can obtain xsel with apt-get install xsel.

The above sends its output to stdout. If you want to capture the output directly back into the selection, use:

xsel -o -p  | grep -v string | xsel -i -s

The option "-i" tells xsel to get input from stdin and save it to the selection. "-s" again specifies the secondary selection.

Related Question