RandomName
RandomName

Reputation: 1

Extract part of the string using sed

I want to display ip address from the string, but the code I found is doing it other way aroud, just removing the ip address.

sed -n 's/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/ /gp' <<< 'https://192.168.1.1/folder'

Upvotes: 0

Views: 60

Answers (2)

SLePort
SLePort

Reputation: 15461

Your command replace all ips with a space.

You can capture and output your ip using backreference:

sed -n 's/.*\(\(\b[0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\)\b.*/\1/p' <<< 'https://192.168.1.1/folder'

Upvotes: 1

mop
mop

Reputation: 433

sed 's/.*\/\(\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*/\1/' <<< 'https://192.168.1.1/folder'

Upvotes: 0

Related Questions