find string from multiple dir and redirect to new files

Hi,

I am new to script and I want find one string from multiple files in diff directories and put that out put to new file.

Like I have A,B & C directories and each has multiple files but one file is unic in all the directories like COMM.txt
Now I want write script to find the string "Jack" and what ever get the output lines need to redirect into my home directory

Hope i get help here....

Do you want any specific action with unique file?
by the way,

find /path/to/parent/dir/of/A,B,C/ -type f  | xargs grep 'Jack' > $HOME/result.dat

Yes, there is one more req with that. in sub directories, each dir has one file name with Jack.txt and other files are diff names. but i want to go to each sub dir's and search the string in only that file and when it get result it should create same file name in my home dir for each file search and put the result init.

Thanks for your help

So you want to find only in 'Jack.txt' ?

find /path/to/parent/dir/of/A,B,C/ -type f -name "Jack.txt"  | xargs grep 'Jack' > $HOME/result.dat

sorry for keep asking....
actually the file name in each dir is jack<yr>.txt and in each sub dir the file has with yr ext.
When it send the result to result.dat, all going into that file, but each file result should go to with same file name in home dir instead into single file

Thanks

I am still confused.
your files are "jack<year>.txt" or with year extension eg jack.<year> ?
you wrote both the things.
Better give an example with the sample file names.

as per my understanding:

for file in $(find /path/to/parent/dir/of/A,B,C/ -type f -name "*.txt")
do
 grep 'Jack' $file > $HOME/$(basename $file).result
done

you can change the result file name whatever you want.

Great.. its works perfect.
Thanks so much for you help on this

Hi there,

Hope some one can help me for my small req.
I have big log file and it has many error messages.
I want to write script to search for string "Error" and where ever it find it should print full line of it.
exp: cat file.txt

This is test file
Error: user not found
connecting database
Error: Server not found

output should be:
Error: user not found
Error: Server not found
Thanks

View Original: http://www.unix.com/private.php?do=showpm&pmid=26860\#ixzz0lxtXHRWg

The link you have posted is not valid.

regarding your requirement, you could simply do a grep.

grep '^Error' file.txt