Saturday, June 8, 2013

Linux - search for text occurrences in files specified by pattern

Script below searches for files given by pattern (*.java)  containing given key word (case insensitive). It starts from current directory and goes over all sub-directories.
Produced output is divided into sections - one per matching file.
Each section lists key word occurrences within particular file, inclusive line numbers and highlighting.

Create file "se" with following content:
#!/bin/sh
find ${PWD} -name $1  -exec grep --color='auto' -ni $2 '{}' \; -printf '\n\n\n%h-%f\n'

Now in order to find *.java files containing text "nullpointer" execute:
se *.java nullpointer

search result could look like this one:


No comments:

Post a Comment