Thursday, August 27, 2009

Searching for a string inside multiple files and output the name of the files containing that string

find . -name "*" | grep -i ecomm | xargs ls -las | grep "Aug 27" | awk '{print $10}' | xargs grep -i string_to_search_for | grep -i "\.log" | awk '{print $1}'

where:

"ecomm" is part of the name of the file in which we should search for the string
"Aug 27" is the date the files were last modified
"$10" is the last column resulting from the "ls -las" command, this is the column containing the filename (the string is not searched for yet)
"string_to_search_for" the name is obvious :)
"\.log" all the files end with ".log"
"$1" is the filename containing the string

There are still two problems left (I don't have time right now to solve them):
  1. All the filenames end with the ":" character, it needs to be omitted
  2. Filenames are redundant, i.e. each file is repeated many times
Update: Problem number 2 solved using the "sort" and "uniq" commands which sort the results and then omit the repeated results, here is an example:

find . -name "*" | grep -i ecomm | xargs ls -las | grep "Aug 31" | awk '{print $10}' | xargs grep -i mbassiouny | grep -i "\.log" | awk '{print $1}' | sort | uniq

No comments: