From: Janis Papanagnou on 11 Aug 2010 14:54 On 11/08/10 19:44, Chris wrote: > On Aug 11, 7:20 pm, pk <p...(a)pk.invalid> wrote: >> On Wed, 11 Aug 2010 10:26:47 -0700 (PDT) Chris <cconnel...(a)lycos.com> wrote: >> >>> Hi >>> I have a file containing 2 words on 2 seperate lines: >> >>> e.g. >> >>> car >>> bike >> >>> I want to pipe this file through sed or awk, and only return the >>> output only **both** words are found. Could someone help me do this? >> >> Which output are you talking about? >> >>> As they are on different lines grep wont work. >> >> You can pipe two greps but without knowing what it is that you want to >> output it's difficult to tell. >> >> What do you want to output? Matching lines? The whole file? The file name? > > I would like to return the matching lines. But only print the lines if > both strings exist in the file. How many occurrences may be present and want to output? awk ' /car/ { car = $0 } /bike/ { bike = $0 } END { if (car && bike) print car ORS bike } ' Not bulletproof, but who knows what you really want. :-) awk ' /car/ { cars = cars ORS $0 } /bike/ { bikes = bikes ORS $0 } END { if (cars && bikes) print cars ORS bikes } ' Janis > >
From: Chris on 11 Aug 2010 15:41 On Aug 11, 8:54 pm, Janis Papanagnou <janis_papanag...(a)hotmail.com> wrote: > On 11/08/10 19:44, Chris wrote: > > > > > On Aug 11, 7:20 pm, pk <p...(a)pk.invalid> wrote: > >> On Wed, 11 Aug 2010 10:26:47 -0700 (PDT) Chris <cconnel...(a)lycos.com> wrote: > > >>> Hi > >>> I have a file containing 2 words on 2 seperate lines: > > >>> e.g. > > >>> car > >>> bike > > >>> I want to pipe this file through sed or awk, and only return the > >>> output only **both** words are found. Could someone help me do this? > > >> Which output are you talking about? > > >>> As they are on different lines grep wont work. > > >> You can pipe two greps but without knowing what it is that you want to > >> output it's difficult to tell. > > >> What do you want to output? Matching lines? The whole file? The file name? > > > I would like to return the matching lines. But only print the lines if > > both strings exist in the file. > > How many occurrences may be present and want to output? > > awk ' > /car/ { car = $0 } > /bike/ { bike = $0 } > END { if (car && bike) print car ORS bike } > ' > > Not bulletproof, but who knows what you really want. :-) > > awk ' > /car/ { cars = cars ORS $0 } > /bike/ { bikes = bikes ORS $0 } > END { if (cars && bikes) print cars ORS bikes } > ' > > Janis > > Hi, this is fine and thanks for the answers..
From: Ed Morton on 11 Aug 2010 16:33
On Aug 11, 12:44 pm, Chris <cconnel...(a)lycos.com> wrote: > On Aug 11, 7:20 pm, pk <p...(a)pk.invalid> wrote: > > > > > > > On Wed, 11 Aug 2010 10:26:47 -0700 (PDT) Chris <cconnel...(a)lycos.com> wrote: > > > > Hi > > > I have a file containing 2 words on 2 seperate lines: > > > > e.g. > > > > car > > > bike > > > > I want to pipe this file through sed or awk, and only return the > > > output only **both** words are found. Could someone help me do this? > > > Which output are you talking about? > > > > As they are on different lines grep wont work. > > > You can pipe two greps but without knowing what it is that you want to > > output it's difficult to tell. > > > What do you want to output? Matching lines? The whole file? The file name? > > I would like to return the matching lines. But only print the lines if > both strings exist in the file.- Hide quoted text - > > - Show quoted text - Something like this might be what you're looking for: awk ' /car/ { car = car $0 ORS if (bike) { printf "%s%s",bike,car bike=car="" } } /bike/ { bike = bike $0 ORS if (car) { printf "%s%s",car,bike bike=car="" } } ' file Depends what you'd really want to do with: car car bike Regards, Ed. |