awk 使用 Shell 引用
有两种可能的方法可以让 awk 使用 shell 变量:
1. 使用 Shell 引用让我们用一个示例来演示如何在一条 awk 命令中使用 shell 引用来替代一个 shell 变量。在该示例中,我们希望在文件 /etc/passwd 中搜索一个用户名,过滤并输出用户的账户信息。
因此,我们可以编写一个 test.sh 脚本,内容如下:
#!/bin/bash
### 读取用户名
read -p "请输入用户名:" username
### 在 /etc/passwd 中搜索用户名,然后在屏幕上输出详细信息
cat /etc/passwd | awk "/$username/ "' { print $0 }'
然后,保存文件并退出。
上述 test.sh 脚本中 awk 命令的说明:
cat /etc/passwd | awk "/$username/ "' { print $0 }'
"/$username/ ":该 shell 引用用于在 awk 命令中替换 shell 变量 username 的值。username 的值就是要在文件 /etc/passwd 中搜索的模式。
注意,双引号位于 awk 脚本 '{ print $0 }' 之外。
接下来给脚本添加可执行权限并运行它,操作如下:
$ chmod +x test.sh
$ ./text.sh
运行脚本后,它会提示你输入一个用户名,然后你输入一个合法的用户名并回车。你将会看到来自 /etc/passwd 文件中详细的用户账户信息,如下图所示:
Therefore, we can write a test.sh script with the following content:
#!/bin/bash #read user input read -p "Please enter username:" username #search for username in /etc/passwd file and print details on the screen cat /etc/passwd | awk "/$username/ "' { print $0 }'
Thereafter, save the file and exit.
Interpretation of the Awk command in the test.sh script above:
cat /etc/passwd | awk "/$username/ "' { print $0 }'
"/$username/ " – shell quoting used to substitute value of shell variable username in Awk command. The value of username is the pattern to be searched in the file /etc/passwd.
Note that the double quote is outside the Awk script, ‘{ print $0 }’.
Then make the script executable and run it as follows:
$ chmod +x test.sh $ ./text.sh
After running the script, you will be prompted to enter a username, type a valid username and hit Enter. You will view the user’s account details from the /etc/passwd file as below:
Shell Script to Find Username in Password File
2. Using Awk’s Variable AssignmentThis method is much simpler and better in comparison to method one above. Considering the example above, we can run a simple command to accomplish the job. Under this method, we use the -v option to assign a shell variable to a Awk variable.
Firstly, create a shell variable, username and assign it the name that we want to search in the /etc/passswdfile:
username="aaronkilik"
Then type the command below and hit Enter:
# cat /etc/passwd | awk -v name="$username" ' $0 ~ name {print $0}'
Find Username in Password File Using Awk
Explanation of the above command:
Let us take a careful look at $0 ~ name inside the Awk script, ' $0 ~ name {print $0}'. Remember, when we covered Awk comparison operators in Part 4 of this series, one of the comparison operators was value ~pattern, which means: true if value matches the pattern.
The output($0) of cat command piped to Awk matches the pattern (aaronkilik) which is the name we are searching for in /etc/passwd, as a result, the comparison operation is true. The line containing the user’s account information is then printed on the screen.
ConclusionWe have covered an important section of Awk features, that can help us use shell variables within Awk commands. Many times, you will write small Awk programs or commands within shell scripts and therefore, you need to have a clear understanding of how to use shell variables within Awk commands.
In the next part of the Awk series, we shall dive into yet another critical section of Awk features, that is flow control statements. So stay tunned and let’s keep learning and sharing.