How to grep lines only one or two before and above of my line, I use
grep --color=always -Irn -E 'foo|bar' but it finds me lines through all document:
foo.md:10:foo foo.md:142:bar I need only a coincidence of NEARBY lines, one or maybe two.
foo.md:10:foo foo.md:12:bar Something like that.
1 Answer
I tried to find for several hours, but as always it was so simple:
grep --color=always -Irn 'foo' -A1 -B1 | grep -In --color=always "bar" -A2 -B1 Finding 'foo' and 1 line after '-A1' and 1 line before '-B1', pass this to second grep and process with keyword 'bar' and voilà! -I is for ignoring binaries, -r means recursive search and -n means output line number. --color=always - highlights matched string.
2