2016-09-29 55 views
0

我有具有以下線 -無法更換線使用的sed

[arakoon_scaler_thresholds] 
checker_loop_time = 3600 

我想更換2行以上與以下行文件 -

[arakoon_scaler_thresholds] 
checker_loop_time = 60 

我使用下面的命令但沒有發生變化。

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

注意沒有與名稱checker_loop_time多個參數,我只想改變checker_loop_time這是根據部分[arakoon_scaler_thresholds]

+1

開箱,'sed'一次只檢查一個單行。除此之外,您的搜索表達式會查找與換行符相鄰的關鍵字,而在您的示例中,後面跟着右方括號。 – tripleee

回答

1

試試這個,一旦它是好的,使用就地編輯-i選項

$ 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 

此搜索arakoon_scaler_thresholds,然後獲取下一行與N和T母雞執行所需的搜索和替換

您還可以使用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 

在以前行被保存在一個變量

+0

非常感謝Sundeep,我使用了下面的命令 - sudo sed -E -i'/ arakoon_scaler_thresholds/{N; s /(checker_loop_time \ s * = \ s *)3600/checker_loop_time = 60 /}' –