euphoria83
euphoria83

Reputation: 15136

sed: behavior of H and D

My sed script is this:

# script.sed
1,3H
1,3g
3D

When I run it, I get the following:

$ seq 5 | sed -f script.sed

1

1
2
4
5

However, this seems wrong to me. On line 3, once the D command is executed, the pattern space has

1
2
3

When the cycle is restarted, H should set the hold space to:

<empty_line>
1
2
3
1
2
3

Then, g should set the pattern space to the same content. D will then remove the first (empty) line. Every time the cycle is restarted, the hold space will effectively double. Hence, this should lead to an infinite loop.

What am I missing?

Upvotes: 3

Views: 158

Answers (1)

Nil Rift
Nil Rift

Reputation: 26

Below, I show how I interpret the expected execution, showing as an ordered pair the result of the command, with the pattern space first and the hold space following:

1: H(1,\n1)  g(\n1,\n1)                                > \n1\n
2: H(2,\n1\n2) g(\n1\n2,\n1\n2)                        > \n1\n2\n
3: H(3,\n1\n2\n3) g(\n1\n2\n3,\n1\n2\n3) D(,\n1\n2\n3) >
4:                                                     > 4\n
5:                                                     > 5\n

If I take the output of this interpretation and concatenate it into an echo command with the -e option, I get:

$ echo -e '\n1\n\n1\n2\n4\n5\n'

1

1
2
4
5

Upvotes: 1

Related Questions