Pass parameters to the script (1). Pass parameters to the script
In awk, the most confusing thing is to pass parameters to the script. The parameter assigns the value to a variable, which can be accessed in the awk script. This variable can be set on the command line and placed behind the script before the file name.
Awk 'script' var = value inputfile
Each item must be interpreted as a single parameter. Therefore, spaces are not allowed on both sides of the equal sign. You can also use this method to pass multiple parameters. For example, if you want to define variables high and low in the command line, you can use the following code to call awk:
$ Awk-f scriptfile high = 100 low = 60 datafile
In the script, these two variables can be accessed as any awk variable. If you want to write the script into the implementation of a shell script, you can pass shell command line parameters in the form of numerical values (shell provides command line parameter variables by location: $1 indicates the first parameter, $2 indicates the second parameter, and so on ).
Awk-f scriptfile "high = $1" "low = $2" datafile
If the shell script is named awket, you can call it as follows:
$ Awket 100 60
"100" corresponds to $1, and its value is assigned to the variable high. In addition, the output results of environment variables or commands can also be passed as variable values. There are two examples:
Awk '{...}' directory = $ cwd file1...
Awk '{...}' directory = 'pwd' file1...
"$ Cwd" returns the value of the variable cwd, that is, the current working path (only csh ). In the second example, run the pwd command using backquotes and assign the result to the variable directory (which is very convenient ).