Variable [reprint] how to use awk in shell or assignment of

How to use a variable assignment or in the shell awk

http://blog.jcix.top/2017-03-30/shell_awk_variables/

 

Write shell scripts when processing text, often used to match the shell awk command. But in braces awk and shell looks like two worlds. This paper describes only the easiest way to understand (the author is limited, complex may later supplement), to achieve the use of awk and shell variables change.

If we become as a function of the awk language, or a package, then you want to use or modify external variables, the problem is actually the input parameters and return values ​​of the output. For use shell variables, in fact, as a matter of how the shell variable argument to awk; and for variable assignment to the shell awk, awk can be seen as the return of output value.

use shell variables awk

awk options parameter is passed -v [awk_var=$SHELL_VAR], it should be added '' before the main awk program enclosed.

1 sample script:

 

 

Output:

 

 

to the shell awk variable assignment

 

A single variable

Now for awk to shell variable assignment, it can be seen as the return of awk output value. For the return of a single variable, you can use the shell backquote syntax. Value of the result back quotes `commands may be preserved assigned to a variable, the commands of course also be awk.

Sample script 2:

 

 

Output:

 

 

Multiple variables

For the assignment of multiple variables, we need to briefly explain the eval command to use, it's eval command to execute a shell command as a parameter, such as:

Sample script 3:

 

 

Output:

 

 

But the script above without "eval", the result is the same, what is the difference? Personal understanding, if not necessarily the same situation encountered in the implementation of each command script, we only need minor cost of raw command executed in the script the string, and then combined with the eval command can be achieved.

DETAILED to awk program, we can generate a command string awk as "intermediate program", then the corresponding multi-variable assignment we are now the problem, the fact is to generate a similar "VAR1 = 123; VAR2 = 321; VAR3 = 111 "this string of.

So, although here that is multi-variable assignment, but in fact  still use the above mentioned one-way output variables, but this is the single variable is a string variable - a string containing shell commands  . You might think it would be a good mouthful ah ~ see an example:

4 sample script:

 

 

Output:

 

 

[What is the “eval” command in bash?] http://unix.stackexchange.com/questions/23111/what-is-the-eval-command-in-bash

[AWK call SHELL, and variable passed to the SHELL] http://smilejay.com/2014/01/awk-call-shell/

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/12490944.html