You are not logged in.

#1 2014-05-21 08:16:29

yjcong
Member
Registered: 2009-07-06
Posts: 59

what happened behind "sed 'x;H;D' file"

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

#2 2014-05-26 09:41:16

zorro
Member
Registered: 2011-11-18
Posts: 47

Re: what happened behind "sed 'x;H;D' file"

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

Board footer

Powered by FluxBB