Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

syntax explanation needed
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

Report this thread to moderator Post Follow-up to this message
Old Post
Robert Katz
12-25-04 08:56 PM


Re: syntax explanation needed
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

Report this thread to moderator Post Follow-up to this message
Old Post
Bob Harris
12-26-04 08:55 AM


Re: syntax explanation needed
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

Report this thread to moderator Post Follow-up to this message
Old Post
Robert Katz
12-26-04 08:55 AM


Re: syntax explanation needed
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

Report this thread to moderator Post Follow-up to this message
Old Post
Bob Harris
12-27-04 01:55 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 08:23 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.