Why do awk command line vars behave the way they do?
This came up a little in another thread. Can someone explain some why awk (I happen to use gawk) behaves as follows:
I understand this is "how it is". And it is clear to me what is happening. I'm interested in knowing "why". Any practical example to show the advantage or benefit of why it's set up like this?
The gawk man page says:
Quote:
Command line variable assignment is most useful for dynamically
assigning values to the variables AWK uses to control how
input is broken into fields and records. It is also useful for
controlling state if multiple passes are needed over a single data file.
That's a little helpful, but not much. And I have not previously used "multiple passes", so maybe I'm missing something there.
Well, not being an awk guru, I'd like to comment on some of your questions, certainly not exhaustively:
Quote:
$ awk 'BEGIN {print x}' x=1
[Blank line]
The BEGIN section is executed before any parameters are evaluated, so x is undefined when printed.
Quote:
$ awk x=1 'BEGIN {print x}'
awk: fatal: cannot open file `BEGIN {print x}' for reading (No such file or directory)
Missing the -v option, awk takes x=1 to be the first parameter, expecting it to be the awk program. The second is the file to be opened and worked upon, unless it is a variable assignment. Var assignments are executed (and valid only for the files to follow) in the order they appear on the command line.
This behaviour would also explain all your other examples.
The VAR=1 way of setting variables is the old-fashioned (and arguably more portable) way of doing so. I also think it's more easily readable, so try and do so when I can, but you've discovered its one disadvantage -- BEGIN runs first.
If you wish to set variables before any code runs, you can do this:
However, the old way of setting variables has one advantage -- you can set variables on the fly. You can read a file, change a var, read a file, change a var, change a var...
So for file1, it'd add $1 * 1, for file2 it'd add $1*2, and so forth.
Corona688's final example can be extended to include an assignment after the final file. Such an assignment will take effect after the last line of the last file but before entering the END section. If that last (or only) file is a pipe or stdin, use the filename -.
Thanks for the responses. It doesn't really answer my question as to "why" it's set up this particular way. But I'm perfectly willing to accept that's "the way it is", and make best use of it.
Continuing that thought, from the man page, related to setting variables on the command line:
Quote:
Command line variable assignment is most useful for dynamically
assigning values to the variables AWK uses to control how input
is broken into fields and records. It is also useful for controlling
state if multiple passes are needed over a single data file.
The first sentence is obviously true. For the second sentence, does anyone have experience doing multiple passes over a single data file? It sounds maybe useful, but I don't remember seeing multiple passes before, and using command line variables to control state for multiple passes.
I've demonstrated how it works, and demonstrated that it has some abilities that the more straightforward way doesn't; what more do you want? What it's "for" is whatever you want to do with it, it's just a feature.
I've done multiple passes on the same file on occasion for some difficult comparisons or sorting. Standard deviations is another one where two passes is helpful, you get the average and count on the first pass then use those to get the deviation on the second pass.
As per requirement if column 2 is NULL then 'N' ELSE 'Y'.
I have written below awk code. But it is not replacing values for first line. :confused:
cat temp.txt
1|abc|3
1||4
1|11|c
awk -F'|' '{if($2==""){$2="N"}else{$2="Y"} print $0 } {OFS="|"} ' < temp.txt
1 Y 3 ... (4 Replies)
Hi,
I am trying to pass awk field to a command line executed within awk (need to convert a timestamp into formatted date).
All my attempts failed this far.
Here's an example.
It works fine with timestamp hard-codded into the command
echo "1381653229 something" |awk 'BEGIN{cmd="date -d... (4 Replies)
My input file gfile values is CTRY=GM&PROJTYPE=SP&PROJECTTYPE=Small+Project
If i am giving PROJECTTYPE then it must give Small Project
awk -F"&" '{for (i=1; i<=NF; i++) if ($i ~ "^"PAT) {sub ("^"PAT"=", "", $i); sed 's/'+'/""/' $i ; print $i }}' PAT=$1 ... (6 Replies)
Hi all,
As of now am using an awk command to check the number of columns in a file that has 10 lakh rows.
Is it possible to remove that particular line having an extra column and copy the remaining lines to a new file ?
YOUR HELP IS HIGHLY APPRECIATED. THANKS IN ADVANCE (5 Replies)
Hi, Im trying to get current line in the AGREP command I use in AWK. My script looks like this:
list.txt
car
bus
checklist.txt
cer
buss
cat list.txt | awk -v mycmd="$(agrep -2 -i $0 checklist.txt)" '{print $mycmd}'
It doesnt work. How can I get the current line in the $0... (6 Replies)
hi,
The following mail cmd executed successfully.
mailx -s 'subject' user@company.com < testfile.dat
However When i include this mail cmd in shell script it behave odd. Getting an error message mailx comand not found. (2 Replies)
Need some help with the following Bourne Shell script. The script is only looping one time and then stops. The script should loop as many times as there are entries in the input file = $FILE_LIST line by line. The file has the path to a source file and the destination directory where the file... (1 Reply)
Hi all
How do I use awk such that it does not require an input file? I have a situation where I need to process some shell vars within awk (passed into awk with "-v VAR1=$VALUE1, VAR2=$VALUE2" etc), but that processing does not require/use an input file.
Any guidance?
TIA
JG (2 Replies)
Hi ,
I have a piece of code ...wherein I need to assign the following ...
1) A command line argument to a variable
e.g origCount=ARGV
2) A unix command to a variable
e.g result=`wc -l testFile.txt`
in my awk shell script
When I do this :
print "origCount" origCount --> I get the... (0 Replies)
Hello,
presently, I'm calling nawk from my main script and have nawk defined in one file. So I call nawk like this
nawk -f file input
This file defines how to separate mails in /var/mail/$user and show 1 at a time. However, I would also like to do other actions (delete message, forward... (9 Replies)