Sed: H and D behavior
My sed script:
# script.sed
1,3H
1,3g
3D
When I run it, I get this:
$ seq 5 | sed -f script.sed
1
1
2
4
5
However, this seems wrong to me. On line 3, after executing the D command, the template space has
1 2 3
When the cycle restarts, H should set a hold:
<empty_line>
1
2
3
1
2
3
Then g should set the template space to the same content. Then D will remove the first (empty) line. Each time the cycle is restarted, the hold space will effectively double. Hence, this should result in an endless loop.
What am I missing?
+3
source to share
1 answer
Below, I'll show you how to interpret the expected execution by displaying as an ordered pair the result of a command with a template space and a holding 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 combine it into an echo command with an option -e
, I get:
$ echo -e '\n1\n\n1\n2\n4\n5\n'
1
1
2
4
5
+1
source to share