I'm new to awk and need some help. <br>
<br>
When a string is found, using awk, how do you read and display the next 3 lines of the file if the search string is not present?<br>
<br>
Thanks!!!!
I found the answer, in case anyone else has hit the same stumbling block, here it is. <br>
This script will print the line in which the string is found and 2 lines after. Variables can be adjusted to print n # of lines. My search was for the first field only, however this can also be adjusted. Have fun and God bless.<br>
<br>
awk '<br>
BEGIN {<br>
flag = 0;<br>
count = 0;<br>
limit = 3<br>
}<br>
{<br>
if (( flag == 0 ) && ( $1 == "string" ))<br>
#<br>
flag=1<br>
#<br>
if (flag > 0)<br>
{<br>
++count<br>
print<br>
}<br>
if ( count >= limit )<br>
{<br>
flag=0<br>
count=0<br>
}<br>
} ' filename
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.