user3860949
user3860949

Reputation: 125

how to edit a line having IPv4 address using sed command

I need to modify an ntp configuration file by adding some options to the line containing Ip addresses. I have been trying it for so long using sed command, but no able to modify the line unless i don't know the IP addresses. Let say, i have few lines as, server 172.0.0.1 server 10.0.0.1

I need to add iburst option after the ip address. I have tried command like.. sed -e 's/(\d{1,3}\.\d{1.3}\.\d{1,3}\.\d{1,3})/ \1 iburst/g' ntp_file or sed -e 's/^server +\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/server \1\.\2\.\3\ iburst/g' ntp_file but its not modifying the line. Any kind of suggestions would be really appriciated.

Upvotes: 1

Views: 424

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627468

The regex you have used as POSIX BRE cannot match the expected strings due to \d shorthand class that sed does not support, the misused dot inside a range quantifier and incorrect escaping of grouping and range quantifier delimiters.

You may use

sed -E -i 's/[0-9]{1,3}(\.[0-9]{1,3}){3}/ & iburst/g' ntp_file

The POSIX ERE (enabled with the -E option) expression means to match

  • [0-9]{1,3} - one to three digits
  • (\.[0-9]{1,3}){3} - three occurrences of a dot and one to three digits

The replacement pattern is & iburst where & stands for the whole match.

The g flag replaces all occurrences.

Upvotes: 2

Related Questions