You're viewing a comment by Vadim Bogulean and its responses.

Vadim Bogulean Permalink
March 02, 2013, 09:15

Hi and thanks for your tips.
I was actually looking a way to print certain number of lines after searching a line in a file.

Based on the tips from 'Print lines using patters' I could elaborate one for my needs. Thought you could add it as well for others.

awk "{cnt-=1;if(cnt<=0)p=0;}/$1/{p=1;cnt=$2}p" $3

where $1, $2 $3 to be replaced with :
$1 - Your search pattern,
$2 - Number of lines to display (including searched pattern)
$3 - Your input file

$ awk "{cnt-=1;if(cnt<=0)p=0;}/Error:/{p=1;cnt=2}p" file

file content:
Error # 1
Warning # 2
Error # 3

Result would be:
Error # 1
Error # 3

Reply To This Comment

(why do I need your e-mail?)

(Your twitter handle, if you have one.)

Type the word "network_100": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.