LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-05-2011, 12:26 PM   #1
samanp
LQ Newbie
 
Registered: Dec 2005
Posts: 22

Rep: Reputation: 0
filter source line based on results line in log using awk and sed


Hi,

I have a log file with entries such as

<time stamp> <file name>
<time stamp> <result>

where result is 0 or more.

I want to get the list of file names where result count is more than 0 (in other words filter out files names with result 0).

File name and result are in two different lines in log file.

How can i filter file name based on result?

Thanks in advance.
 
Old 04-05-2011, 12:35 PM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
Quote:
Originally Posted by samanp View Post
<time stamp> <file name>
<time stamp> <result>
Assuming they're on consecutive lines, then
Code:
awk '(NF==2 && $2!=0 && $2~/^-*[1-9][0-9]*$/) { print last ; print $0 ; next } { last=$0 }' file
should do the trick. It keeps the previous line in last, printing it and the current line if the second field in the current line is numeric and nonzero. You can modify the last=$0 and print last ; print $0 statements to define which bits are output.

Hope this helps.
 
Old 04-05-2011, 02:07 PM   #3
samanp
LQ Newbie
 
Registered: Dec 2005
Posts: 22

Original Poster
Rep: Reputation: 0
thanks for the quick reply. i'll take a look.
 
Old 04-05-2011, 02:19 PM   #4
samanp
LQ Newbie
 
Registered: Dec 2005
Posts: 22

Original Poster
Rep: Reputation: 0
unfortunately i couldn't modify it to my log. following are some of the actual log entries. can you please help

[2011-03-31 15:03:46] INFO systemmsg Printing chk_print 1/6: /data/datprint/chk_print/weekly/chk_print/chk_print_master_job.01181
[2011-03-31 15:03:48] INFO systemmsg Result 0 documents
[2011-03-31 15:03:49] INFO systemmsg Printing chk_print 2/6: /data/datprint/chk_print/weekly/chk_print/chk_print_master_job.01182
[2011-03-31 15:03:51] INFO systemmsg Result 0 documents
[2011-03-31 15:03:52] INFO systemmsg Printing chk_print 3/6: /data/datprint/chk_print/weekly/chk_print/chk_print_master_job.01183
[2011-03-31 15:04:06] INFO systemmsg Result 1 documents
[2011-03-31 15:04:07] INFO systemmsg Printing chk_print 4/6: /data/datprint/chk_print/weekly/chk_print/chk_print_master_job.01184
[2011-03-31 15:04:09] INFO systemmsg Result 0 documents
[2011-03-31 15:04:10] INFO systemmsg Printing chk_print 5/6: /data/datprint/chk_print/weekly/chk_print/chk_print_master_job.01185
[2011-03-31 15:04:12] INFO systemmsg Result 0 documents
[2011-03-31 15:04:13] INFO systemmsg Printing chk_print 6/6: /data/datprint/chk_print/weekly/chk_print/chk_print_master_job.01186
[2011-03-31 15:04:25] INFO systemmsg Result 2 documents
 
Old 04-05-2011, 04:06 PM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
Quote:
Originally Posted by samanp View Post
[2011-03-31 15:03:46] INFO systemmsg Printing chk_print 1/6: /data/datprint/chk_print/weekly/chk_print/chk_print_master_job.01181
[2011-03-31 15:03:48] INFO systemmsg Result 0 documents
[2011-03-31 15:03:49] INFO systemmsg Printing chk_print 2/6: /data/datprint/chk_print/weekly/chk_print/chk_print_master_job.01182
[2011-03-31 15:03:51] INFO systemmsg Result 0 documents
[2011-03-31 15:03:52] INFO systemmsg Printing chk_print 3/6: /data/datprint/chk_print/weekly/chk_print/chk_print_master_job.01183
[2011-03-31 15:04:06] INFO systemmsg Result 1 documents
[2011-03-31 15:04:07] INFO systemmsg Printing chk_print 4/6: /data/datprint/chk_print/weekly/chk_print/chk_print_master_job.01184
[2011-03-31 15:04:09] INFO systemmsg Result 0 documents
[2011-03-31 15:04:10] INFO systemmsg Printing chk_print 5/6: /data/datprint/chk_print/weekly/chk_print/chk_print_master_job.01185
[2011-03-31 15:04:12] INFO systemmsg Result 0 documents
[2011-03-31 15:04:13] INFO systemmsg Printing chk_print 6/6: /data/datprint/chk_print/weekly/chk_print/chk_print_master_job.01186
[2011-03-31 15:04:25] INFO systemmsg Result 2 documents
The format is a bit different than what you initially said, that is all. If the file names do not have spaces in them, then you can use
Code:
awk '($5=="Result" && $6>0) { print fn ; next } ($5=="Printing") { fn=$8 }' file
 
Old 04-06-2011, 09:42 AM   #6
samanp
LQ Newbie
 
Registered: Dec 2005
Posts: 22

Original Poster
Rep: Reputation: 0
that worked perfectly. thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] move a line with either sed/awk/perl script don_wombat_73 Linux - Newbie 9 07-07-2009 01:06 PM
replace a pattern with a line using sed/awk lokeshn05 Linux - Newbie 3 05-06-2009 03:01 PM
extract part of a line with sed or awk alirezan1 Linux - Newbie 2 10-01-2008 09:44 PM
Problem changing line with sed. Should i use AWK? RattleSn@ke Linux - General 7 11-12-2007 01:03 PM
sed/awk/grep for multiple line data hotrodmacman Programming 8 10-18-2007 11:06 AM


All times are GMT -5. The time now is 09:47 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration