With sed
:
sed '$!N;/remove/!P;D' infile
This pulls the N
ext line into pattern space (if not !
on la$
t line) and checks if pattern space matches remove
. If it doesn't (means none of the two lines in the pattern space contains the string remove
) it P
rints up to the first \n
ewline character (i.e. it prints the first line). Then it D
eletes up to the first \n
ewline character and restarts the cycle. This way, there are never more than two lines in the pattern space.
It's probably easier to understand the N
,P
,D
cycle if you add l
before and after the N
to look at the pattern space:
sed 'l;$!N;l;/remove/!P;D' infile
so, using only the last six lines from your example:
8AC3
remove
8AE4
8AE5
8AE6
remove
the last command outputs:
8AC3$
8AC3\n remove$
remove$
remove\n 8AE4$
8AE4$
8AE4\n 8AE5$
8AE4
8AE5$
8AE5\n 8AE6$
8AE5
8AE6$
8AE6\n remove$
remove$
remove$
Here is a short explanation:
cmd output cmd
l 8AC3$ N # read in the next line
l 8AC3\n remove$ D # delete up to \n (pattern space matches so no P)
l remove$ N # read in the next line
l remove\n 8AE4$ D # delete up to \n (pattern space matches so no P)
l 8AE4$ N # read in the next line
l 8AE4\n 8AE5$ # pattern space doesn't match so print up to \n
P 8AE4 D # delete up to \n
l 8AE5$ N # read in the next line
l 8AE5\n 8AE6$ # pattern space doesn't match so print up to \n
P 8AE5 D # delete up to \n
l 8AE6$ N # read in the next line
l 8AE6\n remove$ D # delete up to \n (pattern space matches so no P)
l remove$ # last line so no N
l remove$ D # delete (pattern space matches so no P)