2016-05-04 5 views
1

Würde mir irgendjemand sagen, wie man alle ipv4-adresse (in der "()") in diesem inhalt in bash in linux bekommt?wie bekomme ich die ganze ipv4-adresse in diesem inhalt

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 
.... 

Das Ergebnis sollte für die Hilfe bei

223.5.5.5 
23.88.2.1 
172.246.0.235 
.... 

Dank viel wie dieser!

Antwort

3

diese grep Linie arbeitet für Ihr Beispiel:

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

Es gibt:

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 mit PCRE (-P):

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

Beispiel:

$ 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

Eine Zeile Skript in Bash (Grundkenntnisse in der Bash wird genug sein) 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 

Weitere Erklärung:

# 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

Obwohl dieser Code die Frage beantworten kann, sofern zusätzlicher Kontext in Bezug auf _why_ und/oder _how_ es antwortet die Frage würde deutlich im beweisen Sie seine langfristige Wert. Bitte [bearbeiten] Sie Ihre Antwort, um eine Erklärung hinzuzufügen. –

Verwandte Themen