Home > Archive > AWK > December 2004 > syntax explanation needed
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 |
syntax explanation needed
|
|
| Robert Katz 2004-12-25, 3:56 pm |
| There have been many threads that required a re-arrangement of the input
record. The first proposal was '$1 = $1'
Followups pointed out the the line won't print if $1 = "" or $1 == 0, so
a fix to that was '{$1 = $1}1'
Now I can explain that, I said to myself; since } ends a line, the above
is equivalent to
' {$1 = $1}
1'
The first action line rearranges the input line, the second expression
is always true, so now we print the rearranged line.
Kenny countered the solution of '{$1 = $1}1' with '($1 = $1)1'. Now
that I'm not sure I understand.
What does 'expr expr expr' mean?
When is 'expr expr expr' the same as 'expr || expr || expr' ?
what does '($1 = $1)1' do? and why is it better than '{$1 = $1}1' ?
--
Regards,
---Robert
| |
| Bob Harris 2004-12-26, 3:55 am |
| In article <88izd.4994$n05.4427@news.cpqcorp.net>,
Robert Katz <katz@hp.com> wrote:
> There have been many threads that required a re-arrangement of the input
> record. The first proposal was '$1 = $1'
>
> Followups pointed out the the line won't print if $1 = "" or $1 == 0, so
> a fix to that was '{$1 = $1}1'
>
> Now I can explain that, I said to myself; since } ends a line, the above
> is equivalent to
>
> ' {$1 = $1}
> 1'
>
> The first action line rearranges the input line, the second expression
> is always true, so now we print the rearranged line.
>
> Kenny countered the solution of '{$1 = $1}1' with '($1 = $1)1'. Now
> that I'm not sure I understand.
>
> What does 'expr expr expr' mean?
> When is 'expr expr expr' the same as 'expr || expr || expr' ?
>
> what does '($1 = $1)1' do? and why is it better than '{$1 = $1}1' ?
This is a guess, but I think the (...)1 is just awk concatenation of
(...) and 1 so that the result of (...) is always non-zero. For example
echo 0 | awk '($1 = $1)'
will not print anything, but
echo 0 | awk '($1 = $1)1'
will print
0
Bob Harris
| |
| Robert Katz 2004-12-26, 3:55 am |
| Bob Harris wrote:
> In article <88izd.4994$n05.4427@news.cpqcorp.net>,
> Robert Katz <katz@hp.com> wrote:
>
>
>
>
> This is a guess, but I think the (...)1 is just awk concatenation of
> (...) and 1 so that the result of (...) is always non-zero. For example
>
> echo 0 | awk '($1 = $1)'
>
> will not print anything, but
>
> echo 0 | awk '($1 = $1)1'
>
> will print
>
> 0
>
> Bob Harris
Yup Bob,
According to
http://www.opengroup.org/onlinepubs...lities/awk.html
the result of
expr expr is string concatenation, but I don't quite understand how the
resulting string affects the input line. Are the two expr applied
sequentially from left to right?
--
Regards,
---Robert
| |
| Bob Harris 2004-12-26, 8:55 pm |
| In article <o2szd.4997$Pf5.1016@news.cpqcorp.net>,
Robert Katz <katz@hp.com> wrote:
> Bob Harris wrote:
> Yup Bob,
>
> According to
>
> http://www.opengroup.org/onlinepubs...lities/awk.html
>
> the result of
>
> expr expr is string concatenation, but I don't quite understand how the
> resulting string affects the input line. Are the two expr applied
> sequentially from left to right?
Starting with
pattern { action }
where the default action is "print $0" if the pattern is true
(non-zero, or non-null string).
So for most things
($1 = $1)
will be non-zero and/or a non-null string, so the default action of
"print $0" will occur.
However if the input is '0' or a null string, then nothing would be
printed.
But by concatenating 1 to the result of ($1 = $1), you are assured to
always have a non-zero or non-null result, so you would always print $0
Bob Harris
|
|
|
|
|