How to let awk use Shell variables under Linux
When we write shell scripts, we typically include other small programs or commands in the script, such as awk operations. For awk , we need to find some way to pass some values from the shell to the awk operation. So how do you get awk to use Shell variables? Brother even next for you to introduce:
There are two possible ways for awk to use shell variables:
1. using Shell references
Let's use an example to demonstrate how to use a shell to override a shell variable in an awk command . In this example, we want to search for a user name in the file /etc/passwd , filtering and outputting the user's account information.
Therefore, we can write a test.sh script that reads as follows:
#!/bin/bash
# # # Read user name
Read-p " Please enter user name:" username
# # # Search for the user name in /etc/passwd and then output the details on the screen
cat/etc/passwd | awk "/$username/" ' {print $} '
Then, save the file and exit.
Description of the awk command in the test.sh script above:
cat/etc/passwd | awk "/$username/" ' {print $} '
"/$username/": the shell Reference is used to replace the shell variable in the awk command The value of the username. the username value is the pattern to search for in the file /etc/passwd .
Note that the double quotes are located outside of the awk script ' {print $} ' .
Next, add the executable permission to the script and run it as follows:
$ chmod +x test.sh
$./text.sh
After you run the script, it prompts you to enter a username, and then you enter a valid user name and return. You will see Detailed user account information from the/etc/passwd file, as shown in:
Shell script to find the user name in the Password file
Shell script to find the user name in the Password file
2. Assigning variables using awk
Compared with the method described above, this method is more single and better. Considering the example above, we can run a simple command to accomplish the same task. In this method, we use the -v option to assign the value of a shell variable to an awk variable.
First, create a shell variable usernameandgive it a name that we want to search for in the /etc/passwd file.
Username= "Aaronkilik"
Then type the following command and enter:
# CAT/ETC/PASSWD | Awk-v name= "$username" ' $ ~ Name {print $} '
Use awk to find the user name in the Password file
Use awk to find the user name in the Password file
Description of the above command:
-V: One of the awk options for declaring a variable
Username: is the shell variable
Name: is the awk variable
Let's take a closer look at the "$ ~ name" in the awk script ' $ ~ Name {print $} ' . Remember, when we introduced the awk comparison operatorin the fourth section of the awk series,value ~ Pattern was one of the comparison operators, It means: returns trueif value matches pattern .
The cat command matches the mode (Aaronkilik) with the output ($) from the pipeline to awk , which is the /c6> The name of the search in/etc/passwd, and finally, the comparison operation returns true. The line that contains user account information is then printed on the screen.
Conclusion
Brother Lian has introduced an important part of the awk feature that helps us to use shell variables in the awk command . Most of the time, you 'll write small awk Programs or commands in your shell scripts , so you need to clearly understand how to use the code in the awk command /c11> theshell variable.
How to let awk use Shell variables under Linux