While perhaps the solution to your problem can easily be achieved by other means, the answer to your question is a simple one. sed
, by default, works a line at a time on 2 buffers - one persistent across line cycles called the h
old space and one refreshed at least once per cycle called the pattern space - and the latter is where all edits are performed.
Look ahead can be gained in one of two ways - you can save old lines and fall behind the line cycle in order to make better use of commands to swap and compare the buffers. This involves command primitives such as [hH]
old, [gG]
et, ex
change - which save to, copy from and swap out the hold buffer respectively - and lower case forms overwrite and uppercase forms append to their target buffer.
Or you can work future lines into a constant edit algorithm in which you consistently remove as many input lines as you read per cycle. This latter would be my preference here - especially because sed
makes it so very easy and efficient - especially with the N;P;D
commands.
Here's a demo using your example data:
sed '$!N;s/ime\(>\n<geo\)/ags\1/;P;D
' <<\IN
<time>20260664</time>
<tags>substancesummit ss</time>
<geo>asdsadsa</geo>
<time>20260664</time>
<tags>substancesummit ss</time>
<geo>asdsadsa</geo>
IN
N
ext, P
rint, and D
elete, like their lower-case counterparts n;p;d
get the next line of input, print, and delete into/from pattern space respectively. Unlike their lowercase counterparts (if a little less unlike in N
's case), these three work on newline boundaries rather than pattern space as a whole.
N
will append the next input line to pattern space following a \n
ewline character.
P
will print only up to the first occuring \n
ewline character in pattern space.
D
will delete only up to and including the first occurring \n
ewline in pattern space before quitting the script for the current cycle and queueing up the next with whatever remains in pattern space, or, if nothing remains following its delete action, with the next line awaiting on input as per usual.
These three can work together to expand sed
's edit window on a file very simply and efficiently - sed
slides through a file printing per cycle only the oldest from a series of lines it consistently deletes and replenishes according to a scripter's instructions - which leaves the sed
der in charge of the line cycle.
And a next line lookahead is easily expanded upon. If you wanted a 4-line pattern-space window throughout the script you could do:
sed -e '1{N;N' -e '};N;...;P;D'
...or, perhaps more usefully...
sed -e ':next
$!{/\(.*\n\)\{3\}/!{
N;b next' -e '}
};...cmds...;P;D'
...in which sed
only draws in an input line - and continues to do so until it has enough before executing any other commands - if there are fewer than three \n
ewline characters in pattern space and the current line is not the last. This occurs regardless of whatever the edits made by the subsequent commands might do.
\n
separates a line from the nextline but it is commonly known as newline character – miracle173 Dec 1 '14 at 1:00