9

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.

12
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.

  • I use this one all the time. It should work well, just make sure your copy isn't "too large" or you could get unexpected output. This should be fine for anything your normally copy and paste though. – coteyr Jan 17 '14 at 4:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.