| Icarus Sparry 2007-12-28, 1:26 pm |
| On Fri, 28 Dec 2007 11:23:11 -0600, Ed Morton wrote:
> On 12/28/2007 1:01 AM, Kompu Kid wrote:
>
> Appologies if there's some reason you NEED to use sed, but often people
> request a solution with one tool because they don't know the
> alternatives. In this case:
>
> awk '/:/{print ""}1' file
>
> In general sed should only be used for simple substitutions.
>
> Ed.
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. 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.
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.
Both awk and sed are useful tools. 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). If you can learn more than one, then learn both
sed and awk and use them at the correct times.
|