To display 10 lines before and after the error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To display 10 lines before and after the error
# 1  
Old 04-17-2013
To display 10 lines before and after the error

Hi,

I have a huge log file and I wanted to check for the errors which happened on the particular time frame- Since its huge - vi is making difficult for me
So I used the below command to
Code:
grep -i 'ERROR' wls.log | grep 'Apr 8'

which showed there were few errors

Is there some way, we can also get the 10 lines before and after the error?
Thanks in advance!

Last edited by Franklin52; 04-18-2013 at 04:14 AM.. Reason: Please use code tags
# 2  
Old 04-17-2013
Some systems let you use -B with grep to get context, but odds are good you don't have that. you can use an awk script:

Code:
$ cat context.awk
# Recall N lines ago up to 19 lines
function last(N)
{
        if(N>L) return("");
        return(LINE[(L-N)%20]);
}

{ LINE[(++L)%20]=$0 } # Remember line for later

/Apr 8/ && /ERROR/  {
        for(N=10; N>=0; N--) print last(N);
        for(N=1; N<=9; N++) { getline ; print }
}

$ awk -f context.awk logfile

Use nawk on solaris.

Last edited by Corona688; 04-17-2013 at 04:12 PM..
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 04-17-2013
Thanks much!! worked perfectly!!
# 4  
Old 04-17-2013
It had a typo in it meaning you might have only got 9 previous lines instead of ten, fixed now... Close enough for government work anyway Smilie
# 5  
Old 04-18-2013
Try also this:
Code:
grep -i -C 10 'ERROR' wls.log

# 6  
Old 04-18-2013
Code:
grep -C 10  "pattern" filename


Last edited by Franklin52; 04-18-2013 at 08:58 AM.. Reason: Please use code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Display lines between timestamp

Hi Gurus, I have a software which logs event in the log file and it has become to big to search into it. I want to display all the lines from the log files between <Jul 21, 2016 3:30:37 PM BST> to <Jul 21, 2016 3:45:37 PM BST> that is 15 min data . Please help Use code tags, thanks. (10 Replies)
Discussion started by: guddu_12
10 Replies

2. Shell Programming and Scripting

Display 2 lines before and after a particular pattern

Hi team, Is it possible to display 2 lines after a particular pattern in a shell script. For example in a file which has the below contents. Mummy Daddy Son Daughter Children Aunty Uncle Grandma Grandpa Son Father Mother Brother-in-law I want to display 2 lines before and after... (1 Reply)
Discussion started by: balamv
1 Replies

3. Shell Programming and Scripting

To display last 5 lines of a file

Hi Guys, I want to echo last 5 lines of a file to a mail. My script getting continuously looped and not getting the output. can anyone help? #!/bin/bash read karthick; tail -5 $karthick; echo $karthick | mail -s "genius" someone@gmail.com Thanks NK (2 Replies)
Discussion started by: Karthick N
2 Replies

4. UNIX for Dummies Questions & Answers

How to display lines by range specified?

My input file is N3*123 ABC FLATS~ REF*D9*10000000001~ N3*223 ABC FLATS~ REF*D9*10000000002~ N3*323 ABC FLATS~ REF*D9*10000000003~ N3*423 ABC FLATS~ REF*D9*10000000004~ N3*523 ABC FLATS~ REF*D9*10000000005~ N3*623 ABC FLATS~ REF*D9*10000000006~ N3*723 ABC FLATS~ REF*D9*10000000007~ N3*823... (2 Replies)
Discussion started by: nsuresh316
2 Replies

5. UNIX for Dummies Questions & Answers

Display last few lines

I have huge text files and I only want to display on the screen the last lines. with less -G file.txt i get the beginning of the file. (2 Replies)
Discussion started by: FelipeAd
2 Replies

6. Shell Programming and Scripting

display 5 lines from the particular text

Hi All, please help me to display 5 continious lines from a particular text. my file is as below. file1.txt ------ Good 1 2 3 4 5 luck 1 2 3 I want to diplay 5 lines from the word Good. (4 Replies)
Discussion started by: little_wonder
4 Replies

7. Solaris

grep and display few lines before and after

Hi is there a way in grep to display few lines before and after the pattern?? I tried options A and B and after-context and before-context. But they don't work on Solaris platform. please advise. (13 Replies)
Discussion started by: melanie_pfefer
13 Replies

8. Shell Programming and Scripting

Display file without # lines

Hi to all in this great forum, im sure this has been asked lots of times before but ive been looking for the past day and cant find the answer. I use cat/some/file to display its contents but how can i get it to not display hashed out lines, or do i need another command, Thanks in advance:) (5 Replies)
Discussion started by: dave123
5 Replies

9. Shell Programming and Scripting

display all lines

Dear Experts, can any one tell me how to display all lines except the last line of a file using awk. take care Regards, SHARY (3 Replies)
Discussion started by: shary
3 Replies

10. Shell Programming and Scripting

display no of empty lines

I want to display the number of empty lines in a file. I guess i should use 'grep'...but how.. 10x for those who'll help. (5 Replies)
Discussion started by: atticus
5 Replies
Login or Register to Ask a Question