1

I have a file which has the below lines-

[arakoon_scaler_thresholds]
checker_loop_time = 3600

I want to replace the above 2 lines with the below line -

[arakoon_scaler_thresholds]
checker_loop_time = 60

I am using the below command but changes are not taking place.

sed -i "s/arakoon_scaler_thresholds\nchecker_loop_time = 3600/arakoon_scaler_thresholds\nchecker_loop_time = 60/g" file.ini

Note There are multiple parameters with name checker_loop_time, I want to change only that checker_loop_time which is under the section [arakoon_scaler_thresholds]

Sundeep
  • 23,246
  • 2
  • 28
  • 103
DD1
  • 357
  • 4
  • 14
  • 1
    Out of the box, `sed` only examines a single line at a time. Aside from that, your search expression looks for the keyword adjacent to a newline, whereas in your example, it is followed by a right square bracket. – tripleee Sep 29 '16 at 07:41

1 Answers1

2

Try this and once it is okay, use the -i option for inplace editing

$ cat ip.txt 
checker_loop_time = 5463

[arakoon_scaler_thresholds]
checker_loop_time = 3600
checker_loop_time = 766

$ sed -E '/arakoon_scaler_thresholds/{N;s/(checker_loop_time\s*=\s*)3600/\160/}' ip.txt 
checker_loop_time = 5463

[arakoon_scaler_thresholds]
checker_loop_time = 60
checker_loop_time = 766

This searches for arakoon_scaler_thresholds, then gets next line with N and then performs the required search and replace

You can also use awk

$ awk 'p ~ /arakoon_scaler_thresholds/{sub("checker_loop_time = 3600","checker_loop_time = 60")} {p=$0}1' ip.txt 
checker_loop_time = 5463

[arakoon_scaler_thresholds]
checker_loop_time = 60
checker_loop_time = 766

where previous line is saved in a variable

Sundeep
  • 23,246
  • 2
  • 28
  • 103
  • Thanks a lot Sundeep, I used the below command- sudo sed -E -i '/arakoon_scaler_thresholds/{N;s/(checker_loop_time\s*=\s*)3600/checker_loop_time = 60/}' – DD1 Sep 29 '16 at 07:19