我有/var/tmp/
以下随机.xml
文件,并且我有这样的行包含: cidr=x.x.x.x/32
或x.x.x.x
仅,现在我需要搜索 IP 并像 add/edit/del 一样替换它们。
前:
file1.xml: <user id="1" cidr="192.168.1.12">
file2.xml: <user id="2" cidr="192.168.1.12,192.168.1.13">
file3.xml: <user id="3" cidr="8.8.8.8">
file4.xml: <user id="4" cidr="8.8.8.8/32">
fileX.xml: <user id="5" cidr="192.168.1.12/32">
之后(预期输出):
file1.xml: <user id="1" >
file2.xml: <user id="2" cidr="192.168.1.13">
file3.xml: <user id="3" cidr="8.8.8.8">
file4.xml: <user id="4" cidr="8.8.8.8/32">
fileX.xml: <user id="5" >
现在,要找到哪一行包含匹配的 ip 然后替换它,我在一行中尝试了类似的东西:
grep -R 'cidr=' ./*.xml # search in whole directory
| awk '/192.168.1.12/ {print $1}' # find for a given search criteria
| sed 's/^.\///g' # replace some not necessary values
| sed 's/://' # ..
| xargs sed -i s/""/""/ /var/tmp/ # on the fly workout on that line
# + save it to that file
或者
grep -R 'cidr=' ./*.xml # search in whole directory
| awk '/192.168.1.12/ {print $1}' # find for a given search criteria
| sed 's/^.\///g' # replace some not necessary values
| sed 's/://' # ..
| ( grep -R 'cidr=' ./*.xml
| awk '/192.168.1.12/ {print $4}'
| sed 's/ //g'
)
| xargs sed -i s/""/""/ /var/tmp/ # on the fly workout on that line
# + save it to that file
但似乎不像预期的输出那样工作。