1

How can I pipe files from grep to open all matching files?

I've tried grep -li "type" * | open, but that does not work.

Oliver Salzburg
  • 89,072
  • 65
  • 269
  • 311
violet
  • 125

3 Answers3

2
IFS=$'\n'; for i in $(grep -rli "type" *); do open "$i"; done

The IFS is necessary, so that it won't split filenames containing whitespaces.

elsamuko
  • 291
1

Use while read line loop:

| while read line

If

$ grep -l something *.py
file1.py
file2.py

Then

$ grep -l something *.py | while read line ; do open "$line" ; done

Is equivalent to

$ open "file1.py"
$ open "file2.py"

Using quotes like "$line" instead of just $line allows for it to match filenames that contain spaces, otherwise filenames with spaces will cause errors.
Notice that line is just a commonly-used name for a variable in this usage. It could just as easily be

$ grep -l something *.py | while read potato ; do open "$potato" ; done
violet
  • 125
0

There is many ways to do this (using find/exec, xargs,...), one could be :

grep -li "type" * | while read file; do echo "processing $file"; done
mpromonet
  • 242
  • 2
  • 13