LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to use regular expression in grep? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-use-regular-expression-in-grep-783677/)

sagarkha 01-21-2010 02:01 AM

How to use regular expression in grep?
 
On my server, logs are saved in log.gz format. I want to know what regular expression should i use to search both oracle and a particular IP, say 1.2.3.4 in a single log line ?

quanta 01-21-2010 02:16 AM

zcat log.gz | egrep "oracle|1.2.3.4"

Tinkster 01-21-2010 02:20 AM

Quote:

Originally Posted by quanta (Post 3834622)
zcat log.gz | egrep "oracle|1.2.3.4"

He wants AND, not OR.

And since he didn't say which comes first...
Code:

zcat log.gz | egrep "oracle.*1\.\2\.\3\.4|1\.\2\.\3\.4.*oracle"

Cheers,
Tink

quanta 01-21-2010 02:24 AM

Quote:

Originally Posted by Tinkster (Post 3834630)
He wants AND, not OR.

Sorry, I didn't read carefully.

syg00 01-21-2010 02:51 AM

Wrong tool - grep doesn't like AND. At least I've never been able to get a "clean" answer. Use zcat piped to awk.
Code:

zcat log.gz | awk '/oracle/ && /1.2.3.4/'

chrism01 01-21-2010 05:55 PM

When I'm in a hurry, I just use 2 greps

zcat log.gz|grep oracle|grep '1.2.3.4'

or similar.

cola 01-23-2010 08:15 AM

Quote:

Originally Posted by sagarkha (Post 3834613)
On my server, logs are saved in log.gz format. I want to know what regular expression should i use to search both oracle and a particular IP, say 1.2.3.4 in a single log line ?

Mark the thread as "[SOLVED]".It's under the "Thread Tools" above.


All times are GMT -5. The time now is 03:04 PM.