Link to home
Start Free TrialLog in
Avatar of enthuguy
enthuguyFlag for Australia

asked on

Search string in a file recursively and extract line to another file in bash

HI Friends,
Would like to achieve below in bash, pls help

I have to search for files “ip.txt” recursively which are created today under certain parent directory
Search for a string “WARNING” and extract the line to another file with date stamp yyyy-mm-yy_all.txt. If we find 50 lines with WARNING string. Then yyyy-mm-yy_all.txt should have 50.
Then the pickup the next file from different subdirectory and perform same search but append the lines to the same yyyy-mm-yy_all.txt. if we find 50 again then yyyy-mm-yy_all.txt should have 100 now.

Thanks in advance
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

extractfile=$(date "+%Y-%m-%d_all.txt") #  I assume that you want the file to be named "yyyy-mm-dd_all.txt" instead of "yyyy-mm-yy_all.txt"!
cd /certain/parent/directory
find . -type f -name "ip.txt" -mtime -1 -print  | while read fn
   do
      F=$(grep "WARNING" $fn) && echo $F >> $extractfile
   done
A shorter version:

extractfile=$(date "+%Y-%m-%d_all.txt")
cd /certain/parent/directory
find . -type f -name "ip.txt" -mtime -1 | xargs -r  grep -h "WARNING" >> $extractfile
Avatar of enthuguy

ASKER

Thanks very much.  Will try this  :)
Thanks very much...that worked
Is it possible to specify time instead of last 1 day pls

eg. Consider files which were created after 6am
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks again