Why does “1” in awk print the current line?

In awk, Since 1 always evaluates to true, it performs default operation {print $0}, hence prints the current line stored in $0 So, awk ‘$2==”no”{$3=”N/A”}1’ file is equivalent to and shorthand of awk ‘$2==”no”{$3=”N/A”} {print $0}’ file Again $0 is default argument to print, so you could also write awk ‘$2==”no”{$3=”N/A”} {print}’ file In-fact you … Read more

Assigning system command’s output to variable

Note: Coprocess is GNU awk specific. Anyway another alternative is using getline cmd = “strip “$1 while ( ( cmd | getline result ) > 0 ) { print result } close(cmd) Calling close(cmd) will prevent awk to throw this error after a number of calls : fatal: cannot open pipe `…’ (Too many open files)

Using AWK to Process Input from Multiple Files

awk ‘FNR==NR{a[$1]=$2 FS $3;next} here we handle the 1st input (file2). say, FS is space, we build an array(a) up, index is column1, value is column2 ” ” column3 the FNR==NR and next means, this part of codes work only for file2. you could man gawk check what are NR and FNR { print $0, … Read more

Can we use shell variables in awk?

Yes, you can use the shell variables inside awk. There are a bunch of ways of doing it, but my favorite is to define a variable with the -v flag: $ echo | awk -v my_var=4 ‘{print “My var is ” my_var}’ My var is 4 Just pass the environment variable as a parameter to … Read more