-1

I need to grep for string named "Account_Id" in my logs folder. However, there are 10000 log files there and i need to search this only in files which were created in last 30 days.

I need output like this in a csv file. This is just an example

File_name       Matched_string           Line
program1.log    where account_id1         14
program2.log    substr(account_id,1,4)    45

I did try using grep -nHi "Account_Id" * However it searches entire directory and takes a lot of time

Any help is really really appreciated

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • 3
    Take a look at the `find ` command –  Apr 25 '15 at 02:36
  • possible duplicate of [grep files based on time stamp](http://stackoverflow.com/questions/8851667/grep-files-based-on-time-stamp) – Joe Apr 25 '15 at 13:31

2 Answers2

2
DIRECTORY=logs
echo -e "File_name\tMatched_string\tLine"
for i in $(find $DIRECTORY -ctime -30)
do 
    grep -nHIs "Account_Id" $i | awk -F ":" '{print $1"\011" $3 "\011" $2}'
done

I'm not sure you'll get the output like you specified in your question because of tab align issues

UPDATE

Oneliner which starts in current dir. It uses find -exec and awk printf. Probably you need to adjust printf format ("%-40s\t%-40s\t%-4s\n") :

find . -ctime -30 -exec grep -nHIs "Account_Id" {} \; | awk -F ":" 'BEGIN { printf "%-40s\t%-40s\t%-4s\n" , "File_name", "Matched_string", "Line" } { printf "%-40s\t%-40s\t%-4s\n", $1, $3, $2}' > out.csv
Andrey Sabitov
  • 454
  • 2
  • 5
0

grep -Hin Account_Id find * -mtime -30 |awk -F":" '{print $1 "\t" $3"\t" $2}' >> xyz.csv

If you have the file names in the specific pattern then you can add that pattern in find , like following

grep -Hin Account_Id find program* -mtime -30 |awk -F":" '{print $1 "\t" $3"\t" $2}' >> xyz.csv

Note: I don’t know why tilt symbol is disappearing from my comments Make sure you put tilt(the keyboard button bellow to Esc) before word find and after the -30 in the above command.