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. soHow do I get awk to use Shell variables? Brother Lian Linux Training let me introduce you to the following:
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 username, and give it a /etc/passwd that we want to The name of the search in the 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 script "$ ~ Name {print $} ' $0 ~ name awk series fourth describes the Span style= "FONT-FAMILY:CALIBRI;" >awk value ~ pattern value Span style= "font-family: ' The song Body '; > matches pattern true
The cat command matches the mode (Aaronkilik) with the output ($) from the pipeline to awk , which is the /c8> 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 (www.lampbrother.net) has introduced an important part of the awk feature that can help us in awk The shell variable is used in the command . In many cases, you will Write small awk Programs or commands in your shell scripts, so you need to clearly understand how the awk used in Commands Shell variable.
How to let awk use Shell variables under Linux