Unix Shell - find a file containing two words in different lines

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2006 > find a file containing two words in different lines





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author find a file containing two words in different lines
chtfj21@googlemail.com

2006-10-25, 1:27 pm

I need to find a text document containing both the words "camolile" and
"ginger".

Doing

egrep -r "(camomile.*ginger|ginger.*camomile)" *

does not work; it will only find the files containing those two words
in the same line,
but not in different lines. Using the newline character .*\n*.* instead
of just .*
doesn't work either.

Does anyone know how to do this?

RolandRB

2006-10-25, 1:27 pm


chtfj21@googlemail.com wrote:
> I need to find a text document containing both the words "camolile" and
> "ginger".
>
> Doing
>
> egrep -r "(camomile.*ginger|ginger.*camomile)" *
>
> does not work; it will only find the files containing those two words
> in the same line,
> but not in different lines. Using the newline character .*\n*.* instead
> of just .*
> doesn't work either.
>
> Does anyone know how to do this?


http://www.datasavantconsulting.com...cripts/nonzgrep

try my script in the form...

nonzgrep ginger $(nonzgrep camomile *)

....but I bet there are better ways to do it.

Ed Morton

2006-10-25, 1:27 pm

chtfj21@googlemail.com wrote:
> I need to find a text document containing both the words "camolile" and
> "ginger".
>
> Doing
>
> egrep -r "(camomile.*ginger|ginger.*camomile)" *
>
> does not work; it will only find the files containing those two words
> in the same line,
> but not in different lines. Using the newline character .*\n*.* instead
> of just .*
> doesn't work either.
>
> Does anyone know how to do this?
>


awk '/camomile/{c=1}/ginger/{g=1}c==g==1{ print "Eureka!" }' file

or, if you have some character that you can guarantee won't appear in
your input file and your input file isn't too huge, then you can set the
awk record separator to that character and read the whole file as one
record:

awk -v RS="xxx" '/camomile/&&/ginger/{ print "Eureka!" }' file

where "xxx" is that special character.

Regards,

Ed.
Chris F.A. Johnson

2006-10-25, 1:27 pm

On 2006-10-25, chtfj21@googlemail.com wrote:
> I need to find a text document containing both the words "camolile" and
> "ginger".
>
> Doing
>
> egrep -r "(camomile.*ginger|ginger.*camomile)" *
>
> does not work; it will only find the files containing those two words
> in the same line,
> but not in different lines. Using the newline character .*\n*.* instead
> of just .*
> doesn't work either.
>
> Does anyone know how to do this?


Use grep -l to greate a list containing one of the words, then grep
those for the other word:

IFS='
'
ly=$(grep -l camomile *)
grep ginger $ly

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Rakesh Sharma

2006-10-26, 7:16 am


chtfj21@googlemail.com wrote:
> I need to find a text document containing both the words "camolile" and
> "ginger".
>
> Doing
>
> egrep -r "(camomile.*ginger|ginger.*camomile)" *
>
> does not work; it will only find the files containing those two words
> in the same line,
> but not in different lines. Using the newline character .*\n*.* instead
> of just .*
> doesn't work either.
>
> Does anyone know how to do this?
>


sed -e '
/camomile/H
/ginger/H
g
/camomile/!d
/ginger/!d
s/.*/FOUND EM/
q
' yourfile

anbu

2006-10-26, 1:15 pm

sed -n "/camomile/{N;/ginger/s/\n/ /p;}" file

Spiros Bousbouras

2006-10-26, 7:19 pm

Ed Morton wrote:

> chtfj21@googlemail.com wrote:
>
> awk '/camomile/{c=1}/ginger/{g=1}c==g==1{ print "Eureka!" }' file


This isn't going to work. The opening poster has a list
of files. The above won't print the file name and apart
from that the variables c and g retain their value from
one file to the next. The correct way to do it is

awk 'FNR<=1 {c=g=0} /camomile/{c=1} /ginger/{g=1} c && g {print
FILENAME ; nextfile}' list_of_files

The above will work assuming you're using gawk which has
the nextfile statement. If your version of awk does not have
nextfile then the way to do it is

awk 'FNR<=1 {c=g=0 ; d=1} /camomile/{c=1} /ginger/{g=1} c && g &&
d{print FILENAME ; d=0}' list_of_files

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com