Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Integer in AWK statement & access an array 1

Status
Not open for further replies.

moonring

Technical User
Feb 15, 2008
38
US
Hi everybody,

I've been looking up and down and I haven't found a satifactorily answer to this.
What is the meaning of a single number in an awk statement, as in this piece of code, which removes the commas from the lines of a file:
Code:
awk 'BEGIN{FS=","}{$1=$1; OFS=""}[red]1[/red]' file           # here number can be 1 or any other integer.
or as in this statement, which prints the whole file as is:
Code:
awk 1 file

So my question is more conceptual, regarding the presence of an integer in AWK. Is it a constant, or just a value that the shell needs, to evaluate to true the whole awk statement? How does it play out in AWK language as a whole? This is not clear to me !!
So can an expert shed some light on this ?


Secondly, when I try to access an array in the END statement of an AWK code, (which is built before the END statement), I have to change the subscript of the array to something else in order to print its values, as in this snippet:
Code:
awk '{ .....
..... array[$0]=$0 }
END { if ( k in array ) 
        print array[k]    # which works fine
                     # if I go : if ($0 in array ) print array[$0] --> doesn't work.
}
Any ideas why is that so?

Thanks for your time

P.S.
Does anybody know any good site, or any other explanatory material on this and other AWK tricks? The books that I've read so far don't cover them well ?

 
Hi

moonring said:
What is the meaning of a single number in an awk statement, as in this piece of code
man awk said:
AWK is a line-oriented language. The pattern comes first, and then the action. Action statements are enclosed in { and }. Either the pattern may be missing, or the action may be missing, but, of course, not both. If the pattern is missing, the action is executed for every single record of input. A missing action is equivalent to
{ print }
which prints the entire record.
So that single [tt]1[/tt] is the condition and always evaluates to true and determines the execution of the default action.
moonring said:
Does anybody know any good site, or any other explanatory material on this and other AWK tricks?
Well, personally I find the AWK forum on Tek-Tips the most helpful resource... ;-)


Feherke.
 
Hi

Oops, now I see you actually posted two questions. ( Please post only one question pert thread in the future. Keeps the discussion easier to read. )

The variables which refer to the currently processed record or the processed input file are undefined in the [tt]BEGIN[/tt] and [tt]END[/tt] blocks. Although you will find it working in many [tt]awk[/tt] implementations, you can not expect to happen the same way everywhere.

Feherke.
 

Thank You a lot Feherke,

feherke said:
So that single 1 is the condition and always evaluates to true and determines the execution of the default action.
Your explanation helped me clear out a very important concept, on which I've been struggling for a while now.

feherke said:
The variables which refer to the currently processed record or the processed input file are undefined in the BEGIN and END blocks.

Yep, that explains it very well.


feherke said:
Although you will find it working in many awk implementations, you can not expect to happen the same way everywhere.
I wished that implementation would have been more consistent.

feherke said:
Please post only one question pert thread in the future. Keeps the discussion easier to read.

I thought about it, but I didn't want to flood the forum with my postings, but I'll post once in the future.

Thanks again.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top