shinobu
shinobu

Reputation: 111

About sed command

I'm struggling with a sed command.

I want to change the IP address in a file to 10.2.22.222:8000

The file contains this.

Uri = 10.1.11.111:8000

I believe that the following code is good.

sed -e "s/[0-9]{1-3}.[0-9]{1-3}.[0-9]{1-3}.[0-9]{1-3}:[0-9]{1-4}/10.2.22.222:8000/"

However, it does not work correctly, even though I believe my regular expression to be correct.

What should I do?

Upvotes: 2

Views: 56

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626802

Your regex is incorrect, the range quantifier syntax is {min,max}, not {min-max}. Also, the -e option does not help here much since your pattern is POSIX ERE compliant (as the {x,y} quantifier braces are not escaped).

Use

sed -E 's/[0-9]{1,3}(\.[0-9]{1,3}){3}:[0-9]{1,4}/10.2.22.222:8000/'

Or a POSIX BRE:

sed 's/[0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{3\}:[0-9]\{1,4\}/10.2.22.222:8000/'

See the online sed demo.

Upvotes: 1

Related Questions