[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Strange sed behavior
From: |
Micah Cowan |
Subject: |
Re: Strange sed behavior |
Date: |
Thu, 18 Oct 2007 15:16:27 -0700 |
User-agent: |
Thunderbird 1.5.0.13 (X11/20070824) |
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
Stephane CHAZELAS wrote:
> 2007-10-18, 14:47(+02), David Kastrup:
>> Can anybody explain to me what is happening here? I am a bit befuddled.
>>
>> $ { echo 1;echo 2;}|sed 'H;${x;P;D;}'
> [...]
>
> Your code is doing
>
> $h = $p = ""
>
> while ($p = readline()) {
> START:
> $h = $h . "\n" . $p; # H
> if (lastline) {
> ($h, $p) = ($p, $h); # x
> print firstlineof($p); # P
> removefirstlineof($p); if ($p != "") goto START; # D
> }
> }
I did _not_ know that D will not result in appending the next input
line, and instead just use the current pattern buffer unless it's empty
(not a "normal cycle"). GNU's docs are great at explaining this;
unfortunately, POSIX seems not at all clear on that point. Maybe I
should issue a defect report on that, if there isn't one...
- --
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFHF9s77M8hyUobTrERCCkfAJ9meF8hcdIpUU88Rnxbg0xmW9qnZACfXie/
9TuLqo7Zxour+l+fgmtXNwc=
=K3E8
-----END PGP SIGNATURE-----