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

Re: More grep

__/ [ 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.

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


,----[ file1 ]
| apple
| orange
| apricot
`----

,----[ Command ]
| 
| grep 'ap' file1 | grep '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.

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

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