简体   繁体   English

如何获取此内容中的所有ipv4地址

[英]how to get all the ipv4 address in this content

Would any body please tell me how to get all the ipv4 address(in the "()") in this content in bash in linux? 有没有人请告诉我如何在linux中的bash中获取这个内容中的所有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
 ....

The result should be like this 结果应该是这样的

223.5.5.5
23.88.2.1
172.246.0.235
....

Thanks a lot for helping me! 非常感谢帮助我!

this grep line works for your example: 这个grep行适合你的例子:

grep -Po '.*\(\K[^)]*' file

It outputs: 它输出:

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 with PCRE ( -P ): 用PCRE( -Pgrep

grep -Po '\(\K[^)]+(?=\))' file.txt

Example: 例:

$ 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/.*(\([^)]*\).*/\1/' 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

One line script in bash (basic knowledge in bash will be enough) http://www.tldp.org/LDP/abs/html/string-manipulation.html 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

More explanation: 更多解释:

# 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM