我有两个文件,一个是ips.yaml
:
-
dedicatedip: 1.1.1.11
-
dedicatedip: 2.2.2.2
-
dedicatedip: ''
-
dedicatedip: 3.3.3.3
-
dedicatedip: 3.3.3.33
字符串
这是我的result.txt
:
+----------+-----------------------+------------------+--------+-----------------------------+---------------+
| id | hostname | some other field | status | networks | plan |
+----------+-----------------------+------------------+--------+-----------------------------+---------------+
| 11111111 | some-hostname | some-value | Active | External_Networks=1.1.1.11 | not important |
| 11111111 | some another hostname | | Active | External_Networks=1.1.1.111 | not important |
| 11111111 | some.fqdn.com | | Active | External_Networks=1.1.1.112 | not important |
| 11111111 | fourth.hostname.com | | Active | External_Networks=1.1.1.1 | not important |
| 11111111 | the-other.com | IHaveSomething | Active | External_Networks=2.2.2.2 | not important |
| 11111111 | not.least.com | | Active | External_Networks=2.2.2.25 | not important |
| 11111111 | last.fqdn.com | The Last Value | Active | External_Networks=3.3.3.39 | not important |
+----------+-----------------------+------------------+--------+-----------------------------+---------------+
型
我想从result.txt
中删除ips.yaml
中存在的每个ip,这样预期的输出将是:
+----------+-----------------------+------------------+--------+-----------------------------+---------------+
| id | hostname | some other field | status | networks | plan |
+----------+-----------------------+------------------+--------+-----------------------------+---------------+
| 11111111 | some another hostname | | Active | External_Networks=1.1.1.111 | not important |
| 11111111 | some.fqdn.com | | Active | External_Networks=1.1.1.112 | not important |
| 11111111 | fourth.hostname.com | | Active | External_Networks=1.1.1.1 | not important |
| 11111111 | not.least.com | | Active | External_Networks=2.2.2.25 | not important |
| 11111111 | last.fqdn.com | The Last Value | Active | External_Networks=3.3.3.39 | not important |
+----------+-----------------------+------------------+--------+-----------------------------+---------------+
型
这是我目前的bash:
while read -r yaml_ips
do
while read -r result_ips
do
if [ "$yaml_ips" == "$result_ips" ]
then
sed "/$yaml_ips/d" result.txt
fi
done < <(grep -Eo 'External_Networks=[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' result.txt | awk -F '=' '{print $2}')
done < <(awk '/dedicatedip: [0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/ {print $2}' ips.yaml)
型
这是我当前的输出:
+----------+-----------------------+------------------+--------+-----------------------------+---------------+
| id | hostname | some other field | status | networks | plan |
+----------+-----------------------+------------------+--------+-----------------------------+---------------+
+----------+-----------------------+------------------+--------+-----------------------------+---------------+
+----------+-----------------------+------------------+--------+-----------------------------+---------------+
| id | hostname | some other field | status | networks | plan |
+----------+-----------------------+------------------+--------+-----------------------------+---------------+
| 11111111 | some-hostname | some-value | Active | External_Networks=1.1.1.11 | not important |
| 11111111 | some another hostname | | Active | External_Networks=1.1.1.111 | not important |
| 11111111 | some.fqdn.com | | Active | External_Networks=1.1.1.112 | not important |
| 11111111 | fourth.hostname.com | | Active | External_Networks=1.1.1.1 | not important |
| 11111111 | last.fqdn.com | The Last Value | Active | External_Networks=3.3.3.39 | not important |
+----------+-----------------------+------------------+--------+-----------------------------+---------------+
型
3条答案
按热度按时间hfyxw5xn1#
仅使用您所显示的示例,请尝试以下GNU
awk
代码。字符串
型
mpgws1up2#
这两个pass
awk
可以在一个命令中做到这一点:字符串
这里:
-F ': '
将第一个文件的字段分隔符设置为": "
,即ips.yaml
FS='[[:blank:]]*[|][[:blank:]]*'
将第二个文件的字段分隔符设置为|
,两侧用空格包围,即result.txt
NF > 1 && $2 ~ /^[0-9]/
:过滤第一个文件中的实际ip
地址行,ips[$2]
将每个ip地址存储在关联数组ips
中FNR > 3
处理了从第4行开始的第2个字段,留下前3个标题行sub(/^[^=]*=/, "", nw)
从networks
列中删除=
和=
之前的文本,即第二个文件中的$6
rur96b6h3#
使用任何awk:
字符串