Skip to main content
added 280 characters in body
Source Link
user001
  • 3.8k
  • 6
  • 43
  • 55

You could try the following:

$ find . -type f -name "*.txt" -exec sh -c "grep -l "Reptile" {} | xargs -I% grep -Hn snake %" \;
./rep1.txt:2:snake
./rep2.txt:5:another snake

Output contains colon-delimited lists in which the first argument is the file name (from the -H argument to grep), the second argument is the line number on which the desired term appears (from the -n argument to grep), and the third argument is the line itself.

The xargs can be moved outside the find, giving you:

$ find . -type f -name "*.txt" -exec grep -l "Reptile" {} \; | xargs -i grep -Hn snake {}

Note that the -i argument to xargs (which is equivalent to -I{} is deprecated but I use it often for convenience.

Input files:

$ tail -n+1 rep*.txt
==> rep1.txt <==
Reptile
snake
iguana
crocodile

==> rep2.txt <==
Reptile
alligator
turtle
another snake
komodo dragon

==> rep3.txt <==
Reptile
lizard
gecko

If you need to deal with malformed file names, you can think about incorporating print0 and the -0 option to xargs.

You could try the following:

$ find . -type f -name "*.txt" -exec sh -c "grep -l "Reptile" {} | xargs -I% grep -Hn snake %" \;
./rep1.txt:2:snake
./rep2.txt:5:another snake

Output contains colon-delimited lists in which the first argument is the file name, the second argument is the line number on which the desired term appears, and the third argument is the line itself.

Input files:

$ tail -n+1 rep*.txt
==> rep1.txt <==
Reptile
snake
iguana
crocodile

==> rep2.txt <==
Reptile
alligator
turtle
another snake
komodo dragon

==> rep3.txt <==
Reptile
lizard
gecko

You could try the following:

$ find . -type f -name "*.txt" -exec sh -c "grep -l "Reptile" {} | xargs -I% grep -Hn snake %" \;
./rep1.txt:2:snake
./rep2.txt:5:another snake

Output contains colon-delimited lists in which the first argument is the file name (from the -H argument to grep), the second argument is the line number on which the desired term appears (from the -n argument to grep), and the third argument is the line itself.

The xargs can be moved outside the find, giving you:

$ find . -type f -name "*.txt" -exec grep -l "Reptile" {} \; | xargs -i grep -Hn snake {}

Note that the -i argument to xargs (which is equivalent to -I{} is deprecated but I use it often for convenience.

Input files:

$ tail -n+1 rep*.txt
==> rep1.txt <==
Reptile
snake
iguana
crocodile

==> rep2.txt <==
Reptile
alligator
turtle
another snake
komodo dragon

==> rep3.txt <==
Reptile
lizard
gecko

If you need to deal with malformed file names, you can think about incorporating print0 and the -0 option to xargs.

Source Link
user001
  • 3.8k
  • 6
  • 43
  • 55

You could try the following:

$ find . -type f -name "*.txt" -exec sh -c "grep -l "Reptile" {} | xargs -I% grep -Hn snake %" \;
./rep1.txt:2:snake
./rep2.txt:5:another snake

Output contains colon-delimited lists in which the first argument is the file name, the second argument is the line number on which the desired term appears, and the third argument is the line itself.

Input files:

$ tail -n+1 rep*.txt
==> rep1.txt <==
Reptile
snake
iguana
crocodile

==> rep2.txt <==
Reptile
alligator
turtle
another snake
komodo dragon

==> rep3.txt <==
Reptile
lizard
gecko