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

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

Re: sed vs. awk


From: Bruno Haible
Subject: Re: sed vs. awk
Date: Thu, 31 Jan 2008 03:31:10 +0100
User-agent: KMail/1.5.4

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
  
s,^\\(\\([^\"'/]\\|\"\\([^\\\"]\\|[\\].\\)*\"\\|'\\([^\\']\\|[\\].\\)*'\\|[/][^\"'/*]\\|[/]\"\\([^\\\"]\\|[\\].\\)*\"\\|[/]'\\([^\\']\\|[\\].\\)*'\\)*\\)//.*,\\1,
  te
  
s,^\\(\\([^\"'/]\\|\"\\([^\\\"]\\|[\\].\\)*\"\\|'\\([^\\']\\|[\\].\\)*'\\|[/][^\"'/*]\\|[/]\"\\([^\\\"]\\|[\\].\\)*\"\\|[/]'\\([^\\']\\|[\\].\\)*'\\)*\\)/[*]\\([^*]\\|[*][^/*]\\)*[*][*]*/,\\1
 ,
  ta
  
/^\\([^\"'/]\\|\"\\([^\\\"]\\|[\\].\\)*\"\\|'\\([^\\']\\|[\\].\\)*'\\|[/][^\"'/*]\\|[/]\"\\([^\\\"]\\|[\\].\\)*\"\\|[/]'\\([^\\']\\|[\\].\\)*'\\)*[/][*]/{
    
s,^\\(\\([^\"'/]\\|\"\\([^\\\"]\\|[\\].\\)*\"\\|'\\([^\\']\\|[\\].\\)*'\\|[/][^\"'/*]\\|[/]\"\\([^\\\"]\\|[\\].\\)*\"\\|[/]'\\([^\\']\\|[\\].\\)*'\\)*\\)/[*].*,\\1
 ,
    tu
    :u
    n
    s,^\\([^*]\\|[*][^/*]\\)*[*][*]*/,,
    tv
    s,^.*\$,,
    bu
    :v
  }
  :e
}"


> That being said, I'd prefer to see less use of "sed" and more of
> "awk".  Typically "awk" programs are more readable and maintainable
> than "sed" programs are.

Hmm. It's true that 'awk' can do many more things. 'sed' is to 'awk' like a
pocket knife compared to a toolbox. That's actually a reason to use 'sed'
sometimes: When I'm sure that I don't need field splitting or other fancy
'awk' features, I might prefer the simple knife.

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.

Bruno





reply via email to

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