Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Matching line if next line doesn't have pattern 1

Status
Not open for further replies.

cryptoadm

MIS
Nov 6, 2008
71
0
0
US
Doing host1
Doing host2
Doing host3
host3:grep
Doing host4
host4:grep
Doing host5
Doing host6
host6:grep

The file is similar to the above and I know how to match say "Doing" and print the next line, but am unsure how to match "Doing" and print that line if the next line is also "Doing" and doesn't have "grep".

Example:
Doing host1
Doing host2
Doing host5
 
Here is an awk script which does what you want for the supplied example file.
Code:
BEGIN {state=0}
{
 if ( $1 == "Doing" ) {
    if (state == 1) { print tmp; tmp=$0 }
       else { state=1; tmp=$0 }
 } else { state = 0 }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top