Tuesday, October 3, 2017

linux - String Search: find and grep and?


Task:
1) find a string 'decor' inside files in a directory
2) get # of occurrence for decor for each file that has it


The first part of the problem is partially solved with find . -type f | grep -i decor *
But this doesn't seem grep for all files that are found from find but rather what's on top of current directory.
Do I have to use something other than * for grep to grep for files found from find?


what needs to be piped to so that it will display with a file name and the number of occurrences for decor for each file?


Answer



You can do:


find . -type f -exec grep -icH decor {} \; | grep -v :0\$

This will get the filename and count of each match and remove files with a count of 0 using the piped egrep


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...