-1
cat test.txt
baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/$basearch/
enabled=0  

Above is target file test.txt.I need to find pattern baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/$basearch/,replace next line enabled=0 to enabled=1.

I tried sed '@baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/$basearch/@!b;n;cenabled=1' test.txt but failed.

Notice:CANNOT use other delimeter such as @ instead of / because it's not replacement command.

Thanks in advance!

kittygirl
  • 2,255
  • 5
  • 24
  • 52

3 Answers3

0

If you are ok with awk could you please try following.

awk '
/baseurl=http:\/\/repo\.mysql\.com\/yum\/mysql-5\.5-community\/el\/7\/\$basearch\//{
  print
  flag=1
  next
}
flag && /enabled/{
  print "enabled=1"
  flag=""
  next
}
1
'  Input_file

In case you want to save output into Input_file itself append > temp_file && mv temp_file Input_file in above code.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
-1

This might work out—it finds 'baseurl=' then grabs the next line and substitutes 'enable=0' with 'enabled=1':

sed '/baseurl=/ {N;s/enabled=0/enabled=1/;}' test.txt

Feel free to change the initial regex to the line you mentioned. I just wanted to show the general solution.

I hope this helps!

Rafael
  • 7,605
  • 13
  • 31
  • 46
-1
sed -E '/baseurl=http:\/\/repo\.mysql\.com\/yum\/mysql-5\.5-community\/el\/7\/\$basearch\//!b;n;cenabled=1' test.txt > temp_file && mv temp_file  test.txt

This answer come from RavinderSingh13 and WiktorStribiżew,I just comibne it.

kittygirl
  • 2,255
  • 5
  • 24
  • 52