How to print few lines before and after matching word is found suing grep?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print few lines before and after matching word is found suing grep?
# 1  
Old 02-24-2014
How to print few lines before and after matching word is found suing grep?

Hi,

here are few lines present in the logs. I want to grep on Error and print few lines before and after Error word is found

line1
Line2
Line3
Error
Line4
Line5
Line6
Line7

I want the output to be

Line2
Line3
Error
Line5

How can i get this output?
# 2  
Old 02-24-2014
GNU grep:
Code:
grep Error -A 1 -B 2 file.log

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script example to Grep matching word from file

Hi expert, Need help in shell script. a.txt file output is below i would like to grep 3 line and 1st column value which is admin\22226 only and not full line. i only know admin word as 22226 can come anything with admin\ in file. also after fetching it i would like to use this... (1 Reply)
Discussion started by: kuljeetpal
1 Replies

2. UNIX for Dummies Questions & Answers

awk - Print lines if only matching key is found

I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. Thanks a lot. Any help is appreciated. Script I am using: awk 'FNR == NR && ! /^]*$/ {... (9 Replies)
Discussion started by: High-T
9 Replies

3. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

4. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

5. Shell Programming and Scripting

Search string in unix and print whole matching word

Hi I have requirement to search string starting with specific characters and print whole matching word in that string. example mystr="ATTRIBUTE NAME="Event Name" VALUE="Execute"" I want to search by passing "NAME=" and result should be NAME="Event Name". i am using below command but... (3 Replies)
Discussion started by: tmalik79
3 Replies

6. Shell Programming and Scripting

print next word after found pattern

Hi all, I'd like to print the next word after a found pattern. example text: word1 word2 word3 word4 pattern word5 pattern word1 word2 word3 word4 word1 word2 pattern word4 basiclly the word after pattern. Thanks (9 Replies)
Discussion started by: stinkefisch
9 Replies

7. Shell Programming and Scripting

Perl XML, find matching condition and grep lines and put the lines somewhere else

Hi, my xml files looks something like this <Instance Name="New York"> <Description></Description> <Instance Name="A"> <Description></Description> <PropertyValue Key="false" Name="Building A" /> </Instance> <Instance Name="B"> ... (4 Replies)
Discussion started by: tententen
4 Replies

8. Shell Programming and Scripting

How to Print from matching word to end using awk

Team, Could some one help me in Printing from matching word to end using awk For ex: Input: I am tester for now I am tester yesterday I am tester tomorrow O/p tester for now tester yesterday tester tomorrow i.e Starting from tester till end of sentence (5 Replies)
Discussion started by: mallak
5 Replies

9. Shell Programming and Scripting

AIX equivalent to GNU grep's -B and -A [print lines after or before matching lines]

Hi folks I am not allowed to install GNU grep on AIX. Here my code excerpt: grep_fatal () { /usr/sfw/bin/gegrep -B4 -A2 "FATAL|QUEUE|SIGHUP" } Howto the same on AIX based machine? from manual GNU grep ‘--after-context=num’ Print num lines of trailing context after... (4 Replies)
Discussion started by: slashdotweenie
4 Replies

10. UNIX for Dummies Questions & Answers

grep only word matching the pattern

Hi gurus, A file contains many words in format "ABC.XXXX.XXXX.X.GET.LOG" (X->varying). Now my shell script want this list (only words in formatABC.XXXX.XXXX.X.GET.LOG ) to continue the process. Pls help me. Thanks, Poova. (8 Replies)
Discussion started by: poova
8 Replies
Login or Register to Ask a Question