LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Can I use grep to find two words near each other? (https://www.linuxquestions.org/questions/linux-software-2/can-i-use-grep-to-find-two-words-near-each-other-917935/)

walterbyrd 12-09-2011 12:36 PM

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?

SecretCode 12-09-2011 01:01 PM

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?

makyo 12-09-2011 04:47 PM

Hi.

See:
Code:

glark - Search text files for complex regular expressions
...
          -a NUM expr1 expr2
          --and NUM expr1 expr2
          --and=NUM expr1 expr2
          ( expr1 --and=NUM expr2 )
              Match both of the two expressions, within NUM lines of each
              other.
...
-- excerpt from man glark

It was in Debian repository, but can also be found at: http://www.incava.org/projects/glark/

Best wishes ... cheers, makyo

Karl Godt 12-09-2011 06:45 PM

Quote:

Originally Posted by walterbyrd (Post 4546065)
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?

probably
Code:

grep -n -i -A4 -B4 PATTERN1 /path/to/file |grep -E 'PATTERN1|PATTERN2'

makyo 12-09-2011 11:01 PM

Hi.

Displaying the context, the synthetic data file, and results:
Code:

#!/usr/bin/env bash

# @(#) s1        Demonstrate match within 4 lines, glark, grep.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C glark grep

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results, glark:"
glark --no-line-number --no-highlight -a 4 "x" "y" $FILE

pl " Results, grep:"
grep -n -i -A4 -B4 "x" data1 |grep -E 'x|y'

exit 0

producing:
Code:

% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny)
GNU bash 3.2.39
glark, version 1.8.0
GNU grep 2.5.3

-----
 Input data file data1:
x y 0
s1
s2
s3
s4
s5
x 1
y
s1
s2
s3
s4
s5
h
x 2
1
y
s1
s2
s3
s4
s5
x 3
1
2
y
s1
s2
s3
s4
s5
x 4
1
2
3
y
s1
s2
s3
s4
s5
x 5
1
2
3
4
y
s1
s2
s3
s4
s5
y 1
x

-----
 Results, glark:
x y 0
x 1
y
x 2
1
y
x 3
1
2
y
x 4
1
2
3
y
y 1
x

-----
 Results, grep:
1:x y 0
7:x 1
8-y
15:x 2
17-y
23:x 3
26-y
32:x 4
36-y
42:x 5
53-y 1
54:x

Best wishes ... cheers, makyo


All times are GMT -5. The time now is 11:29 PM.