From: mrk on

>
> desc != 0 { print $0 }
> desc != 0 { print }
> desc != 0
> desc
>
> As awk statements, each of the above is equivalent due to awk's defaults.

In all above cases line will be printed?

From: Ed Morton on
On May 19, 11:19 am, mrk <no.ente...(a)hot.com> wrote:
> > desc != 0 { print $0 }
> > desc != 0 { print }
> > desc != 0
> > desc
>
> > As awk statements, each of the above is equivalent due to awk's defaults.
>
> In all above cases line will be printed?

It depends on the value of desc. In the first 3 cases the current
input line (record) will be printed as long as desc doesn't not have
the value of zero. In the 4th case the line will be printed as long as
the value of desc is neither zero nor the empty string.

So, if desc is zero, then in all 4 cases above the current input line
will NOT be printed but if desc is "", then the first 3 lines above
WILL result in the current input line being printed.

Ed.
From: mrk on
Jon LaBadie napisal w dniu 2010-05-19 16:34:
>
>
> desc != 0 { print $0 }
> desc != 0 { print }
> desc != 0
> desc
>
> As awk statements, each of the above is equivalent due to awk's defaults.

I'm afraid that it is to clever for me.
I don't see connection between this variable, also why variable is
placed beyond brackets {}. I've wrote variable inside {}.
Now I'm perform command awk with several different settings for practice
like:

awk '/error/a' file.txt - shows me all lines (no matter for awk that
pattern /error/ is set)

awk 'a' file.txt - shows nothing

awk '1' file.txt - shows me all lines

Could you describe me how it works with examples please.




From: Janis Papanagnou on
mrk wrote:
> Jon LaBadie napisal w dniu 2010-05-19 16:34:
>>
>>
>> desc != 0 { print $0 }
>> desc != 0 { print }
>> desc != 0
>> desc
>>
>> As awk statements, each of the above is equivalent due to awk's defaults.
>
> I'm afraid that it is to clever for me.
> I don't see connection between this variable, also why variable is
> placed beyond brackets {}. I've wrote variable inside {}.
> Now I'm perform command awk with several different settings for practice
> like:
>
> awk '/error/a' file.txt - shows me all lines (no matter for awk that
> pattern /error/ is set)

(Why would you want to write that?)

>
> awk 'a' file.txt - shows nothing

Variable a is 0, which equals 'false', thus no lines are printed.

>
> awk '1' file.txt - shows me all lines

Constant 1 is equivalent to 'true', thus all lines are printed.

>
> Could you describe me how it works with examples please.

You gave the examples already. But mostly just your second example makes
sense in some appropriate context where you have some other awk actions
besides the check against 'a'. Instead of '1' you would (more legible)
write '{print $0}'. Abstract examples are

awk '
condition { do_something_with_a_eg_set_or_unset_a }
a ## condition a equals "print if set"
'

awk '
condition { manupulate_the_line }
1 ## condition 1 equals "print the line"
'


Janis

>
>
>
>
From: mrk on
Janis Papanagnou napisal w dniu 2010-05-19 20:50:
> mrk wrote:
>> Jon LaBadie napisal w dniu 2010-05-19 16:34:
>>>
>>>
>>> desc != 0 { print $0 }
>>> desc != 0 { print }
>>> desc != 0
>>> desc
>>>
>>> As awk statements, each of the above is equivalent due to awk's defaults.
>>
>> I'm afraid that it is to clever for me.
>> I don't see connection between this variable, also why variable is
>> placed beyond brackets {}. I've wrote variable inside {}.
>> Now I'm perform command awk with several different settings for practice
>> like:
>>
>> awk '/error/a' file.txt - shows me all lines (no matter for awk that
>> pattern /error/ is set)
>
> (Why would you want to write that?)
>
>>
>> awk 'a' file.txt - shows nothing
>
> Variable a is 0, which equals 'false', thus no lines are printed.
>
>>
>> awk '1' file.txt - shows me all lines
>
> Constant 1 is equivalent to 'true', thus all lines are printed.
>
>>
>> Could you describe me how it works with examples please.
>
> You gave the examples already. But mostly just your second example makes
> sense in some appropriate context where you have some other awk actions
> besides the check against 'a'. Instead of '1' you would (more legible)
> write '{print $0}'. Abstract examples are
>
> awk '
> condition { do_something_with_a_eg_set_or_unset_a }
> a ## condition a equals "print if set"
> '
>
> awk '
> condition { manupulate_the_line }
> 1 ## condition 1 equals "print the line"
> '
>
>
> Janis
>
>>
>>
>>
>>
I think that I understand a little.
The key-word is "condition". Prewiously I couldn't tie between condition
and {program/commands}.
But it's become clear for me.

Everything after "'" is condition and after "{" is do if condition true.
For awk every true condition is 1 and false is 0. Now I understand (I
think) how it works.