Print lines before and after..not grep -A


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print lines before and after..not grep -A
# 1  
Old 04-18-2011
Print lines before and after..not grep -A

Hi

I have this in my file

Code:
[01002] 2011-04-18 15:32:11 [Root]system-alert-00012: UDP flood! From xxxxxx to yyyyyyyyyy, int ethernet0/2). Occurred 1 times.
[01003] 2011-04-18 15:32:11 [Root]system-alert-00012: UDP flood! From xxxxxx to yyyyyyyyyy, int ethernet0/2). Occurred 1 times.
[01004] 2011-04-18 15:32:11 [Root]system-alert-00008: IP spoofing! From xxxxxx to yyyyyyyyyy, int redundant1). Occurred 1 times.
[01377] 2011-04-18 15:31:52 [Root]system-alert-00012: UDP flood! From xxxxxx to yyyyyyyyyy, int ethernet0/2). Occurred 1 times.
[01378] 2011-04-18 15:31:52 [Root]system-alert-00012: UDP flood! From xxxxxx to yyyyyyyyyy, int ethernet0/2). Occurred 1 times.
[01379] 2011-04-18 15:31:52 [Root]system-alert-00012: UDP flood! From xxxxxx to yyyyyyyyyy, int ethernet0/2). Occurred 1 times.
[01380] 2011-04-18 15:31:51 [Root]system-alert-00008: IP spoofing! From xxxxxx to yyyyyyyyyy, int redundant1). Occurred 1 times.

My goal is to find how long the UDP flood is. So how do I search for a line that has "UDP flood" but the line before or after it does not have "UDP Flood"? awk or grep is prefered.

Thanks

Last edited by pludi; 04-18-2011 at 06:44 AM..
# 2  
Old 04-18-2011
Code:
grep -An

print n lines After the line found by grep
Code:
grep -Bn

print n lines Before the line found by grep
Code:
grep -Cn

print n lines of Context from the line found by grep (n before and n after)
# 3  
Old 04-18-2011
I think you got it wrongly. If I use grep -An or -Bn or -Cn it will show all lines that have UDP flood. I do not want that. I want only lines that have UDP flood but the line before or after it must NOT have "UDP flood".
# 4  
Old 04-18-2011
Can you post how your output should look like from the given sample input..?
# 5  
Old 04-18-2011
Thanks for the reply. I guess my sample input should have more lines. So let us have more input lines:

[01575] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01576] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01577] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01578] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01579] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 2 times.
[01580] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01581] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 2 times.
[01582] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01583] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01584] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01585] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01586] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01587] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01588] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01589] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01590] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01591] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01592] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01593] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01670] 2011-04-18 15:31:13 [Root]system-alert-00008: IP spoofing! From xxxxx to yyyyy, int redundant1). Occurred 1 times.
[01594] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01595] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01596] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01597] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01598] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01599] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01600] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 2 times.
[01601] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01602] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01603] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 2 times.
[01604] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01605] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01606] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01607] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01608] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01609] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01610] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01670] 2011-04-18 15:31:13 [Root]system-alert-00008: IP spoofing! From xxxxx to yyyyy, int redundant1). Occurred 1 times.
[01611] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01612] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01613] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 2 times.
[01614] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01615] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01616] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01617] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01618] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01619] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01620] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01621] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01622] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01623] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01624] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01625] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01626] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01627] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01628] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01629] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01630] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 2 times.
[01631] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01632] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01633] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01634] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01635] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01636] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 2 times.
[01637] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01638] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01639] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01640] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01641] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01670] 2011-04-18 15:31:13 [Root]system-alert-00008: IP spoofing! From xxxxx to yyyyy, int redundant1). Occurred 1 times.
[01642] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01643] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01644] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01645] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01646] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01647] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01648] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01649] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01650] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01651] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01652] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01653] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
016703] 2011-04-18 15:31:13 [Root]system-alert-00008: IP spoofing! From xxxxx to yyyyy, int redundant1). Occurred 1 times.
[01654] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01655] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01656] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01657] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01658] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 2 times.
[01659] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01660] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01661] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01662] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 2 times.
[01663] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01664] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01665] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01666] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01667] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01668] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01669] 2011-04-18 15:31:15 [Root]system-alert-00008: IP spoofing! From xxxxx to yyyyy, int redundant1). Occurred 1 times.
[01670] 2011-04-18 15:31:13 [Root]system-alert-00008: IP spoofing! From xxxxx to yyyyy, int redundant1). Occurred 1 times.
[01671] 2011-04-18 15:31:11 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01672] 2011-04-18 15:31:11 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01673] 2011-04-18 15:31:11 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01674] 2011-04-18 15:31:11 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01675] 2011-04-18 15:31:11 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01676] 2011-04-18 15:31:11 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01677] 2011-04-18 15:31:11 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01678] 2011-04-18 15:31:11 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01679] 2011-04-18 15:31:11 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01680] 2011-04-18 15:31:11 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01681] 2011-04-18 15:31:11 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01682] 2011-04-18 15:31:11 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01683] 2011-04-18 15:31:11 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01684] 2011-04-18 15:31:11 [Root]system-alert-00008: IP spoofing! From xxxxx to yyyyy, int redundant1). Occurred 1 times.

The output should look like this:

[01670] 2011-04-18 15:31:13 [Root]system-alert-00008: IP spoofing! From xxxxx to yyyyy, int redundant1). Occurred 1 times.
[01594] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01670] 2011-04-18 15:31:13 [Root]system-alert-00008: IP spoofing! From xxxxx to yyyyy, int redundant1). Occurred 1 times.
[01611] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01670] 2011-04-18 15:31:13 [Root]system-alert-00008: IP spoofing! From xxxxx to yyyyy, int redundant1). Occurred 1 times.
[01642] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
016703] 2011-04-18 15:31:13 [Root]system-alert-00008: IP spoofing! From xxxxx to yyyyy, int redundant1). Occurred 1 times.
[01654] 2011-04-18 15:31:15 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01670] 2011-04-18 15:31:13 [Root]system-alert-00008: IP spoofing! From xxxxx to yyyyy, int redundant1). Occurred 1 times.
[01671] 2011-04-18 15:31:11 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01683] 2011-04-18 15:31:11 [Root]system-alert-00012: UDP flood! From xxxxx to yyyyy, int ethernet0/2). Occurred 1 times.
[01684] 2011-04-18 15:31:11 [Root]system-alert-00008: IP spoofing! From xxxxx to yyyyy, int redundant1). Occurred 1 times.

Thanks
# 6  
Old 04-18-2011
try this:
Code:
awk '/UDP flood/{flood=$0; if(line){print line ORS $0;line=""}} !/UDP flood/{line=$0; if(flood){print flood ORS $0;flood=""}}' file

# 7  
Old 04-18-2011
Sorry for misunderstanding you. Try this :
Code:
grep -v "UDP flood" -A1 filename | grep -vE '^--$'

This User Gave Thanks to Dahu For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print remaining lines using grep

Hi All, I am having a text file like below ERROR - Not a valid ID : 123 ERROR - Not a valid hello ID : 124 SUCCESS - Valid ID : 12 I need to display like below after reading the file if it finds the error keyword along with displaying this first line when error pattern... (10 Replies)
Discussion started by: rohit_shinez
10 Replies

2. Shell Programming and Scripting

How to print N number of lines before and after the grep?

Hi , My record file , need to print up to above (DATA array)(there may be n no lines ) , grep "myvalue" row now .....suggest me some options --- DATA Array--- record type xxxxx sequence type yyyyy 2 3---> data1 /dev/ --- DEVICE --- MAXIMUM_People= data_blocks= MY_value=2 xyz abc ... (0 Replies)
Discussion started by: Huvan
0 Replies

3. AIX

Grep a pattern and print following n lines

Hi all, I am struck with the below requirement. I need to grep a particular pattern in a file and then print next n lines of it for further processing. I have used the below code grep -A 3 "pattern" filename But it is throwing error as below. grep: illegal option -- A Can... (14 Replies)
Discussion started by: ssk250
14 Replies

4. Shell Programming and Scripting

Grep for the string and then print the next 4 lines

RHEL 5.8 I have a text file like below. I want to grep for a string and then print the next 4 lines including the line with the string I grepped for For eg: I want grep for the string HANS and then print the next 4 lines including HANS $ cat someText.txt JOHN NATIONALITY:... (7 Replies)
Discussion started by: omega3
7 Replies

5. Shell Programming and Scripting

Grep and print next 10 Lines separated by ,

Hi All, I need to grep through a file for a string and print the next ten lines to a file separating the lines with a , and save it as a csv file to open it as a XL file. The 10 lines should be on a sigle row in xl. Any suggesstions please. Note; I dont have a GNU Grep to use -A flag. ... (6 Replies)
Discussion started by: Nani369
6 Replies

6. Shell Programming and Scripting

Print the above and below lines for the grep pattern.

Hi, i would like to get the above and below lines of the grep pattern . For ex : file as below: chk1- aaaa 1-Nov chk2 -aaaa ########## chk1-bbbbbb 1-Nov chk2-bbbbbb ######### my search pattern is date : 1-Nov i need the o/p as below chk1- aaaa 1-Nov (6 Replies)
Discussion started by: expert
6 Replies

7. Shell Programming and Scripting

Print lines between two lines after grep for a text string

I have several very large file that are extracts from Oracle tables. These files are formatted in XML type syntax with multiple entries like: <ROW> some information more information </ROW> I want to grep for some words, then print all lines between <ROW> AND </ROW>. Can this be done with AWK?... (7 Replies)
Discussion started by: jbruce
7 Replies

8. 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

9. Shell Programming and Scripting

Print lines after grep

Hi all, I need help in following scenario. I have a file with about 10,000 lines. There are several lines which have word "START" (all upper case) in them. I want to grep line with word "START" and then do the following 1. Print the line number having word "START" 2. Print the next 11 lines. ... (4 Replies)
Discussion started by: jakSun8
4 Replies
Login or Register to Ask a Question