How to use the "grep/egrep" command to search files.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to use the "grep/egrep" command to search files.
# 1  
Old 01-09-2009
How to use the "grep/egrep" command to search files.

Hi Team,

I am new to this forum and also trying to learn Unix.

I will highly appriciate your help if you can help me to get the right command .

{{{

I use the command " today | egrep '(10:| 11: )' | grep ERROR " to grep all the files that has been error betweeen 10 to 11 O'clock.

}}}

My Question 1.) If i want to see all the files which has been ERROR between the time , let's say from 10:15 to 11:45 what command should i be using?

Please help.

regards,

Ran
# 2  
Old 01-09-2009
please post the sample log.
# 3  
Old 01-10-2009
egrep -l gives you the list of files that contain the pattern.
# 4  
Old 01-18-2009
Quote:
Originally Posted by ahmedwaseem2000
please post the sample log.

Thanks AHMED for asking for the log. Below is the log from my system. Please help if possible

{{

onestop@uto-prod-tx-01ce0:~$ today | grep -i ERROR
1034601 18.01 19:36 pando_baplie S ERROR
800990 18.01 15:14 (emailrouter) R ERROR
641550 18.01 12:13 (emailrouter) R ERROR
478830 18.01 09:13 (emailrouter) R ERROR
322211 18.01 06:13 (emailrouter) R ERROR

}}

regards,

Ran
# 5  
Old 01-18-2009
Oh my goodness I did it Smilie

I was thinking at first I screwed up because I came up with no results, and was getting frustrated, then I realized the sample you provided did not have 10|11 Smilie

So here is the code with find and awk.. although you can alter that..

Code:
find . -maxdepth 1 -type f | xargs awk '( ( $3 ~ /(10|11|12):[0-9]+/ ) && ( $6 ~ /^ERROR$/ ) ) { print FILENAME" : "$0; }'

You can also just do it using egrep, which is a lot shorter, and uses a pretty good regex..

Code:
egrep -r "(10|11|12):[0-9]+ \([a-zA-Z]+\) [a-zA-Z] ERROR$" *
test.txt:641550 18.01 12:13 (emailrouter) R ERROR

Pretty cool I think. Is this what you were asking?
# 6  
Old 01-19-2009
Quote:
Originally Posted by Rhije
Oh my goodness I did it Smilie

I was thinking at first I screwed up because I came up with no results, and was getting frustrated, then I realized the sample you provided did not have 10|11 Smilie

So here is the code with find and awk.. although you can alter that..

Code:
find . -maxdepth 1 -type f | xargs awk '( ( $3 ~ /(10|11|12):[0-9]+/ ) && ( $6 ~ /^ERROR$/ ) ) { print FILENAME" : "$0; }'

You can also just do it using egrep, which is a lot shorter, and uses a pretty good regex..

Code:
egrep -r "(10|11|12):[0-9]+ \([a-zA-Z]+\) [a-zA-Z] ERROR$" *
test.txt:641550 18.01 12:13 (emailrouter) R ERROR

Pretty cool I think. Is this what you were asking?

Hi Mate,

Sorry i appolozise for posting wrong log and using different command. Here is what i was originally talking about.


Code:
 
onestop@uto-prod-tx-01ce0:~$ today | egrep '(10:|11:)' | grep ERROR
 
Log no      Date/Time                     Connection               R (Direction/Received)
 
 909740  19.01 11:44                 PO_Molineaux_PBS    R    ERROR           
 875981  19.01 11:28                 IFTERA_IN               R    ERROR           
 870310  19.01 11:26                 IFTERA_IN               R    ERROR           
 849500  19.01 11:17                 CMR-PANDO-3          R    ERROR           
 821731  19.01 11:13                 (EDIFACT router)      R    ERROR           
 835160  19.01 11:11                 CMR-PANDO-7          R    ERROR           
 834930  19.01 11:11                 CMR-PANDO-0          R    ERROR           
 829080  19.01 11:08                 IFTERA_IN               R    ERROR           
 827330  19.01 11:06                 CMR-PANDO-9          R    ERROR


I used the both command but seems it is not working.

Here you can see the Error i an getting.

1.) onestop@uto-prod-tx-01ce0:~$ find . -maxdepth 1 -type f | xargs awk '( ( $3 ~ /(10|11|12):[0-9]+/ ) && ( $6 ~ /^ERROR$/ ) ) { print FILENAME" : "$0; }'

find: bad option -maxdepth
find: path-list predicate-list
onestop@uto-prod-tx-01ce0:~$


2.) onestop@uto-prod-tx-01ce0:~$ egrep -r "(10|11|12):[0-9]+ \([a-zA-Z]+\) [a-zA-Z] ERROR$" *
egrep: illegal option -- r
usage: egrep [ -bchilnsv ] [ -e exp ] [ -f file ] [ strings ] [ file ] ...
onestop@uto-prod-tx-01ce0:~$


regards,

Ran
# 7  
Old 01-19-2009
A script like this may work

Code:
counter=0
while read line
do
 counter=`expr $counter + 1`
 if [ $counter -gt 2 ]
 then
 hrs=`echo $line | cut -d" " -f 3 | cut -d":" -f 1`
 mins=`echo $line | cut -d" " -f 3 | cut -d":" -f 2`
  if [ $hrs -eq 10 -a $mins -ge 15 ]
  then
   echo $line
  fi
  if [ $hrs -eq 11 -a $mins -le 45 ]
  then
   echo $line
  fi
 fi
done<logfile

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

2. UNIX for Dummies Questions & Answers

Egrep confusion with "I" and "-I" pattern

I am executing following command egrep -w I filename.txt the filename.txt has following data .... -I 07-18 08:31:19.924 9880 6 SessionManager ConnectConfig: ConfigurationWebService LoginResults=SuccessfulLogin I am so hungry that I need to eat I expect egrep to print only the second... (1 Reply)
Discussion started by: VBG
1 Replies

3. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

Hello. Following recommendations for one of my threads, this is working perfectly : #!/bin/bash CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" ) Now I need a grep success for some thing like : #!/bin/bash CNT=$( grep -c -e "some text_1... (4 Replies)
Discussion started by: jcdole
4 Replies

4. Shell Programming and Scripting

Problem with "find" and "grep" command

I want to list all files/lines which except those which contain the pattern ' /proc/' OR ' /sys/' (mind the leading blank). In a first approach I coded: find / -exec ls -ld {} | grep -v ' /proc/| /sys/' \; > /tmp/list.txt But this doesn't work. I got an error (under Ubuntu): grep:... (5 Replies)
Discussion started by: pstein
5 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

grep - search after line containing "unavailable"

Hi everybody; I have a file status.txt: Following 163 ports are up: ----------------------------------------------------- MOD PORT PORTNAMES ABCDEFG1 ABCDEFG2 STA TYPE 1 Prt122 port323-1/2/3 11111 11111 1111 A 1 Prt126 port328-1/2/3 11111 11111 ... (7 Replies)
Discussion started by: gc_sw
7 Replies

7. Shell Programming and Scripting

ps -ef | grep "string1" "string2" " "string3"

Hi all, can any one suggest me the script to grep multiple strings from ps -ef pls correct the below script . its not working/ i want to print OK if all the below process are running in my solaris system. else i want to print NOT OK. bash-3.00$ ps -ef | grep blu lscpusr 48 42 ... (11 Replies)
Discussion started by: steve2216
11 Replies

8. UNIX for Dummies Questions & Answers

search ")" with egrep - egrep: syntax error

Hi Guys, we have a shell script which basically query the Database which retrieves huge data and use the data with "egrep" . Now there is some data which contains characters like "abc)" and the same is used like below : "egrep (.+\|GDPRAB16\|GDPR/11702 96 abc)\|$ temp.txt" now while... (7 Replies)
Discussion started by: sagarjani
7 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

10. UNIX for Dummies Questions & Answers

grep/cat/more -- search in a txt file and display content from a specific "keyword"

Hi, I have a .txt file Sample: ===================== NEXT HOST ===================== AEADBAS001 ip access-list extended BLA_Incoming_Filter ip access-list extended BLA_Outgoing_Filter access-list 1 permit xxxxxxxxxxxxxx access-list 2 permit xxxxxxxxxxxxxx =====================... (4 Replies)
Discussion started by: I-1
4 Replies
Login or Register to Ask a Question