realpikolo
realpikolo

Reputation: 13

I need list of files in long format that contain string

I want to have the list of files, in long format (ls -l) including date and time, that contain an specific string and if possible, number of occurrences.

The most I have achieved is a list of files (just the name) with number of occurrences:

grep -c 'string' * | grep -v :0

That shows something like: filename:number of occurrences

But I cannot improve it to show also file date and time. It has to be something simple, but I am a bit newbie.

Upvotes: 1

Views: 36

Answers (1)

Valdrinium
Valdrinium

Reputation: 1416

I have used -s for ignoring the folder warnings. ':0$' is the regex for ending in :0. awk then calls ls -l on just the filenames found, then | tr '\n' ' ' replaces the newlines of the ls command with spaces. We output the number of occurences at the end of each line so we don't lose the info while going forward. The last awk is to just to print the columns needed.

grep -c 'form-group' * -s | grep -v ':0$' | awk -F ':' '{ printf system( "ls -l \"" $2 "\" | tr \"\n\" \" \"" ); print " " $3 }' | awk -F ' ' '{ print $6 " " $7 " " $8 " " $9 " : " $11 }'

Here is some sample output:

Sep 1 13:47 xxx.blade.php : 12
Sep 1 13:47 xxx.blade.php : 5
Sep 1 13:47 xxx.blade.php : 6
Sep 11 17:25 xxx.blade.php : 4
Sep 4 15:03 xxx.blade.php : 6

Upvotes: 1

Related Questions