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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I replace pattern on 3 consecutive lines

Status
Not open for further replies.

Jack12

Technical User
Nov 24, 2004
2
IN
I need to replace this pattern on 3 consecutive lines with a blank. These line are consecutive and occure ramdomly at dt different places throughout the file.

]
?please~0.001)
([
 
Quick and easy in sed would be

sed '
/^\]/{
$!N
/\n?please~0.001)/{
$!N
/.*\n(\[/s/.*//
}
}' < input

and harder in awk

awk 'BEGIN{
L1=&quot;]&quot;
L2=&quot;?please~0.001)&quot;
L3=&quot;([&quot;
}
{
a[0]=a[1]; a[1]=a[2]; a[2]=$0
if( $1 == L3 && a[0] == L1 && a[1] == L2 )
printf(&quot;\n&quot;)
else if( $1 != L1 && $1 != L2 && $1 != L3 ){
if( a[0] == L1 && a[1] == L2 )
printf(&quot;%s\n%s\n%s\n&quot;,a[0],a[1], $0)
else if( a[1] == L1 )
printf(&quot;%s\n%s\n&quot;,a[1], $0)
else
print
}
}
END{
if( a[2] == L1 ) printf(&quot;%s\n&quot;, a[2])
else if( a[1] == L1 && a[2]==L2 ) printf(&quot;%s\n%s\n&quot;, a[1],a[2])
}' < input

The awk example is harder because I assume to keep these strings when they are out of order/sequence in instances of lines fewer then 3 and at the end of the file - common sources of error in awk scripts. Similar is the $!N in the sed script. Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top