bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: sed vs. awk


From: Micah Cowan
Subject: Re: sed vs. awk
Date: Wed, 30 Jan 2008 18:53:04 -0800
User-agent: Thunderbird 2.0.0.6 (X11/20071022)

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Bruno Haible wrote:
> Paul Eggert wrote:
>>>   - Also for maintainability: Use simple grep and simple sed where possible.
>>>     I claim that many people know sed's s/.../.../ command syntax by heart
>>>     while the use of braces is outside their 'sed' vocabulary.
>> I dunno; that sort of usage is quite common, and has been common since
>> the 1970s.
>>
>> One advantage of using just "sed" rather than a pipeline is that it's
>> easier to detect failures by examining sed's exit status.
> 
> Well, if complex sed expressions are so common, and if you find that large
> sed expressions are not hampering readability, then you can certainly tell
> what the following sed expression does :-)
> 
> sed_riddle="
> /[/][/*]/{
>   ta
>   :a

<snip>

Note that he said that _braces_ containing multiple commands were a
common usage, and not that "complex sed expressions" including branches,
etc were.

Though, I'd tend to side with you on this; the vast majority of sed
expressions I've seen lacked branches, braces, file reads, and hold
buffer manipulations.

> Hmm. It's true that 'awk' can do many more things.

I guess that depends on how you define "many". With a single
input/output, sed can do everything awk can do (barring the more exotic
GNU extensions, such as network support). It just takes a... _lot_...
more effort. ;)

> Another thing I don't like about 'awk' is its different syntax for regular
> expressions. 'sed' and 'grep' accept BREs by default, 'awk' doesn't. This
> cannot help maintainability.

I don't think I've ever heard before, an argument that BREs make for
better maintainability. I find them far less readable (and powerful, of
course, though GNU extensions help there) than their ERE cousins.

And while folks who are used to sed and grep may find EREs unfamiliar,
IME I've encountered far more users these days who are used to Perl
regexes than folks who are handy with sed and grep; and EREs are much
more accessible than BREs, to folks used to PCREs.

That being said, I'm not sure I agree entirely with Paul's statement
that awk has become nearly as portable as sed. Traditional awk; has
lacked support for intervals ({X,Y}), whereas POSIX has added that; I
run across awks that still lack intervals all the time (in fact, unless
I'm mistaken, Ubuntu ships with one by default).

My two cents would be to use whichever one seems to suit. Simple
substitutions, are much simpler in sed; match-and-deletes are roughly
equal in simplicity (though the sed will still be shorter); much beyond
that, and awk rapidly rises in attractiveness. :)

And I do think that multiple pipelines may be a more readable solution
than some of the same solutions in a single sed statement.

- --
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

iD8DBQFHoTgP7M8hyUobTrERAmFtAJ95hrpgCASUWfGa3BORKNTUhqeEiwCfYpeI
ZJ4XCkEJ57QzlgVaMTcHxKI=
=hdNi
-----END PGP SIGNATURE-----




reply via email to

[Prev in Thread] Current Thread [Next in Thread]