0

I am trying to figure out which files I need to modify; so by that, I use sequences of grep commands. I want to find out which files contain both foo and bar. Therefore, my command is:
grep foo `grep bar * -l` | awk -F':' '{print $1}' | sort | uniq
This command gets me a big list that looks like this:
pageABC.txt
pageBCD.txt
pageDEF.txt

I want this output to be opened in emacs. So what I'd normally do is:
emacs ` whatever_was_in_my_output `
This command normally opens all the files.
If I try
emacs `grep foo `grep bar * -l` | awk -F':' '{print $1}' | sort | uniq `
Emacs won't even start. Maybe it's because of the double grave accents used. Any ideas how to solve this?

Many Thanks,

D

1
  • Rather than piping through awk, you could also pass -l to the outer grep. Commented Oct 6, 2011 at 9:06

2 Answers 2

5

You forgot to escape the inner command substitution:

emacs `grep foo \`grep bar * -l\` | awk -F':' '{print $1}' | sort | uniq`

In cases like this, I usually prefer the alternative command substition syntax, since it nests more easily:

emacs $(grep foo $(grep bar * -l) | awk -F':' '{print $1}' | sort | uniq)
Sign up to request clarification or add additional context in comments.

1 Comment

Also note the traditional ARG_MAX warning partmaps.org/era/unix/award.html#arg-max. A more robust, scalable solution would be grep -l bar * | xargs grep -l foo | sort -u | xargs emacs ... with the possible comment that xargs emacs is overkill (you hardly want to open more than Emacs instance, even if you don't want to hit "bash: Argument list too long" errors; but then you have emacsclient for those kinds of situations).
3

Avoid backticks in bash, and use $(command) to run sub-commands. They nest properly, unlike backticks.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.