[英]Search string and add string at end of line but before quotes using sed
我正在尋求幫助以查找字符串並在行尾但在引號之前添加字符串
文件輸出:
I have a file
search_string="It has some text"
some other text
我嘗試了以下方法:
sed -i '/^search_string=/ s/$/ add_string one add_string two/' file
我得到以下輸出:
I have a file
search_string="It has some text" add_string one add_string two
some other text
預期輸出:
I have a file
search_string="It has some text add_string one add_string two"
some other text
$ sed 's/search_string="[^"]*/& add_string_one add string two/' file
您可以使用-i
就地進行操作:
$ sed -i.bak 's/search_string="[^"]*/& add_string_one add string two/' file
$ diff file.bak file
2c2
< search_string="It has some text"
---
> search_string="It has some text add_string_one add string two"
sed '/^.*search_string=/ s/\(.*\)"/\1 add_string one add_string two"/' file
簡單修改OP的代碼
$ sed '/^search_string=/ s/"$/ add_string one add_string two"/' file
I have a file
search_string="It has some text add_string one add_string two"
some other text
"$
匹配行尾的雙引號 或者,為避免將文本重復顯示較長的文本,請使用&
。 例如:
$ sed '/^search_string=/ s/ text"$/ add_string one add_string two&/' file
I have a file
search_string="It has some add_string one add_string two text"
some other text
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.