Quantcast
Channel: linux
Viewing all articles
Browse latest Browse all 81

Using find and grep to print lines before and after what you’re searching for

$
0
0

Using find and grep to print lines before and after what you’re searching for

A cool thing about the Unix/Linux grep command is that you can show lines before and after a pattern match with the -B and -A options. As an example, I just used this combination of find and grep to search for all Scala files under the current directory that contain the string null. This command prints five lines before and after each null line in each file:

$ find . -type f -name "*.scala" -exec grep -B5 -A5 null {} \;

That’s good stuff, but it prints a really long list of lines, and I can’t tell the output of one file from another. To fix this, I put the following code in a file named helper.sh, and made it executable:


Viewing all articles
Browse latest Browse all 81

Trending Articles