如何获取此内容中的所有 ipv4 地址
how to get all the ipv4 address in this content
谁能告诉我如何在 bash in linux 的内容中获取所有 ipv4 地址(在“()”中)?
traceroute to 223.5.5.5 (223.5.5.5), 30 hops max, 60 byte packets
1 1.2-88-23.rdns.scalabledns.com (23.88.2.1) 0.388 ms 0.404 ms 0.415 ms
2 dist01-dc03-core.dc08.lax.ip4.scalabledns.com (172.246.0.235) 0.273 ms 18.825 ms 0.247 ms
3 207.254.184.97 (207.254.184.97) 0.660 ms 0.771 ms 0.834 ms
4 199.102.95.6 (199.102.95.6) 0.836 ms 0.808 ms 0.782 ms
5 219.158.30.53 (219.158.30.53) 192.201 ms 192.186 ms 192.160 ms
6 219.158.97.17 (219.158.97.17) 168.116 ms 168.193 ms 168.153 ms
....
结果应该是这样的
223.5.5.5
23.88.2.1
172.246.0.235
....
非常感谢您的帮助!
此 grep 行适用于您的示例:
grep -Po '.*\(\K[^)]*' file
它输出:
223.5.5.5
23.88.2.1
172.246.0.235
207.254.184.97
199.102.95.6
219.158.30.53
219.158.97.17
grep
与 PCRE (-P
):
grep -Po '\(\K[^)]+(?=\))' file.txt
示例:
$ cat file.txt
traceroute to 223.5.5.5 (223.5.5.5), 30 hops max, 60 byte packets
1 1.2-88-23.rdns.scalabledns.com (23.88.2.1) 0.388 ms 0.404 ms 0.415 ms
2 dist01-dc03-core.dc08.lax.ip4.scalabledns.com (172.246.0.235) 0.273 ms 18.825 ms 0.247 ms
3 207.254.184.97 (207.254.184.97) 0.660 ms 0.771 ms 0.834 ms
4 199.102.95.6 (199.102.95.6) 0.836 ms 0.808 ms 0.782 ms
5 219.158.30.53 (219.158.30.53) 192.201 ms 192.186 ms 192.160 ms
6 219.158.97.17 (219.158.97.17) 168.116 ms 168.193 ms 168.153 ms
$ grep -Po '\(\K[^)]+(?=\))' file.txt
223.5.5.5
23.88.2.1
172.246.0.235
207.254.184.97
199.102.95.6
219.158.30.53
219.158.97.17
$ sed 's/.*(\([^)]*\).*//' file
223.5.5.5
23.88.2.1
172.246.0.235
207.254.184.97
199.102.95.6
219.158.30.53
219.158.97.17
bash中的一行脚本(bash中的基本知识就足够了)
http://www.tldp.org/LDP/abs/html/string-manipulation.html
# while read l;do expr "$l" : ".*(\(.*\))";done <your_trc_file
223.5.5.5
23.88.2.1
172.246.0.235
207.254.184.97
199.102.95.6
219.158.30.53
219.158.97.17
更多说明:
# while read l;do echo "$l";done <file ## read lines from a file
# # Matching with reg-exp by 'exp :' command
# expr "abcXdefghXXijk" : ".*X.*XX" ## return length
11
# expr "abcXdefghXXijk" : ".*X\(.*\)XX" ## return extract
defgh
谁能告诉我如何在 bash in linux 的内容中获取所有 ipv4 地址(在“()”中)?
traceroute to 223.5.5.5 (223.5.5.5), 30 hops max, 60 byte packets
1 1.2-88-23.rdns.scalabledns.com (23.88.2.1) 0.388 ms 0.404 ms 0.415 ms
2 dist01-dc03-core.dc08.lax.ip4.scalabledns.com (172.246.0.235) 0.273 ms 18.825 ms 0.247 ms
3 207.254.184.97 (207.254.184.97) 0.660 ms 0.771 ms 0.834 ms
4 199.102.95.6 (199.102.95.6) 0.836 ms 0.808 ms 0.782 ms
5 219.158.30.53 (219.158.30.53) 192.201 ms 192.186 ms 192.160 ms
6 219.158.97.17 (219.158.97.17) 168.116 ms 168.193 ms 168.153 ms
....
结果应该是这样的
223.5.5.5
23.88.2.1
172.246.0.235
....
非常感谢您的帮助!
此 grep 行适用于您的示例:
grep -Po '.*\(\K[^)]*' file
它输出:
223.5.5.5
23.88.2.1
172.246.0.235
207.254.184.97
199.102.95.6
219.158.30.53
219.158.97.17
grep
与 PCRE (-P
):
grep -Po '\(\K[^)]+(?=\))' file.txt
示例:
$ cat file.txt
traceroute to 223.5.5.5 (223.5.5.5), 30 hops max, 60 byte packets
1 1.2-88-23.rdns.scalabledns.com (23.88.2.1) 0.388 ms 0.404 ms 0.415 ms
2 dist01-dc03-core.dc08.lax.ip4.scalabledns.com (172.246.0.235) 0.273 ms 18.825 ms 0.247 ms
3 207.254.184.97 (207.254.184.97) 0.660 ms 0.771 ms 0.834 ms
4 199.102.95.6 (199.102.95.6) 0.836 ms 0.808 ms 0.782 ms
5 219.158.30.53 (219.158.30.53) 192.201 ms 192.186 ms 192.160 ms
6 219.158.97.17 (219.158.97.17) 168.116 ms 168.193 ms 168.153 ms
$ grep -Po '\(\K[^)]+(?=\))' file.txt
223.5.5.5
23.88.2.1
172.246.0.235
207.254.184.97
199.102.95.6
219.158.30.53
219.158.97.17
$ sed 's/.*(\([^)]*\).*//' file
223.5.5.5
23.88.2.1
172.246.0.235
207.254.184.97
199.102.95.6
219.158.30.53
219.158.97.17
bash中的一行脚本(bash中的基本知识就足够了) http://www.tldp.org/LDP/abs/html/string-manipulation.html
# while read l;do expr "$l" : ".*(\(.*\))";done <your_trc_file
223.5.5.5
23.88.2.1
172.246.0.235
207.254.184.97
199.102.95.6
219.158.30.53
219.158.97.17
更多说明:
# while read l;do echo "$l";done <file ## read lines from a file
# # Matching with reg-exp by 'exp :' command
# expr "abcXdefghXXijk" : ".*X.*XX" ## return length
11
# expr "abcXdefghXXijk" : ".*X\(.*\)XX" ## return extract
defgh