Operating System - HP-UX
1833465 Members
2764 Online
110052 Solutions
New Discussion

Need pattern matching commands from a log file

 
SOLVED
Go to solution
Shivkumar
Super Advisor

Need pattern matching commands from a log file

Hello,

I am looking out for a pattern in a log file say somefile.log

The log gets entries like below sometime.

Success (code 0) :10001

Success (code 0) :9104

Success (code 0) :9021

I need to grep string "Success (code 0)" and number if it is less than 9000 in the log file.

once the above pattern match is successful then i will need to assign it to a variable called TEMPFILE like below:

TEMPFILE= `Pattern matching commands`

Appreciate your help.

Thanks,
Shiv
17 REPLIES 17
mvpel
Trusted Contributor

Re: Need pattern matching commands from a log file

Do you have codes lower than 1000? If you can assume it's always going to be four digits it makes the grep pattern easier.
James R. Ferguson
Acclaimed Contributor

Re: Need pattern matching commands from a log file

Hi Shiv:

# VALS=$(perl -nle 'm{Success.+\s:(\d+)} && $1 > 9000 and print $1' file

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor
Solution

Re: Need pattern matching commands from a log file

Hi (again):

Oops: you wanted less than 9000:

# VALS=$(perl -nle 'm{Success.+\s:(\d+)} && $1 < 9000 and print $1' file

Regards!

...JRF...
Shivkumar
Super Advisor

Re: Need pattern matching commands from a log file

Is it possible to have shell script commands as well ?
Steven E. Protter
Exalted Contributor

Re: Need pattern matching commands from a log file

Shalom Shiv,

That last perl command looks pretty good to me.

One command line that does it all.

Partial attempt below...

I could do it multi step in the command line.

grep Success > tfile1

while read -r DL
do
CODE=$(echo $DL | awk -F: '{ print $2}')
< perhaps a case statement here >

done < tfile1

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
James R. Ferguson
Acclaimed Contributor

Re: Need pattern matching commands from a log file

Hi (again) Shif:

> Is it possible to have shell script commands as well ?

Given your format, something like this would work for you:

# cat ./findit
#!/usr/bin/sh
while read LINE
do
echo "${LINE}" | grep -q Success || continue
VAL=$( echo "${LINE}" | cut -d: -f2 )
if [ "${VAL}" -lt 9000 ]; then
VALUES=$( echo "${VALUES} ${VAL}" )
fi
done < $1
echo ${VALUES}

...run as:

# ./findit file

Regards!

...JRF...
Shivkumar
Super Advisor

Re: Need pattern matching commands from a log file

Forgot to mention that log file will contain some other lines containing different strings for errors.

Some of the lines will be as mentioned below:
Success (code 0) :10001

Success (code 0) :12004

Success (code 0) :9021
Patrick Wallek
Honored Contributor

Re: Need pattern matching commands from a log file

That looks the same as what you originally posted.
James R. Ferguson
Acclaimed Contributor

Re: Need pattern matching commands from a log file

Hi (again) Shiv:

> Forgot to mention that log file will contain some other lines containing different strings for errors.

The code (both Perl and shell) I posted reports the values that follow a ":". If the line doesn't contain the string "Success" then the line isn't a candidate and is skipped.

Regards!

...JRF...
Steven E. Protter
Exalted Contributor

Re: Need pattern matching commands from a log file

Shalom again Shiv,

Take note:

VAL=$( echo "${LINE}" | cut -d: -f2 )

That line of excellent shell code assumes the data we need to look at comes after the :

If that is not the case with all your data, further adjustment will be required.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
James R. Ferguson
Acclaimed Contributor

Re: Need pattern matching commands from a log file

Hi:

> SEP: Take note...That line of excellent shell code assumes the data we need to look at comes after the :

Yes, that's what I said!

> SEP: If that is not the case with all your data, further adjustment will be required.

That goes without saying. After all, that's what good parsing is all about.

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Need pattern matching commands from a log file

>once the above pattern match is successful then I will need to assign it to a variable called TEMPFILE like below:
TEMPFILE= `Pattern matching commands`

You want TEMPFILE to have a copy of ALL lines matching your pattern? Or only the first?

TEMPFILE=$(awk -F: '
/Success \(code 0\)/ {
if ($2 < 9000) {
print $0
exit # if want only first
}
}' file)
Shivkumar
Super Advisor

Re: Need pattern matching commands from a log file

Dennis,

I put your code in an script and ran it in debug mode by putting first line as #!/bin/ksh -x).

Got below output:

+ + awk -F:
/Success \(code 0\)/ {
if ($2 < 9000) {
print $0
exit # if want only first
}
} bh.log
TEMPFILE=Success (code 0) :10002

============================
Looks like it is wrongly comparing.
Dennis Handly
Acclaimed Contributor

Re: Need pattern matching commands from a log file

>Looks like it is wrongly comparing.

I can't get it to fail like you have. It only fails for me if you have something after the number:
Success (code 0) :80210 xx

I fixed it with: if ($2 + 0 < 9000) {
Arturo Galbiati
Esteemed Contributor

Re: Need pattern matching commands from a log file

Hello,
try:
TEMPFILE=$(awk -F: '
/Success \(code 0\):/ {
if ($2 < 9000) {
print $0
exit # if want only first
}
}' file)

Note ":" added. In this way in the second file ($2( you shoulkd have only numeric value.

HTH,
Art
Dennis Handly
Acclaimed Contributor

Re: Need pattern matching commands from a log file

>Arturo: /Success \(code 0\):/ {
>Note ":" added. In this way in the second field ($2) you should have only numeric value.

This doesn't add/fix anything unless you want to make sure that ":" is matched.
Arturo Galbiati
Esteemed Contributor

Re: Need pattern matching commands from a log file

Hi Dennis,
you're rigth!
Sorry I dind't read comamnd carefully: -F: was already at the beginning.
Discard my reply!
Rgds,
Art