From: Ed Morton on
On 5/19/2010 3:18 PM, mrk wrote:
> 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.

It's very simple: awk scripts like:

awk 'script' file

are

condition { action }

statements. It's not that every true condition is "1", but rather every "1" is a
true condition. "27" is also a true condition, but not every true condition is "27".

Ed.