0

Tell me how to add it using the sed command.

[Before revision]

BONDING_OPTS="mode=1 miimon=200 primary=ens192" 

[After revision]

BONDING_OPTS="mode=1 miimon=200 primary=ens192 primary=eth0"

I have succeeded so far.

sed "/BOND/s/$/primary=eth0/g" /etc/sysconfig/network-scripts/ifcfg-bond0

Output:

BONDING_OPTS="mode=1 miimon=200 primary=ens192"primary=eth0
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
sangyul lee
  • 103
  • 2
  • 7
  • It is un-clear what you are asking, please provide more detailed information in your post. Also add your efforts too which you have in order to solve your problem in your post with CODE TAGS. – RavinderSingh13 Jan 25 '19 at 06:56

2 Answers2

2

Thanks for adding your effort in your post. Could you please try following.

awk '/BOND/{sub(/\"$/,"  primary=eth0&")} 1' Input_file

In case you want to save output into Input_file itself then try:

awk '/BOND/{sub(/\"$/,"  primary=eth0&")} 1' Input_file > temp && mv temp Input_file

With sed:

sed '/BOND/s/\"$/  primary=eth0&/'  Input_file

Please use either sed -i (to save output into Input_file itself) OR use sed -i.bak (to save output into Input_file itself taking a backup of existing Input_file).

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • @sangyullee, glad that it helped you, you could give it sometime and when few answers are there then you could select anyone of them as correct one, cheers. – RavinderSingh13 Jan 25 '19 at 07:05
2
sed '/BOND/s/"$/ primary=eth0"/'
  • use single quotes, string inside double quotes is subject to shell interpretation before it is passed to sed and that leads to mistakes often
  • since you need to insert before something at end of line, you need to add that condition before applying $ anchor - which is " in this case

In case you have a non-trivial string or regex before the anchor, you can use & to get back that matched string in replacement section instead of duplicating the text

sed '/BOND/s/"$/ primary=eth0&/' ip.txt

For in-place editing, see sed in-place flag that works both on Mac (BSD) and Linux

Sundeep
  • 23,246
  • 2
  • 28
  • 103