2016-05-04 11 views
1

このコンテンツのすべてのipv4アドレス( "()")を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 
.... 

結果はこのように私を助けるため

223.5.5.5 
23.88.2.1 
172.246.0.235 
.... 

おかげで多くのことをする必要があります!

答えて

3

このgrepのラインはあなたの例のために働く:

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

それは出力:PCREと

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 
0

grep-P):

​​3210

例:

bashで
$ 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 
0
$ 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 
0

一行スクリプト(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 
+0

_why_および/または_how_に関する追加の文脈 は、長期的には という値を証明してください。あなたの答えを[編集]して、説明を加えてください。 –

関連する問題