bash - In Unix how to search string only in few files in directory -
i need grep string named "account_id" in logs folder. however, there 10000 log files there , need search in files created in last 30 days.
i need output in csv file. example
file_name matched_string line program1.log account_id1 14 program2.log substr(account_id,1,4) 45
i did try using grep -nhi "account_id" *
searches entire directory , takes lot of time
any really appreciated
directory=logs echo -e "file_name\tmatched_string\tline" in $(find $directory -ctime -30) grep -nhis "account_id" $i | awk -f ":" '{print $1"\011" $3 "\011" $2}' done
i'm not sure you'll output specified in question because of tab align issues
update
oneliner starts in current dir. uses find -exec
, awk printf
. need 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
Comments
Post a Comment