linux - 与 grep 命令一起使用的 Shell 脚本变量

标签 linux bash shell unix

我正在为一个 ansible-deployment 项目编写脚本,它几乎完成了,只是修复了错误。 此脚本的一个选项是对先前指定的程序(例如 fontconfig)进行版本检查 Ansible 提供以下输出:

PLAY [all] 
********************************************************************

GATHERING FACTS ***************************************************************
ok: [192.168.40.132]
ok: [192.168.40.133]
ok: [192.168.40.134]
ok: [192.168.40.135]

TASK: [Find affected package] *************************************************
changed: [192.168.40.135]
changed: [192.168.40.132]
changed: [192.168.40.133]
changed: [192.168.40.134]

TASK: [debug var=dpkg.stdout_lines] *******************************************
ok: [192.168.40.132] => {
"dpkg.stdout_lines": [
    "ii  fontconfig                    2.11.0-6.3+deb8u1           i386         generic font configuration library - support binaries",
    "ii  fontconfig-config             2.11.0-6.3+deb8u1           all          generic font configuration library - configuration",
    "ii  libfontconfig1:i386           2.11.0-6.3+deb8u1           i386         generic font configuration library - runtime"
]
}
ok: [192.168.40.133] => {
"dpkg.stdout_lines": [
    "ii  fontconfig                    2.11.0-6.3+deb8u1           i386         generic font configuration library - support binaries",
    "ii  fontconfig-config             2.11.0-6.3+deb8u1           all          generic font configuration library - configuration",
    "ii  libfontconfig1:i386           2.11.0-6.3+deb8u1           i386         generic font configuration library - runtime"
]
}
ok: [192.168.40.134] => {
"dpkg.stdout_lines": [
    "ii  fontconfig                     2.11.0-6.3+deb8u1           amd64        generic font configuration library - support binaries",
    "ii  fontconfig-config              2.11.0-6.3+deb8u1           all          generic font configuration library - configuration",
    "ii  libfontconfig1:amd64           2.11.0-6.3+deb8u1           amd64        generic font configuration library - runtime"
]
}
ok: [192.168.40.135] => {
"dpkg.stdout_lines": [
    "rc  fontconfig                     2.11.0-6.3                  amd64        generic font configuration library - support binaries",
    "rc  fontconfig-config              2.11.0-6.3                  all          generic font configuration library - configuration",
    "rc  libfontconfig1:amd64           2.11.0-6.3                  amd64        generic font configuration library - runtime"
]
}

PLAY RECAP ********************************************************************
192.168.40.132             : ok=3    changed=1    unreachable=0    failed=0
192.168.40.133             : ok=3    changed=1    unreachable=0    failed=0
192.168.40.134             : ok=3    changed=1    unreachable=0    failed=0
192.168.40.135             : ok=3    changed=1    unreachable=0    failed=0

我想过滤掉包含例如版本为 2.11.0-6.3 但不包含 2.11.0-6.3+deb8u1 的服务器的 IP 地址。 目前我正在使用以下命令进行过滤($ver 之前是从直接输入中读取的):

cat output.txt | grep -B 3 $ver

很好,到目前为止还可以,但是上面提到的案例不起作用。 所以我尝试使用

part1="'(^| )";
part2="( |$)'";
done=$part1$ver$part2;
cat output.txt | grep -E $done -B 3

但是出现错误:

grep: »(« or »\(« without closing bracket
close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr

使用

cat output.txt | grep -E "$done" -B 3

cat output.txt | grep -E '$done' -B 3

有效,但我没有得到任何输出。

请帮忙。 谢谢!

最佳答案

要仅获取与 2.11.0-6.3 匹配的 IP 地址,请尝试:

$ awk  '/ 2.11.0-6.3 /{print $1}' RS='ok:' output.txt
[192.168.40.135]

使用 shell 变量指定版本:

$ ver='2.11.0-6.3'
$ awk -v ver="$ver"  '$0~(" " ver " "){print $1}' RS='ok:' output.txt
[192.168.40.135]

请注意,ver 被视为正则表达式。这意味着像 + 这样的正则表达式激活字符必须被转义。

关于linux - 与 grep 命令一起使用的 Shell 脚本变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38912773/

相关文章:

linux - 从文件中读取模式,grep 仅第一次出现

perl - 自动将指定文件插入另一个文件(使用 sed、AWK 或 Perl)

sql - 如何获取用户输入并将其应用于 bash 中的 sql 语句?

linux - 验证文件是否已创建 shell

linux - 在 Fish Shell 中,Ctrl Right 和 Ctrl Left 不会从一个单词移动到另一个单词

c++ - Protocol Buffers 的静态链接导致与现有符号的冲突

c++ - 从usb获取设备名称

linux - 为什么 date 命令不能识别我的变量?

linux - 在 bash 中获取参数

c++ - 使用 C++ 的 Unix 命令