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
# 8  
Old 10-09-2013
Quote:
Originally Posted by shamrock
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


I really liked this ed-based approach, as I often find myself looking for an N-th line above/below a given line in logs and I usually write an ugly awk which remembers lines before/after.

I tried it and somehow it did not work please help me to find what do I do wrong here:
Code:
$ cat xyz                    
line 1
line to show
line 2
line 3
line key
line 4
line 5
$ ed -s +"/key/-3 p | q!" xyz
ed: illegal commands -- /
usage: ed [- | -s] [-p string] [-x] [-C] [file]

sorry for offtop
# 9  
Old 10-09-2013
Lightbulb

Quote:
Originally Posted by Akshay Hegde
can you post your log file ? Its difficult for me understand pattern with 9 line
Here is the log with more lines
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
012/01/21 10:29:02  (111111)  Processing Job '34_794862826912345'
     2012/01/21 10:29:02  (111111)
     Making Job '34_794862826912345.0'...
     2012/01/21 10:29:04 (111111)
     Jobnumber '34_794862826912345' 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
012/01/21 10:29:02  (111111)  Processing Job '98_973986746290898'
     2012/01/21 10:29:02  (111111)
     Making Job '98_973986746290898.0'...
     2012/01/21 10:29:04 (111111)
     Jobnumber '98_973986746290898' 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

# 10  
Old 10-09-2013
try

Code:
$ awk '{j=e=""}/Processing/{j=$0}/^Job.*Errors$/{e=$0}(j){print j}(e){print e}' log

Code:
012/01/21 10:29:02  (111111)  Processing Job '23_369468343464564'
Job has 123 Docs 234 Pgs 456 Lines 4 Errors
012/01/21 10:29:02  (111111)  Processing Job '34_794862826912345'
Job has 123 Docs 234 Pgs 456 Lines 4 Errors
012/01/21 10:29:02  (111111)  Processing Job '98_973986746290898'
Job has 123 Docs 234 Pgs 456 Lines 4 Errors

# 11  
Old 10-10-2013
Quote:
Originally Posted by migurus
I really liked this ed-based approach, as I often find myself looking for an N-th line above/below a given line in logs and I usually write an ugly awk which remembers lines before/after.

I tried it and somehow it did not work please help me to find what do I do wrong here:
Code:
$ cat xyz                    
line 1
line to show
line 2
line 3
line key
line 4
line 5
$ ed -s +"/key/-3 p | q!" xyz
ed: illegal commands -- /
usage: ed [- | -s] [-p string] [-x] [-C] [file]

sorry for offtop
I posted an ex one liner...which is not doable in ed as it is far more primitive than ex...but you can do the same thing in ed using the here document facility...
Code:
ed -s file <<eof
/key/-3 p
q
eof

Hope this helps...
This User Gave Thanks to shamrock For This Post:
# 12  
Old 10-10-2013
I guess the requestor's request has not yet been satisfied. You may want to try
Code:
awk     '                               {Arr[NR%5]=$0}
         /34_794862826912345.*succ/     {print Arr[(NR+1)%5]; L=NR+2}
         NR==L
        ' file
012/01/21 10:29:02  (111111)  Processing Job '34_794862826912345'
Job has 123 Docs 234 Pgs 456 Lines 4 Errors


Last edited by RudiC; 10-10-2013 at 02:39 PM.. Reason: pretty print
This User Gave Thanks to RudiC For This Post:
# 13  
Old 10-10-2013
Quote:
Originally Posted by RudiC
I guess the requestor's request has not yet been satisfied. You may want to try
Code:
awk     '                               {Arr[NR%5]=$0}
         /34_794862826912345.*succ/     {print Arr[(NR+1)%5]; L=NR+2}
         NR==L
        ' file
012/01/21 10:29:02  (111111)  Processing Job '34_794862826912345'
Job has 123 Docs 234 Pgs 456 Lines 4 Errors


worked perfectly...Smilie
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