Integer division in awk

Use the int function to get the integer part of the result, truncated toward 0. This produces the nearest integer to the result, located between the result and 0. For example, int(3/2) is 1, int(-3/2) is -1. Source: The AWK Manual – Numeric Functions

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)

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)