grep can search in multiple files, including those files recursively under a specified directory. This makes grep able to find which file contains a pattern.

Can awk and sed also? It seems that they can only process one file? Thanks.

  • 3
    They certainly can. Check their man pages. Neither has a "recursive" option, but most likely your shell can handle that for you (bash: shopt -s globstar; sed '...' **/*.txt), else you can use find. – glenn jackman Aug 31 '14 at 1:31
  • gnu sed takes the -s flag to interepret multiple file args as separate streams – mikeserv Aug 31 '14 at 2:29
  • Did you try running them with multiple filename arguments? – Barmar Sep 1 '14 at 8:29
  • purpose of sed is to edit a stream (Stream EDitor) so it take a stream as input and modify it (depending requested action). You could feed several file as input in a stream but it change the stream not the file. GNU sed allow (via -i) to interact tothe file directly but is not the posix version – NeronLeVelu Sep 3 '14 at 8:26

From the awk FAQ:

16. How does awk deal with multiple files?
16.2 How can I get awk to read multiple files?
it's automatic (under Unix at least) -- use something like:
awk '/^#include/ {print $2}' *.c *.h

Awk will read from the files passed to it as arguments consecutively, ie., completing one before moving to the next. The name of the file being currently read is stored as FILENAME.

This can be particularly useful when you are wanting to compare the contents of more than one file on a line-by-line (record by record) basis: such as in this question.

See the manual on reading files for a full explanation.


if you want to do it in a simple way , try using it along with foreach loop

foreach var (`ls directory_path`)
sed -i '/../.../ ' $var
awk '{ #commands to be run }' $var 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.