awk question
Quartz
quartz at sneakertech.com
Mon Oct 5 22:57:57 UTC 2015
>> It's not very much like sh or C syntax (or
>> any other syntax) and new users tend to get really confused.
>
> Hmmm... I don't know, could you provide an example where you
> would say, like, "this is not intuitive" or even "this does
> something totally strange"?
Things I've noticed new users bump into all the time:
Statements must be wrapped in curly braces, ie;
> awk '{print $1}{print $2}'
I think awk is one of the few languages to do this.
Because of the above, having to type:
> awk '{print $1}'
instead of just"
> awk 'print $1'
.. in other words both the quotes and the curly braces are required. For
most other shell utilities one is enough.
People assume that awk prints string literals like (ba)sh:
> echo "$1$2$3"
and
> awk '{print $1$2$3}'
both yield fields with nothing between them. So far so good, right? but:
> echo "$1,$2,$3"
yields results with commas between them, but:
> awk '{print $1,$2,$3}'
yields results with spaces. OK, so it's not like sh. Maybe it's like
Javascript then?
> awk '{print $1+","+$2+","+$3}'
... nope, now all they get is a huge list of mostly zeros, because awk
doesn't overload operators.
(Note: I am not advocating for overloaded operators and I think
Javascript is a horrible language).
> Yes, this is true, but keep in mind what awk is: a "pattern-directed
> scanning and processing language". If you want higher precision
> math, use system("<math stuff> | dc") and incorporate the result;
> awk isn't really for math, but integer math is usually fine. :-)
Right, but it's just something that makes people shy away from awk, for
better or worse.
More information about the freebsd-questions
mailing list