enthuguy
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
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
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
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
ASKER
Thanks very much. Will try this :)
ASKER
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
Is it possible to specify time instead of last 1 day pls
eg. Consider files which were created after 6am
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks again
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