Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Greping for multiple strings in a file 1

Status
Not open for further replies.

ddrillich

Technical User
Jun 11, 2003
546
US
Good Day,

We have the following log file. At certain intervals, we want to ensure that three engines are referred to in this log file. So I thought of tailing the log file and search for these three strings – "for engine 0", "for engine 1" and "for engine 2".

Is there an elegant way to write the grep logic?

10/12/2009 11:43:53 [1] ChildID=707908 IndexID=4775801 for engine 0, cmd /DREADDDATA?DREDBNAME=autowire
10/12/2009 11:43:53 [3] [2] Time to send command 0.00 secs.
10/12/2009 11:43:53 [3] ChildID=708433 IndexID=4775801 for engine 2, cmd /DREADDDATA?DREDBNAME=autowire
10/12/2009 11:43:53 [2] [1] Time to send command 0.00 secs.
10/12/2009 11:43:53 [2] ChildID=707899 IndexID=4775801 for engine 1, cmd /DREADDDATA?DREDBNAME=autowire
10/12/2009 11:44:09 [0] Index command from 10.240.81.248 allowed
10/12/2009 11:44:09 [0] Streamed 5.03 kB (5153 bytes) to disk in 0.00 secs. Rate 11718.75 kB/s.
10/12/2009 11:44:09 [0] Index Queue Command: /DREADDDATA?DREDBNAME=pr
10/12/2009 11:44:09 [0] Saved command file to disk. IndexID=4775802.
10/12/2009 11:44:09 [1] [0] Time to send command 0.00 secs.
10/12/2009 11:44:09 [1] ChildID=707909 IndexID=4775802 for engine 0, cmd /DREADDDATA?DREDBNAME=pr
10/12/2009 11:44:09 [3] [2] Time to send command 0.00 secs.
10/12/2009 11:44:09 [3] ChildID=708434 IndexID=4775802 for engine 2, cmd /DREADDDATA?DREDBNAME=pr
10/12/2009 11:44:09 [2] [1] Time to send command 0.00 secs.
10/12/2009 11:44:09 [2] ChildID=707900 IndexID=4775802 for engine 1, cmd /DREADDDATA?DREDBNAME=pr


Regards,
Dan
 
grep doesn't support "and" logic, so one way would be to handle it using a loop, something like this:

Code:
TAIL=$(tail -15 /the/log/file)
for engine in 0 1 2
do
        if echo "$TAIL" | grep -q "for engine $engine,"
        then
                :
        else
                echo "engine $engine is not running!"
        fi
done

Annihilannic.
 
Hi
Here is another way to grep multiple strings in the same file

code
tail -100 logtest |grep -Ei "engine 0|engine 1|engine 2"

Here is how the o/p
$ grep -Ei "engine 0|engine 1|engine 2" logtest
10/12/2009 11:43:53 [1] ChildID=707908 IndexID=4775801 for engine 0, cmd /DREADDDATA?DREDBNAME=autowire
10/12/2009 11:43:53 [3] ChildID=708433 IndexID=4775801 for engine 2, cmd /DREADDDATA?DREDBNAME=autowire
10/12/2009 11:43:53 [2] ChildID=707899 IndexID=4775801 for engine 1, cmd /DREADDDATA?DREDBNAME=autowire
10/12/2009 11:44:09 [1] ChildID=707909 IndexID=4775802 for engine 0, cmd /DREADDDATA?DREDBNAME=pr
10/12/2009 11:44:09 [3] ChildID=708434 IndexID=4775802 for engine 2, cmd /DREADDDATA?DREDBNAME=pr
10/12/2009 11:44:09 [2] ChildID=707900 IndexID=4775802 for engine 1, cmd /DREADDDATA?DREDBNAME=pr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top