Awk Traps

by Geethalakshmi 2010-09-17 13:06:31

Awk Traps


Accustomed awk users should take special note of the following:

* Semicolons are required after all simple statements in perl (except at the end of a block). Newline is not a statement delimiter.
* Curly brackets are required on ifs and whiles.
* Variables begin with `$' or `@' in perl.
* Arrays index from 0 unless you set `$['. Likewise string positions in substr() and index().
* You have to decide whether your array has numeric or string indices.
* Associative array values do not spring into existence upon mere reference.
* You have to decide whether you want to use string or numeric comparisons.
* Reading an input line does not split it for you. You get to split it yourself to an array. And the split operator has different arguments.
* The current input line is normally in `$_', not `$0'. It generally does not have the newline stripped. (`$0' is the name of the program executed.)
* `$' does not refer to fields--it refers to substrings matched by the last match pattern.
* The print statement does not add field and record separators unless you set `$,' and `$\'.
* You must open your files before you print to them.
* The range operator is `..', not comma. (The comma operator works as in C.)
* The match operator is `=~', not `~'. (`~' is the one's complement operator, as in C.)
* The exponentiation operator is `**', not `^'. (`^' is the XOR operator, as in C.)
* The concatenation operator is `.', not the null string. (Using the null string would render `/pat/ /pat/' unparsable, since the third slash would be interpreted as a division operator--the tokener is in fact slightly context sensitive for operators like `/', `?', and `<'. And in fact, `.' itself can be the beginning of a number.)
* next, exit and continue work differently.
* The following variables work differently

Awk Perl
ARGC $#ARGV
ARGV[0] $0
FILENAME $ARGV
FNR $. - something
FS (whatever you like)
NF $#Fld, or some such
NR $.
OFMT $#
OFS $,
ORS $\
RLENGTH length($&)
RS $/
RSTART length($`)
SUBSEP $;

Tagged in:

906
like
0
dislike
0
mail
flag

You must LOGIN to add comments