ksh : need to get the 4 th line above and 2 nd below the matched pattern in the log file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh : need to get the 4 th line above and 2 nd below the matched pattern in the log file
# 1  
Old 10-09-2013
Lightbulb ksh : need to get the 4 th line above and 2 nd below the matched pattern in the log file

I have a log file as given below
Code:
 
012/01/21 10:29:02  (111111)  Processing Job '23_369468343464564'
     2012/01/21 10:29:02  (111111)
     Making Job '23_369468343464564.0'...
     2012/01/21 10:29:04 (111111)
     Jobnumber '23_369468343464564' was successful 
     2012/01/21 10:29:04  (111111) 
Job has 123 Docs 234 Pgs 456 Lines 4 Errors
     2012/01/21 10:29:04  (111111)
     Total in Job: 123 documents using 29 KBytes

I want awk the 4th line above (i.e "012/01/21 10:29:02 (111111) Processing batch '23_369468343464564'") and 2nd line below (i.e Job has 123 Docs 234 Pgs 456 Lines 4 Errors ) the pattern

Code:
Jobnumber '23_369468343464564' was successful

Please help
# 2  
Old 10-09-2013
Show o/p requirement its not clear

---------- Post updated at 10:40 AM ---------- Previous update was at 10:37 AM ----------

Quote:
Originally Posted by rpm120
I have a log file as given below
Code:
 
012/01/21 10:29:02  (111111)  Processing Job '23_369468343464564'
     2012/01/21 10:29:02  (111111)
     Making Job '23_369468343464564.0'...
     2012/01/21 10:29:04 (111111)
     Jobnumber '23_369468343464564' was successful 
     2012/01/21 10:29:04  (111111) 
Job has 123 Docs 234 Pgs 456 Lines 4 Errors
     2012/01/21 10:29:04  (111111)
     Total in Job: 123 documents using 29 KBytes

I want awk the 4th line above (i.e "012/01/21 10:29:02 (111111) Processing batch '23_369468343464564'") and 2nd line below (i.e Job has 123 Docs 234 Pgs 456 Lines 4 Errors ) the pattern

Code:
Jobnumber '23_369468343464564' was successful

Please help
if just Jobnumber this will work
Code:
awk '/Jobnumber/' file

This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 10-09-2013
Does it have to be awk as ex can do it all...
Code:
ex -s +"/Jobnumber '23_369468343464564' was successful/-4 p | /Jobnumber '23_369468343464564' was successful/+2 p | q!" file

# 4  
Old 10-09-2013
Lightbulb

Quote:
Originally Posted by Akshay Hegde
Show o/p requirement its not clear

---------- Post updated at 10:40 AM ---------- Previous update was at 10:37 AM ----------



if just Jobnumber this will work
Code:
awk '/Jobnumber/' file

Desired Output:

Code:
012/01/21 10:29:02  (111111)  Processing Job '23_369468343464564'

Job has 123 Docs 234 Pgs 456 Lines 4 Errors

I want to match the entire line (i.e Jobnumber '23_369468343464564' was successful) because the log file may contain other line with same pattern, the log file that I have shared you is a sample.

Last edited by rpm120; 10-09-2013 at 03:38 PM..
# 5  
Old 10-09-2013
Try

Code:
$ cat log 
012/01/21 10:29:02  (111111)  Processing Job '23_369468343464564'
     2012/01/21 10:29:02  (111111)
     Making Job '23_369468343464564.0'...
     2012/01/21 10:29:04 (111111)
     Jobnumber '23_369468343464564' was successful 
     2012/01/21 10:29:04  (111111) 
Job has 123 Docs 234 Pgs 456 Lines 4 Errors
     2012/01/21 10:29:04  (111111)
     Total in Job: 123 documents using 29 KBytes

Code:
$ awk '/Processing/{j=$0}/^Job.*Errors$/{printf j RS  $0 RS}' OFS=\\t log 
012/01/21 10:29:02  (111111)  Processing Job '23_369468343464564'
Job has 123 Docs 234 Pgs 456 Lines 4 Errors

This User Gave Thanks to Akshay Hegde For This Post:
# 6  
Old 10-09-2013
Quote:
Originally Posted by Akshay Hegde
Try

Code:
$ cat log 
012/01/21 10:29:02  (111111)  Processing Job '23_369468343464564'
     2012/01/21 10:29:02  (111111)
     Making Job '23_369468343464564.0'...
     2012/01/21 10:29:04 (111111)
     Jobnumber '23_369468343464564' was successful 
     2012/01/21 10:29:04  (111111) 
Job has 123 Docs 234 Pgs 456 Lines 4 Errors
     2012/01/21 10:29:04  (111111)
     Total in Job: 123 documents using 29 KBytes

Code:
$ awk '/Processing/{j=$0}/^Job.*Errors$/{printf j RS  $0 RS}' OFS=\\t log 
012/01/21 10:29:02  (111111)  Processing Job '23_369468343464564'
Job has 123 Docs 234 Pgs 456 Lines 4 Errors

Thanks for your solution.

As of my understanding you code matches the specific pattern and gets the required line. This works perfectly when these pattern appear constantly every time in the log.

But in my scenario its like only this line - Jobnumber '23_369468343464564' was successful appears constanly every time whereas the fourth line above that and the second line after that line are not always same.
So in order get these two lines (4th line above & 2th line below) every time, for that matter what ever patterns it may be I thought of achieving it in this way.

Hope I am clear about the problem statement.
# 7  
Old 10-09-2013
can you post your log file ? Its difficult for me understand pattern with 9 line
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print previous line of multiple pattern matched line?

Hello, I have below format log file, Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

2. UNIX for Advanced & Expert Users

To print from the first line until pattern is matched

Hi I want to print the line until pattern is matched. I am using below code: sed -n '1,/pattern / p' file It is working fine for me , but its not working for exact match. sed -n '1,/^LAC$/ p' file Input: LACC FEGHRA 0 LACC FACAF 0 LACC DARA 0 LACC TALAC 0 LAC ILACTC 0... (8 Replies)
Discussion started by: Abhisrajput
8 Replies

3. Shell Programming and Scripting

Print line between two patterns when a certain pattern matched

Hello Friends, I need to print lines in between two string when a keyword existed in those lines (keywords like exception, error, failed, not started etc). for example, input: .. Begin Edr ab12 ac13 ad14 bc23 exception occured bd24 cd34 dd44 ee55 ff66 End Edr (2 Replies)
Discussion started by: EAGL€
2 Replies

4. Shell Programming and Scripting

Insert certain field of matched pattern line above pattern

Hello every, I am stuck in a problem. I have file like this. I want to add the fifth field of the match pattern line above the lines starting with "# @D". The delimiter is "|" eg > # @D0.00016870300|0.05501020000|12876|12934|3||Qp||Pleistocene||"3 Qp Pleistocene"|Q # @P... (5 Replies)
Discussion started by: jyu3
5 Replies

5. Shell Programming and Scripting

Count of matched pattern occurences by minute and date in a log file

Anyone knows how to use AWK to achieve the following Sun Feb 12 00:41:01-00:41:59 Success:2 Fail:2 Sun Feb 12 00:42:01-00:42:59 Success:1 Fail:2 Sun Feb 12 01:20:01-01:20:59 Success:1 Fail:2 Mon Feb 13 22:41:01-22:41:59 Success:1 Fail:1 log file: Success Success Fail Fail ... (9 Replies)
Discussion started by: timmywong
9 Replies

6. Shell Programming and Scripting

how do I break line in a file when a pattern is matched ?

Hi All, I am stuck for quite sometime now. Below is a line in my file - GS|ED|001075|001081|20110626|1806|100803|X|004010ST|130|100803001 This line occurs only once and it is the second line. I have to break this line into two lines from ST (bold) such that it looks like -... (5 Replies)
Discussion started by: ihussain
5 Replies

7. Shell Programming and Scripting

Help required on joining one line above & below to the pattern matched string line.

Hi Experts, Help needed on joining one line above & below to the pattern matched string line. The input file, required output is mentioned below Input file ABCD DEFG5 42.0.1-63.38.31 KKKK iokl IP Connection Available ABCD DEFG5 42.0.1-63.38.31 ... (7 Replies)
Discussion started by: krao
7 Replies

8. Shell Programming and Scripting

Count of matched pattern occurences by time in a log file

We have a log file, the format is similar to this: 08/04/2011 05:03:08 Connection Success 08/04/2011 05:13:18 Connection Success 08/04/2011 05:23:28 Connection Fail 08/04/2011 05:33:38 Connection Success 08/04/2011 06:14:18 Connection Success 08/04/2011 06:24:28 Connection Fail 08/04/2011... (6 Replies)
Discussion started by: clu
6 Replies

9. Shell Programming and Scripting

awk script to move a line after the matched pattern line

I have the following text format in a file which lists the question first and then 5 choices after that the explanantion and finally the answer. 1.The amount of time it takes for most of a worker’s occupational knowledge and skills to become obsolete has been declining because of the... (2 Replies)
Discussion started by: nanchil_guy
2 Replies

10. Shell Programming and Scripting

save every line in log file with matched string

i have been doing this script to match every line in a current log file (access_log) with strings that i list from a path (consist of 100 of user's name ex: meggae ).. and then make a directory of every string from the text file (/path/meggae/) --->if it matched.. then print every line from the... (3 Replies)
Discussion started by: meggae
3 Replies
Login or Register to Ask a Question