user7988893
user7988893

Reputation:

Extract all ip addresses with sed and awk from a string

It is simple to extract all ip addresses with grep from a string.

string="221.11.165.237xxxx221.11.165.233\n
219.158.9.97ttttt219.158.19.137"
echo $string |grep -oP "(\d+\.){3}\d+"
221.11.165.237
221.11.165.233
219.158.9.97
219.158.19.137

The regrex pattern is simple (\d+\.){3}\d+.
Do the same job with sed and awk.
For sed:

echo $string | sed 's/^\(\(\d\+\.\)\{3\}\d\+\)$/\1/g'
221.11.165.237xxxx221.11.165.233\n 219.158.9.97ttttt219.158.19.137

For awk:

echo $string |gawk 'match($0,/(\d+\.){3}\d+/,k){print k}'
echo $string |awk '/(\d+\.){3}\d+/{print}'

How to fix it for sed and gawk(awk)?
The expect output is the same as grep.

221.11.165.237
221.11.165.233
219.158.9.97
219.158.19.137

Upvotes: 1

Views: 18126

Answers (3)

grail
grail

Reputation: 930

As you weren't specific about what could be between the ip addresses, I went with the fact that only numbers and periods will be in the ip:

echo "$string" | sed -r 's/[^0-9.]+/\n/'

echo "$string" | awk '1' RS="[^0-9.]+"

Upvotes: 0

potong
potong

Reputation: 58391

This might work for you (GNU sed):

sed '/\n/!s/[0-9.]\+/\n&\n/;/^\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\n/P;D' file

Insert newlines either side of strings consisting only of numbers and periods. If a line contains only an IP address print it.

An easier-on-the-eye rendition uses the -r option:

sed -r '/\n/!s/[0-9.]+/\n&\n/;/^([0-9]{1,3}\.){3}[0-9]{1,3}\n/P;D' <<<"$string"

Upvotes: 3

Ed Morton
Ed Morton

Reputation: 203368

Very few tools will recognize \d as meaning digits. Just use [0-9] or [[:digit:]] instead:

$ echo "$string" | awk -v RS='([0-9]+\\.){3}[0-9]+' 'RT{print RT}'
221.11.165.237
221.11.165.233
219.158.9.97
219.158.19.137

The above uses GNU awk for multi-char RS and RT. With any awk:

$ echo "$string" | awk '{while ( match($0,/([0-9]+\.){3}[0-9]+/) ) { print substr($0,RSTART,RLENGTH); $0=substr($0,RSTART+RLENGTH) } }'
221.11.165.237
221.11.165.233
219.158.9.97
219.158.19.137

Upvotes: 3

Related Questions