Piping to grep with pbpaste

cat file

1 aaa
2 bbb
3 ccc
4 ddd

In TextEdit, I then copy the characters �ccc� to the clipboard. The problem is that the following command gives no output:

bash-3.2$ pbpaste | grep - file

Desired output:

3 ccc

What should the syntax be for that command? I am using MacOS El Capitan with Quartz 2.7.9.

Thanks!

Try:

grep -f <(pbpaste) file

or

grep "$(pbpaste)" file

--
Note: some greps can also do this:

pbpaste | grep -f - file

, but not MacOS' BSD grep.

1 Like

Not sure I understand, but if you want the paste board contents piped on stdin to be grep ped in file , try grep -f- file

This should work with simple strings like the one you showed, but be careful if there are characters with special meanings in regexes.

Note also that on macOS, the grep utility only works on complete lines of text. If the text you selected does not include a <newline> character at the end of the line you selected, pbpaste will only output a partial line of output without a line terminating <newline> character.

1 Like