I think awk is a good thing under Linux, it's very handy to work with some text files. Under Linux, it's often the case that you deal with the shell, so the variables between awk and the shell are sometimes necessary, so simply summarize.
using variables from the shell in awk
one: "' $var '"
In this way, you don't have to change the habit of using the AWK program, which is commonly used by foreigners. such as:
var= "Test"
awk ' begin{print ' $var ' "} '
This notation is actually a constant that is double-parenthesis into parentheses, passed to awk.
If the var contains spaces, for the shell not to use the space as a separator, it should be used as follows:
Var= "This is a test"
awk ' Begin{print ' "$var" ' "} '
two: ' $var '
This is similar to the previous one. If the variable contains spaces, it becomes ' "" $var "" is more reliable.
Three: Export variable, use environ["var" form, get the value of environment variable
For example:
Var= "This is a test"; Export var;
awk ' Begin{print environ["var"]} '
Four: You can use the-v option of awk (if the number of variables is not many, the individual prefers this type of notation)
For example:
Var= "This is a test"
Awk-v awk_var= "$var" ' BEGIN {print Awk_var} '
This then passes the system variable var to the awk variable Awk_var.
awk passing values to shell variables
The idea of passing a variable from awk to the shell is simply to output several shell commands using awk (Sed/perl, and so on), and then use the shell to execute the commands.
Eval $ (awk ' begin{print ' var1= ' str1 '; var2= ' str2 ' "} ')
or eval $ (awk ' {printf ("var1=%s; var2=%s; var3=%s; ", $1,$2,$3)} ' Abc.txt)
Variables such as var1,var2 can then be used in the current shell.
echo "var1= $var 1-----var2= $var 2"
Resources:
Http://developer.51cto.com/art/200509/3781.htm
Http://www.ixpub.net/thread-1457438-1-1.html related Posts: awk invokes the shell and passes the variable to the IFS detail in the shell shell Bash script handles floating-point arithmetic and comparisons (using BC or AWK) the difference between an interactive shell and a non-interactive shell, a login shell, and a non-logon shell