Today I Learned

hashrocket A Hashrocket project

The -A -B and -Cs of Grep

Given the text file things.txt:

foo
bar
baz

When I type cat things.txt | grep 'bar' Then I see just the one line with bar and nothing else, in fact, I have no idea what comes before or after bar.

To know what comes 1 line before bar I can type cat things.txt | grep 'bar' -B 1 then I see:

foo
bar

To know what comes 1 line after bar I can type cat things.txt | grep 'bar' -A 1 then I see:

bar
baz

And to see 1 line either side of the grep result I can type cat things.txt | grep 'bar' -C 1.

See More #command-line TILs