Страница 1 из 1

Extract all ip addresses with sed and awk from a string

Добавлено: 26 дек 2023, 00:49
xor

Source: https://stackoverflow.com/questions/486 ... m-a-string

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

==========

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"

==========

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

==========

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.]+"