从左侧和右侧删除文本并将字符串保留在中间

时间:2017-05-20 08:20:00

标签: linux string ubuntu sed terminal

有没有办法在linux中修改这个字符串



<host endtime="1495011823"><address addr="185.127.7.58" addrtype="ipv4"/><ports><port protocol="tcp" portid="3389"><state state="open" reason="syn-ack" reason_ttl="115"/></port></ports></host>
<host endtime="1495011823"><address addr="188.128.124.114" addrtype="ipv4"/><ports><port protocol="tcp" portid="3389"><state state="open" reason="syn-ack" reason_ttl="46"/></port></ports></host>
<host endtime="1495011823"><address addr="163.191.210.41" addrtype="ipv4"/><ports><port protocol="tcp" portid="3389"><state state="open" reason="syn-ack" reason_ttl="236"/></port></ports></host>
<host endtime="1495011823"><address addr="97.78.205.11" addrtype="ipv4"/><ports><port protocol="tcp" portid="445"><state state="open" reason="syn-ack" reason_ttl="45"/></port></ports></host>
<host endtime="1495011823"><address addr="93.103.218.96" addrtype="ipv4"/><ports><port protocol="tcp" portid="3389"><state state="open" reason="syn-ack" reason_ttl="112"/></port></ports></host>
<host endtime="1495011823"><address addr="75.106.77.247" addrtype="ipv4"/><ports><port protocol="tcp" portid="3389"><state state="open" reason="syn-ack" reason_ttl="237"/></port></ports></host>
<host endtime="1495011823"><address addr="5.200.35.35" addrtype="ipv4"/><ports><port protocol="tcp" portid="3389"><state state="open" reason="syn-ack" reason_ttl="120"/></port></ports></host>
<host endtime="1495011823"><address addr="147.196.146.236" addrtype="ipv4"/><ports><port protocol="tcp" portid="3389"><state state="open" reason="syn-ack" reason_ttl="55"/></port></ports></host>
<host endtime="1495011823"><address addr="89.21.189.7" addrtype="ipv4"/><ports><port protocol="tcp" portid="3389"><state state="open" reason="syn-ack" reason_ttl="53"/></port></ports></host>
<host endtime="1495011823"><address addr="78.139.51.5" addrtype="ipv4"/><ports><port protocol="tcp" portid="3389"><state state="open" reason="syn-ack" reason_ttl="108"/></port></ports></host>
<host endtime="1495011823"><address addr="62.193.141.140" addrtype="ipv4"/><ports><port protocol="tcp" portid="3389"><state state="open" reason="syn-ack" reason_ttl="107"/></port></ports></host>
<host endtime="1495011823"><address addr="136.227.47.165" addrtype="ipv4"/><ports><port protocol="tcp" portid="445"><state state="open" reason="syn-ack" reason_ttl="217"/></port></ports></host>
<host endtime="1495011823"><address addr="109.177.46.82" addrtype="ipv4"/><ports><port protocol="tcp" portid="445"><state state="open" reason="syn-ack" reason_ttl="50"/></port></ports></host>
<host endtime="1495011823"><address addr="89.116.124.135" addrtype="ipv4"/><ports><port protocol="tcp" portid="3389"><state state="open" reason="syn-ack" reason_ttl="110"/></port></ports></host>
<host endtime="1495011823"><address addr="88.245.53.232" addrtype="ipv4"/><ports><port protocol="tcp" portid="3389"><state state="open" reason="syn-ack" reason_ttl="112"/></port></ports></host>
<host endtime="1495011823"><address addr="101.55.37.147" addrtype="ipv4"/><ports><port protocol="tcp" portid="3389"><state state="open" reason="syn-ack" reason_ttl="108"/></port></ports></host>
<host endtime="1495011823"><address addr="206.201.204.229" addrtype="ipv4"/><ports><port protocol="tcp" portid="445"><state state="open" reason="syn-ack" reason_ttl="218"/></port></ports></host>
<host endtime="1495011823"><address addr="120.24.86.220" addrtype="ipv4"/><ports><port protocol="tcp" portid="3389"><state state="open" reason="syn-ack" reason_ttl="102"/></port></ports></host>
<host endtime="1495011823"><address addr="120.42.185.100" addrtype="ipv4"/><ports><port protocol="tcp" portid="3389"><state state="open" reason="syn-ack" reason_ttl="43"/></port></ports></host>
<host endtime="1495011823"><address addr="115.47.49.146" addrtype="ipv4"/><ports><port protocol="tcp" portid="3389"><state state="open" reason="syn-ack" reason_ttl="102"/></port></ports></host>
&#13;
&#13;
&#13;

我需要从左边删除它:

<host endtime="1495011823"><address addr="

这部分来自右边:

" addrtype="ipv4"/><ports><port protocol="tcp" portid="3389"><state state="open" reason="syn-ack" reason_ttl="115"/></port></ports></host>

image

并保持中间的ip 我怎么能用linux中的sed命令呢?

3 个答案:

答案 0 :(得分:1)

您可以使用grep仅过滤掉IP地址中的四个点分隔的数字块:

grep -o -P "([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})"

答案 1 :(得分:1)

sed 's/^.*addr="\([^"]*\).*$/\1/' file

echo $line |sed 's/^.*addr="\([^"]*\).*$/\1/'

答案 2 :(得分:0)

您可以直接 grep addr属性值:

grep -Po 'addr="\K[^"]*' file

输出:

185.127.7.58
188.128.124.114
163.191.210.41
97.78.205.11
93.103.218.96
75.106.77.247
5.200.35.35
147.196.146.236
89.21.189.7
78.139.51.5
62.193.141.140
136.227.47.165
109.177.46.82
89.116.124.135
88.245.53.232
101.55.37.147
206.201.204.229
120.24.86.220
120.42.185.100
115.47.49.146