0

please assist, can't figure out how to add text after a specific line in txt file.

Trying to add "text-111" after specific ip address in a ip.txt file using bash with sed command. this is how the ip.txt looks before changes:

#### STRT 192.168.1.1 ####
#### END  192.168.1.1 ####

#### STRT 192.168.1.11 ####
#### END  192.168.1.11 ####

#### STRT 192.168.1.111 ####
#### END  192.168.1.111 ####

bash looks like that:

#!/bin/bash
ipaddr=192.168.1.1
sed -i "/$ipaddr/a text-111" ip.txt

after running this command it's adding "text-111" to ip.txt under all line that contains 192.168.1.1 as shown below

#### STRT 192.168.1.1 ####
text-111
#### END  192.168.1.1 ####

#### STRT 192.168.1.11 ####
text-111
#### END  192.168.1.11 ####

#### STRT 192.168.1.111 ####
text-111
#### END  192.168.1.111 ####

expected change:

#### STRT 192.168.1.1 ####
text-111
#### END  192.168.1.1 ####

#### STRT 192.168.1.11 ####
#### END  192.168.1.11 ####

#### STRT 192.168.1.111 ####
#### END  192.168.1.111 ####

Can't figure out how to avoid adding "text-111" under other similar lines?

Please assist on that matter :) Thanks!

Max
  • 3
  • 2

1 Answers1

0

Thanks to @Jetchisel,

#!/bin/bash

str='\bSTRT 192.168.1.1\b'
sed -i "/$str/a text-111" ip.txt
Max
  • 3
  • 2