0

I want to check in a file if the word "range 192.168.1.1 192.168.1.100" , if the word range exists in the line then I want to add another line "ip address dhcp" that should be above the line "range 192.168.1.1 192.168.1.100" as a result such as :

"range 192.168.1.1 192.168.1.100" as a result i get : 
"ip address dhcp"
"range 192.168.1.1 192.168.1.100"

I tried with this regular expression: "sed -r /s/([[:space:]]*)/range/l\ip address dhcp" but did not work for me !

Thanks in advance

Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • Maybe you want `sed '/range 0000\.00000 90000/i ip address dhcp' file`? – Wiktor Stribiżew Feb 24 '22 at 20:51
  • Please add sample input (no descriptions, no images) and your desired output for that sample input to your question (no comment). – Cyrus Feb 24 '22 at 20:58
  • It did not work. I don't want it to be like that, but that e.g.: range 192.168.1.1 192.168.1.100 the result: ip address dhcp range 192.168.1.1 192.168.1.100 Thank you for help – Ahmed Asakra Feb 24 '22 at 20:59
  • Then `sed -E '/range ([0-9]{1,3}\.){3}[0-9]{1,3}/i\ip address dhcp' file`? See https://ideone.com/oGgULX – Wiktor Stribiżew Feb 24 '22 at 21:07

2 Answers2

1

You can use either

sed -E '/range ([0-9]{1,3}\.){3}[0-9]{1,3}/i\ip address dhcp' file

Or, a bit more precise:

sed -E '/range ((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)([^0-9]|$)/i\ip address dhcp' <<< "$s"

See the online demo.

See Validating IPv4 addresses with regexp for more IP address patterns if you need to adjust this pattern part.

Also, see the sed docs:

i\
text
insert text before a line.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0
awk '/range/{print "ip address dhcp"} 1' file
Ed Morton
  • 188,023
  • 17
  • 78
  • 185