Web Hosting Talk







View Full Version : Grep'ing more than one variable


obviousl
11-15-2004, 11:26 PM
Hi all,

How would I go about grep'ing more than one variable ?

ie:

cat access.log | grep ".mp3 "

Thanks :)

Chris

kloch
11-15-2004, 11:30 PM
If you want to do it as a logical AND then just pipe several greps together:

grep "asdf" access-log | grep "jkl" | grep "jfjeje"

If you want to do it as a logical OR then you have to create
a suitable regular expression. It's usually easier to do several
greps in succession and combine the results.

grep "asdf" access-log >> tmpfile
grep "jkl" access-log >> tmpfile

obviousl
11-15-2004, 11:39 PM
Ok coolio :)

Cheers

Chris

SPaReK
11-16-2004, 04:28 PM
To do an OR, you can also use egrep or grep -E (Extended grep)

egrep "asdf|jkl" access-log >tmpfile

or

grep -E "asdf|jkl" access-log >tmpfile

itman
12-01-2004, 07:17 AM
Taking this one step further, i am trying to search text log files to match on 2 items, in this case a country code (DEU) and an error code (C075).

I have tried :

grep -il C075 < grep -il DEU *.txt

and

grep -il C075 | grep -il DEU *.txt

but they dont work or dont give me the results i am looking for. Any other thoughts? Any help greatfully received.

Thanks in advance.

brianoz
12-01-2004, 07:36 AM
From your attempts my guess is that you want:

grep -il C075 *.txt | grep -il DEU

itman
12-02-2004, 03:45 AM
no this still doesnt work ... oh well ... will keep trying various combinations

brianoz
12-02-2004, 06:19 AM
when you say "it doesn't work" that's not enough information for anyone to help!!!!!!!!!!! You have to say:
- what you typed in, exactly;
- what the computer did, exactly (copy and paste is good);
- what you expected
Armed with that information we'll have an intelligent discussion! Nowhere in this thread have you said exactly what you wanted (even the initial post is really vague!)

Yes, that WILL work, if you type it in correctly. It will return matches for lines that have BOTH the string 'C075' and the string 'DEU' in any order. If that's not what you wanted, that's why it appears not to work, when in fact it's doing exactly what you told it to do. Remember computers are stupid :)

Perhaps if you want to do this thing you might be well served by buying a small "dummies" guide to Unix shell or shell scripting.

- Brian

cheese32
12-02-2004, 12:27 PM
The -l just lists the filenames, so having it after a pipe is not that useful. Try

display files: grep -li DEU `grep -li C075 *.txt`
lines: grep -i DEU *.txt | grep -i C075 #optionally add -h in the first grep

Look at awk(1) if you want to do more of that stuff.

itman
12-03-2004, 04:39 AM
Doh .... thanks for that ... completely forgot about the ` things.

Worked perfectly ... many thanks for your help.