awk 使用 Shell 引用
- 作者: 五速梦信息网
- 时间: 2026年03月19日 18:01
awk 使用 Shell 引用
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 中搜索的模式。</p><p>注意,双引号位于 awk 脚本 '{ print \)0 }‘ 之外。
接下来给脚本添加可执行权限并运行它,操作如下:
\( chmod +x test.sh</code></li><li><code>\) ./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.</p><p>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}' </pre><p><img src="https://www.tecmint.com/wp-content/plugins/lazy-load/images/1x1.trans.gif" alt="Find Username in Password File Using Awk"/></p><p>Find Username in Password File Using Awk</p><p>Explanation of the above command:</p><ol><li>-v – Awk option to declare a variable</li><li>username – is the shell variable</li><li>name – is the Awk variable</li></ol><p>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.
- 上一篇: iptables命令详解
- 下一篇: PERL 正则表达式详细说明
相关文章
-
iptables命令详解
iptables命令详解
- 技术栈
- 2026年03月19日
-
iptables常用命令
iptables常用命令
- 技术栈
- 2026年03月19日
-
resolv.conf:search、domain、nameserver解释
resolv.conf:search、domain、nameserver解释
- 技术栈
- 2026年03月19日
-
PERL 正则表达式详细说明
PERL 正则表达式详细说明
- 技术栈
- 2026年03月19日
-
Crontab的格式
Crontab的格式
- 技术栈
- 2026年03月19日
-
有关perl正则表达式的一些杂项
有关perl正则表达式的一些杂项
- 技术栈
- 2026年03月19日
