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
  • 86,445
  • 63
  • 260
  • 306
violet
  • 125
  • 1
  • 1
  • 9
  • Not quite the exact same what you're asking, but [a related question/answer](http://superuser.com/questions/189362/how-to-pipe-command-output-to-other-commands/189386#189386) – Rich Homolka Jun 03 '14 at 20:37
  • What do you mean by "open"? Do you want to output their contents to the screen? Open them all in `emacs` in sequence? Open them all in `emacs` at the same time? – dg99 Jun 03 '14 at 22:46

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
  • 271
  • 1
  • 8
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
  • 1
  • 1
  • 9
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