tags:

views:

95

answers:

3

I want to grep multiple files in a directory and collect the output of each grep in a separate file ..So if I grep 20 files, I should get 20 output-files which contain the searched item. Can anybody help me with this? Thanks.

+6  A: 

Use a for statement:

for a in *.txt; do grep target $a >$a.out; done
Greg Hewgill
thanks lot for the quick reply :-)
Sharat Chandra
+1  A: 

just one gawk command

gawk '/target/ {print $0 > FILENAME".out"}' *.txt
ghostdog74
A: 

you can use just the shell, no need external commands

for file in *.txt
do
    while read -r line
    do
        case "$line" in 
            *pattern*) echo $line >> "${file%.txt}.out";;
        esac
    done  < "$file"
done