Operating System - HP-UX
1838383 Members
3825 Online
110125 Solutions
New Discussion

Re: SED Delete multiple sets of 2 Lines in stacked file

 
Tim Wilk
New Member

SED Delete multiple sets of 2 Lines in stacked file

Looking for a way to delete 2 lines that start with 2 values (header and footers), for example, a merged/stacked file w/ 1000 lines, and randomly in the file there will be 2 lines, back to back, that start with 01 (header) and 99 (footer).

01 LINE (keep this header)
SOME LINE
SOME LINE
SOME LINE
99 LINE (delete this footer)
01 LINE (delete this header)
SOME LINE
SOME LINE
SOME LINE
SOME LINE
99 LINE (delete this footer)
01 LINE (delete this header)
SOME LINE
SOME LINE
99 LINE (delete this footer)
01 LINE (delete this header)
SOME LINE
99 LINE (keep this footer)

Results would have just 1 01 and 99 line.
4 REPLIES 4
Steven E. Protter
Exalted Contributor

Re: SED Delete multiple sets of 2 Lines in stacked file

Shalom,

http://www.unix.com/shell-programming-scripting/24126-how-change-file-sed.html

You may need to take multiple passes on the file to do this.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
James R. Ferguson
Acclaimed Contributor

Re: SED Delete multiple sets of 2 Lines in stacked file

Hi Tim:

Using your actual, posted sample:

# perl -ne 'print if eof;print unless /^99 LINE/../^01 LINE/' file

Regards!

...JRF...
Jdamian
Respected Contributor

Re: SED Delete multiple sets of 2 Lines in stacked file

Try awk code (I assumed that the line "01 LINE" always is the next line a "09 LINE" but first and last lines) :

cat file | awk '
/^99 LINE/ {
OLDLINE=$0;
if (!getline) {
print OLDLINE
exit
}
}

{ print $0 }
'
Dennis Handly
Acclaimed Contributor

Re: SED Delete multiple sets of 2 Lines in stacked file

Cleaning up Oscar's awk script and removing assumptions and cat:
awk '
BEGIN { getline; print $0 }
/01 LINE/ { next }
/99 LINE/ { save=$0; next }
{
print $0
}
END { print save } ' file