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.
You could do something like:
while xclip -o -sel c && echo; do
xclip -i -quiet -sel c <> /dev/null >&0 2>&0
done > file
xclip -o -sel c
dumps the content of the CLIPBOARD
selection. xclip -i -quiet -sel c
claims ownership of the CLIPBOARD
selection (and makes it empty) until something else claims it again. If you have a clipboard manager running, you may want to disable it as it would probably interfere.
Best Answer
xsel
’s default behaviour depends on whether its input or output is connected to a terminal, so redirecting to and from files typically does the right thing. As mentioned by codeforester, the solution in your case is to runYou can make your intent explicit by adding
-o
(when outputting the contents of the clipboard) or-i
(when inputting to the clipboard). Without these options, ifxsel
’s context is indeterminate (i.e. neither standard input or standard output are connected to a terminal), it behaves in-o
mode:xsel -b < /dev/null > file
works as you’d expect, butxsel -b < file > /dev/null
doesn’t.You can also use
xclip
to copy the clipboard’s contents to a file:xclip
can additionally request specific versions of the clipboard’s contents (known as targets), depending on the selection’s owner; for example, if you copied text from a web browser, you could retrieve it as HTML usingThe special
TARGETS
target will list the available targets: