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.

2
  • Not quite the exact same what you're asking, but a related question/answer Jun 3, 2014 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 3, 2014 at 22:46

3 Answers 3

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
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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .