OT: sed/awk pointers needed

Brandon Allbery allbery.b at gmail.com
Tue Sep 30 09:00:41 PDT 2014


On Tue, Sep 30, 2014 at 11:44 AM, Craig Treleaven <ctreleaven at macports.org>
wrote:

> I have only rudimentary acquaintance with sed and less with awk.  I'd like
> to learn more and this seemed like an opportunity to do so. From some
> reading [1], I think sed's Hold buffer is the way to extract the port name
> and insert it onto the line with each of the variants.  But the tutorial
> only uses the hold buffer for full lines; not just, say, the first word of
> the line.  Is this possible?  How does one do that?


Very, very painfully. I think I could do it, with a LOT of fiddling, but
would very much prefer to use a more appropriate tool. awk is somewhat
better, but I'd reach for perl/python/ruby first.

Very roughly, the trick is to chop the line down to the first word, save it
to the hold buffer, then on subsequent lines append hold space to pattern
space, swap the line around the embedded newline that gets inserted before
the appendage, then replace that newline with a space. Use branch testing
to detect the difference between the two kinds of lines:

    s/^  //
    t dataline
    s/ .*$//
    h
    b
    : dataline
    G
    s/^\(.*\)\n\(.*\)$/\2 \1/
    # now starts with the package name and a space; put the rest of the
processing here...

You can see how painful it gets. (Also the above is untested.) Better to
use a real language.

-- 
brandon s allbery kf8nh                               sine nomine associates
allbery.b at gmail.com                                  ballbery at sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/macports-dev/attachments/20140930/a25554ba/attachment.html>


More information about the macports-dev mailing list