- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Need pattern matching commands from a log file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2010 12:06 PM
02-23-2010 12:06 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2010 12:20 PM
02-23-2010 12:20 PM
Re: Need pattern matching commands from a log file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2010 12:30 PM
02-23-2010 12:30 PM
Re: Need pattern matching commands from a log file
# VALS=$(perl -nle 'm{Success.+\s:(\d+)} && $1 > 9000 and print $1' file
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2010 12:31 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2010 12:38 PM
02-23-2010 12:38 PM
Re: Need pattern matching commands from a log file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2010 01:02 PM
02-23-2010 01:02 PM
Re: Need pattern matching commands from a log file
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
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2010 01:12 PM
02-23-2010 01:12 PM
Re: Need pattern matching commands from a log file
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2010 02:01 PM
02-23-2010 02:01 PM
Re: Need pattern matching commands from a log file
Some of the lines will be as mentioned below:
Success (code 0) :10001
Success (code 0) :12004
Success (code 0) :9021
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2010 02:11 PM
02-23-2010 02:11 PM
Re: Need pattern matching commands from a log file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2010 02:21 PM
02-23-2010 02:21 PM
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.
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2010 02:58 PM
02-23-2010 02:58 PM
Re: Need pattern matching commands from a log file
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
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2010 03:42 PM
02-23-2010 03:42 PM
Re: Need pattern matching commands from a log file
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2010 03:45 PM
02-23-2010 03:45 PM
Re: Need pattern matching commands from a log file
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)
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2010 04:39 PM
02-23-2010 04:39 PM
Re: Need pattern matching commands from a log file
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2010 12:48 AM
02-24-2010 12:48 AM
Re: Need pattern matching commands from a log file
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) {
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2010 12:54 AM
02-24-2010 12:54 AM
Re: Need pattern matching commands from a log file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2010 01:04 AM
02-24-2010 01:04 AM
Re: Need pattern matching commands from a log file
>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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2010 01:17 AM
02-24-2010 01:17 AM
Re: Need pattern matching commands from a log file
you're rigth!
Sorry I dind't read comamnd carefully: -F: was already at the beginning.
Discard my reply!
Rgds,
Art