Unix Shell - Re: SED question: entering a blank line above a character searched

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2007 > Re: SED question: entering a blank line above a character searched





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Re: SED question: entering a blank line above a character searched
Ed Morton

2007-12-28, 7:23 pm



On 12/28/2007 1:23 PM, Icarus Sparry wrote:
> On Fri, 28 Dec 2007 11:23:11 -0600, Ed Morton wrote:
>
>
>
>
> OK, so lets look at the two programs.
>
> awk
>
> /:/{print ""}
> 1
>
> This requires you to know the idiom '1' to trigger the default action,
> which is not obvious.


Right, but since the program is so small it's the perfect opportunity to learn
it and once you understanfd that small program it's so easy to do so much more
in future.

So a more reasonable program to use would be
>
> /:/{print ""}
> {print}
>
> Contrast this with the sed program
>
> /:/i\
>
>
> They are both 2 lines long, they take about the same amount of time to
> run. Personally I find the sed program "when you see a ':' insert a blank
> line" more readable than the awk program "when you see a ':' print a
> blank line. For every line, print it". Using the GNU versions of awk &
> sed, we see that awk is 5 times the size.


It doesn't matter. The awk program introduces a style that's easy to build on in
future as your requirements evolve. The sed one's just something that isn't too
hard to do IN THIS PARTICULAR case with sed, but if you know how to do it in awk
then it's a waste of time learning this particular sed-ism.

> So apart from Ed's dogma "In general sed should only be used for simple
> substitutions", there is no reason in this case to use awk rather than
> sed.


It's a "for the future" thing.

> Both awk and sed are useful tools.


Absolutely. Just pick the right one for the right job.

> If you can only learn one tool then
> learn PERL (or Python or ruby or whatever the latest scripting language
> flavour of the month is).


I disagree. I think there may be jobs/environments where PERL or ruby are useful
but it seems like sed and awk are more readily available and easier to understand.

> If you can learn more than one, then learn both
> sed and awk and use them at the correct times.


That I agree with.

Ed.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com