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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
12-09-2011, 12:36 PM
|
#1
|
|
Member
Registered: Apr 2004
Posts: 509
Rep:
|
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?
|
|
|
|
12-09-2011, 01:01 PM
|
#2
|
|
Member
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562
Rep: 
|
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?
|
|
|
|
12-09-2011, 04:47 PM
|
#3
|
|
Member
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 713
Rep:
|
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
|
|
|
|
12-09-2011, 06:45 PM
|
#4
|
|
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: 302
Rep:
|
Quote:
Originally Posted by walterbyrd
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'
|
|
|
|
12-09-2011, 11:01 PM
|
#5
|
|
Member
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 713
Rep:
|
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-09-2011 at 11:09 PM.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 12:04 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|