Home Messages Index
[Date Prev][Date Next][Thread Prev][Thread Next]
Author IndexDate IndexThread Index

Re: More grep

  • Subject: Re: More grep
  • From: The Ghost In The Machine <ewill@xxxxxxxxxxxxxxxxxxxxxxx>
  • Date: Wed, 03 May 2006 19:02:21 GMT
  • Newsgroups: comp.os.linux.advocacy
  • Organization: EarthLink Inc. -- http://www.EarthLink.net
  • References: <JpWdnRatH572f8rZRVn-iA@speakeasy.net> <f88li3-e0o.ln1@sirius.tg00suus7038.net> <3836198.nTpF50icrJ@schestowitz.com>
  • User-agent: slrn/0.9.8.1 (Linux)
  • Xref: news.mcc.ac.uk comp.os.linux.advocacy:1106051
In comp.os.linux.advocacy, Roy Schestowitz
<newsgroups@xxxxxxxxxxxxxxx>
 wrote
on Wed, 03 May 2006 07:07:18 +0100
<3836198.nTpF50icrJ@xxxxxxxxxxxxxxx>:
> __/ [ The Ghost In The Machine ] on Wednesday 03 May 2006 01:00 \__
>
>> In comp.os.linux.advocacy, John A. Bailo
>> <jabailo@xxxxxxxxxx>
>>  wrote
>> on Tue, 02 May 2006 15:51:46 -0700
>> <JpWdnRatH572f8rZRVn-iA@xxxxxxxxxxxxx>:
>>>
>>> Say I have a grep statement that returns a list of words.
>>>
>>> I then want to run that list in a series of greps on a file to search
>>> for lines that contain any of those words.
>>>
>>>
>>> grep -o a+ data.txt
>>>
>>>
>>> Then, I want to take the results of that list, and grep with each of the
>>> terms.
>>>
>>> So say it returns
>>>
>>> apple
>>> amber
>>> apricot
>>>
>>>
>>> Then I want to run a second grep
>>>
>>> grep <grep -o a+ data.txt> data.txt
>>>
>>>
>>> How can I do this?
>>>
>> 
>> I'm not entirely sure what you're desiring here, but taking your
>> statement literally the list of words (apparently you've found '-o';
>> congrats) is of course storable in a temporary file:
>> 
>> grep -o a+ data.txt > tempfile.txt
>> 
>> and then one can simply use that file into the -f switch:
>> 
>> grep -f tempfile.txt data.txt
>> 
>> or perhaps
>> 
>> grep -f tempfile.txt *.txt
>> 
>> One might also try
>> 
>> grep -F "`grep -o a+ data.txt`" data.txt
>> 
>> but that's a little iffier.
>
> Also consider piping the output, which mean that you have a chain of
> commands, each of which feeds the output to its successor in the chain.

The main problem is that it's far from clear that the -F option can
accept unnamed pipes.  One can try 'mkfifo', perhaps, or try your
solution below -- which would probably work reasonably well.

>
> Since your (as in Bailo's) example is rather obscure, I'll
> give a separate, generalisable example.

It's an odd request, certainly.

>
>
> ,----[ file1 ]
> | apple
> | orange
> | apricot
> `----
>
> ,----[ Command ]
> | 
> | grep 'ap' file1 | grep 'apple'
> | 
> `----

grep -i 'ap' file1 | grep -i apple

might be a little more along his wishes; it will match apple, APPLE, and
Apple.

>
> This will first reduce your list to everything containing the (sub)string
> "ap" ("apricot" and "apple") and then filter this further to pick just
> apples. No need for temporary, disposable files, which makes this more
> elegant.

There is something to be said for elegance. :-)

>
> Hope it helps,
>
> Roy
>
>
> Roy S. Schestowitz      |    "Somebody, give this politician a wedgie"
> http://Schestowitz.com  |  SuSE GNU/Linux   ¦     PGP-Key: 0x74572E8E
>   7:00am  up 5 days 13:57,  12 users,  load average: 0.81, 0.68, 0.56
>       http://iuron.com - help build a non-profit search engine


-- 
#191, ewill3@xxxxxxxxxxxxx
Windows Vista.  Because it's time to refresh your hardware.  Trust us.

[Date Prev][Date Next][Thread Prev][Thread Next]
Author IndexDate IndexThread Index