Xenakis Kostas
Xenakis Kostas

Reputation: 3

Extract only ip addresses from a txt file an put them into a line

i have a txt file full of traceroutes and its on this form.

traceroute to 188.164.206.1 (188.164.206.1), 30 hops max, 60 byte packets

 1  83.212.10.1 (83.212.10.1)  3.257 ms  4.571 ms *

 2  192.168.199.1 (192.168.199.1)  5.497 ms  3.171 ms  5.537 ms

 3  ihu-1-gw.eier.access-link.grnet.gr (194.177.208.13)  11.188 ms  13.018 ms  11.305 ms

 4  wind.gr-ix.gr (176.126.38.9)  14.421 ms  12.267 ms  14.714 ms

traceroute to 194.39.121.182 (194.39.121.182), 30 hops max, 60 byte packets

1 ec2-50-112-0-84.us-west-2.compute.amazonaws.com (50.112.0.84) 2.026 ms 1.986 ms 1.969 ms

2 100.64.1.149 (100.64.1.149) 1.757 ms 100.64.1.171 (100.64.1.171) 2.006 ms 100.64.1.157 (100.64.1.157) 2.772 ms

3 * * *

4 * * *

5 205.251.232.226 (205.251.232.226) 2.324 ms 205.251.232.166 (205.251.232.166) 2.332 ms 205.251.232.226 (205.251.232.226) 2.988 ms

Im trying to make a script that will read all the lines of the file and it will copy only the ip adreses that are inside the (...) and will transfer them into one line. So every traceroute will be only one line. For example the above traceroute will become

83.212.10.1 192.168.199.1 194.177.208.13 176.126.38.9

I can not think any obvious ways on how to do this, Thanks in advance.

Upvotes: 0

Views: 98

Answers (1)

Etan Reisner
Etan Reisner

Reputation: 81052

This should do what you want.

awk -F'[()]' '/^ / {printf "%s ", $2}'

Using a standard value for FS:

awk '/^ / {gsub(/[()]/, "", $3); printf "%s ", $3}'

Or not using a regex at all:

awk '/^ / {printf "%s ", substr($3, 2, (length($3) - 2))}'

Edit: Handle multiple traceroute output in the same file correctly.

awk -F'[()]' '/^ / {printf "%s ", $2; next} /^[^[:space:]]/{print ""}'

Upvotes: 2

Related Questions