我正在为一个 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/