LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices



Reply
 
Search this Thread
Old 12-09-2011, 01:36 PM   #1
walterbyrd
Member
 
Registered: Apr 2004
Posts: 572

Rep: Reputation: 30
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?
 
Old 12-09-2011, 02:01 PM   #2
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
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?
 
Old 12-09-2011, 05:47 PM   #3
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 719

Rep: Reputation: 72
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
 
Old 12-09-2011, 07:45 PM   #4
Karl Godt
Member
 
Registered: Mar 2010
Location: Kiel , Germany
Distribution: once:SuSE6.2,Debian3.1, aurox9.2+3,Mandrake?,DSL? then:W7st,WVHB, #!8.10.02,PUPPY4.3.1 now:Macpup
Posts: 308

Rep: Reputation: 45
Quote:
Originally Posted by walterbyrd View Post
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'
 
Old 12-10-2011, 12:01 AM   #5
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 719

Rep: Reputation: 72
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

Last edited by makyo; 12-10-2011 at 12:09 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
grep multiple words and linking with other file zonah12 Linux - Newbie 11 11-17-2011 11:29 PM
how to grep 2 words at the same time ufmale Linux - Newbie 2 09-09-2010 05:25 PM
Grep words and paste is on the same line say_hi_ravi Programming 7 10-31-2008 07:56 AM
GREP - reg exp to find words ending with .V and .TO vikram_cvk Linux - Server 5 12-22-2006 02:47 PM
Can grep filter out words? extrasolar Linux - General 1 07-20-2006 04:14 PM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration