Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 11 down vote accepted
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.

share|improve this answer
    
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 at 4:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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