You are not logged in.
Hi,
I am learning sed. And I cannot figure out what happened behind
sed 'x;H;D;' file
cat file
1
2
3
4
5
And it looks like
sed 'x;l;H;D;l;' file
and
sed 'x;l;H;D;' file
have the same output
$
1\n$
2\n1\n$
\n2\n1\n$
1\n\n\n2\n1\n$
2\n1\n\n1\n\n\n2\n1\n$
\n\n2\n1\n\n2\n1\n\n1\n\n\n2\n1\n$
1\n\n1\n\n\n2\n1\n\n\n\n2\n1\n\n2\n1\n\n1\n\n\n2\n1\n$
\n2\n1\n\n2\n1\n\n1\n\n\n2\n1\n\n1\n\n1\n\n\n2\n1\n\n\n\n2\n1\n\n2\n1\
\n\n1\n\n\n2\n1\n$
\n1\n\n\n2\n1\n\n\n\n2\n1\n\n2\n1\n\n1\n\n\n2\n1\n\n\n2\n1\n\n2\n1\n\
\n1\n\n\n2\n1\n\n1\n\n1\n\n\n2\n1\n\n\n\n2\n1\n\n2\n1\n\n1\n\n\n2\n1\
\n$
2\n1\n\n2\n1\n\n1\n\n\n2\n1\n\n1\n\n1\n\n\n2\n1\n\n\n\n2\n1\n\n2\n1\n\
\n1\n\n\n2\n1\n\n\n1\n\n\n2\n1\n\n\n\n2\n1\n\n2\n1\n\n1\n\n\n2\n1\n\n\
\n2\n1\n\n2\n1\n\n1\n\n\n2\n1\n\n1\n\n1\n\n\n2\n1\n\n\n\n2\n1\n\n2\n1\
\n\n1\n\n\n2\n1\n$
1\n\n\n2\n1\n\n\n\n2\n1\n\n2\n1\n\n1\n\n\n2\n1\n\n\n2\n1\n\n2\n1\n\n1\
\n\n\n2\n1\n\n1\n\n1\n\n\n2\n1\n\n\n\n2\n1\n\n2\n1\n\n1\n\n\n2\n1\n\n\
2\n1\n\n2\n1\n\n1\n\n\n2\n1\n\n1\n\n1\n\n\n2\n1\n\n\n\n2\n1\n\n2\n1\n\
\n1\n\n\n2\n1\n\n\n1\n\n\n2\n1\n\n\n\n2\n1\n\n2\n1\n\n1\n\n\n2\n1\n\n\
\n2\n1\n\n2\n1\n\n1\n\n\n2\n1\n\n1\n\n1\n\n\n2\n1\n\n\n\n2\n1\n\n2\n1\
\n\n1\n\n\n2\n1\n$
I know that
1)x command exchanges the pattern space and the hold buffer
2)H command appends the current pattern space into the hole buffer
3)D command deletes the first portion of the pattern space, up to the new line character, leaving the rest of the pattern alone.
But why the output is endless?
Offline
sed -n 'x;l;H' file
$
1\n$
2\n1\n$
3\n2\n1\n$
4\n3\n2\n1\n$
The D command deletes the first part (up to embedded newline) of a multi-line pattern space and resumes editing with first command in script.
Offline