regex 如何从txt文件中删除ip

5lhxktic  于 12个月前  发布在  其他
关注(0)|答案(3)|浏览(95)

我有两个文件,一个是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 |
+----------+-----------------------+------------------+--------+-----------------------------+---------------+

hfyxw5xn

hfyxw5xn1#

仅使用您所显示的示例,请尝试以下GNU awk代码。

awk '
FNR==NR && /^ +/{
  ips[$2]
  next
}
(match($0,/External_Networks=(\S+)/,arr) && (arr[1] in ips)){
  next
}
(/^+-/) || (/^\|/){
  print
  next
} ' FS=": " ips.yaml result.txt

字符串

  • 解释说明:* 为上述代码增加详细解释说明。
awk '                              ##starting awk program from here.
FNR==NR && /^ +/{                  ##Checking condition FNR==NR which will be TRUE when ips.yaml file is being read and checking condition if a line starts with 1 or more spaces then do following.
  ips[$2]                          ##Creating array ips with index of $2 here.
  next                             ##next will skip all further statements from here.
}
(match($0,/External_Networks=(\S+)/,arr) && (arr[1] in ips)){ ##Using match function to match External_Networks= followed by all 1 or more non-spaces and storing IPs values into array arr.
  next                             ##next will skip all further statements from here.
}
(/^+-/) || (/^\|/){                ##Checking condition if line starts from +- OR | then do following.
  print                            ##Print that line.
  next                             ##next will skip all further statements from here.
} ' FS=": " ips.yaml result.txt   ##Setting FS to colon space for file ips.yaml and then mentioning result.txt files.

mpgws1up

mpgws1up2#

这两个pass awk可以在一个命令中做到这一点:

awk -F ': ' '
FNR == NR {
   if (NF > 1 && $2 ~ /^[0-9]/)
      ips[$2]
   next
}
FNR > 3 {
   nw = $6
   sub(/^[^=]*=/, "", nw)
   if (nw in ips)
      next
}
1' ips.yaml FS='[[:blank:]]*[|][[:blank:]]*' result.txt

i+----------+-----------------------+------------------+--------+-----------------------------+---------------+
| 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 |
+----------+-----------------------+------------------+--------+-----------------------------+---------------+

字符串
这里:

  • -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
rur96b6h

rur96b6h3#

使用任何awk:

$ awk '
    NR==FNR { if ($1 == "dedicatedip:") ips[$2]; next }
    { o=$0; f=sub(/.*External_Networks=/,""); ip=$1; $0=o }
    !( f && (ip in ips) )
' ips.yaml result.txt
+----------+-----------------------+------------------+--------+-----------------------------+---------------+
| 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 |
+----------+-----------------------+------------------+--------+-----------------------------+---------------+

字符串

相关问题