-2

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.

Tim
  • 101,790
  • 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 01:31
  • gnu sed takes the -s flag to interepret multiple file args as separate streams – mikeserv Aug 31 '14 at 02:29
  • Did you try running them with multiple filename arguments? – Barmar Sep 01 '14 at 08: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 03 '14 at 08:26

2 Answers2

2

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:<br />

<pre><code>awk '/^#include/ {print $2}' *.c *.h</code></pre></blockquote>

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.

jasonwryan
  • 73,126
0

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 
end
JigarGandhi
  • 4,960