Quote:
Originally Posted by
verge
You brought up a great point Corona, there are "." and " " and "-" in user entered fields that I don't want awk to use as a delimiter ... I haven't yet been able to figure out the syntax for including these characters in the FS statement.
Just add them, if they don't work, put a \\ in front of them. Easy enough to test them by
echo "a-b-c-d-e-f| awk 'BEGIN { FS="..." } { print $1,$2,$3; }'
Quote:
I'm experimenting so that I can understand HOW awk works
If you followed the logic for my example, you're already over the main hurdle, understanding that awk has its own built-in "while" loop to read in data record by record based on RS(usually newline) then split into tokens based on FS(usually whitespace). You can solve quite a few tasks by just by carefully adjusting its input and output settings.
But you can also cavalierly ignore them whenever you please and just deal with whole lines via $0, print arbitrary anything with
printf, read an extra line into $0 with
getline, etc.
Other mindblowers:
- N gives you the variable, $N gives you the record number. Say you did N=4; V=$N, that's effectively V=$4 This makes it easy to get the last field(NF is the number of fields, $NF is the last field), loop over fields for(N=1; N<=NF; N++) printf("%s\n", $N); etc. $0 is the entire record (usually line).
- You can actually modify the $1,$2,... special variables! And the value of $0 will change itself to match. And vice versa, so you can, say, do substitutions inside $0 and end up with different tokens. Or do one little change to $3 then print the entire modified line just by $3=toupper($3); print;
- Arrays can have strings as indexes, making it easy to do word counting. { for (N=1; N<=NF; N++) word[$N]++;} END { for(keys in words) printf("%s %d\n", key, words[key]); }
...and lots more. My own understanding of awk is far from complete.