Can I use grep to find two words near each other?
Is there a way to use grep to search a text document to find two words that are -say- within four lines of each other?
|
I'm sure it's possible to build a regular expression that finds two words within 4 lines of each other ... word1(.*\n){0,4}word2 would be my starting point (not tested, needs some work)
But I think grep is only capable of matching single lines at a time, so with grep itself, no. What do you need to achieve? |
Hi.
See: Code:
glark - Search text files for complex regular expressions Best wishes ... cheers, makyo |
Quote:
Code:
grep -n -i -A4 -B4 PATTERN1 /path/to/file |grep -E 'PATTERN1|PATTERN2' |
Hi.
Displaying the context, the synthetic data file, and results: Code:
#!/usr/bin/env bash Code:
% ./s1 |
All times are GMT -5. The time now is 11:29 PM. |