簡體   English   中英

sed:僅用該行中第一個出現的字符串替換單詞,直到最后一次匹配

[英]Sed : replace words only with first occurrence of string in the line not till last match

我被困在腳本的某一處。 你能幫忙嗎?

問題:有一個帶有行的文件:

"<abc>-group ab:cd:ef +define_1 +DEFINE_2 </abc>"

現在,我想從該文件的所有行中刪除-group ab:cd:ef 我嘗試過

%s/-group .* //g

但是它將刪除所有測試直到最后一個空格。 和新的文本將成為<abc></abc>在我想要這樣的地方:

<abc>+define_1 +DEFINE_2 </abc>

謝謝。

使用sed

sed 's/-group [[:graph:]]* //' file

在vi / vim中:

%s/-group [[:graph:]]* //g

使用awk

awk '{sub(/-group [^ ]* /,"")}1' file
"<abc>+define_1 +DEFINE_2 </abc>"

還是這個sed

sed -r 's/-group [^ ]* //' file
"<abc>+define_1 +DEFINE_2 </abc>"

您應該將search & replace表達式更改為以下形式:

`%s/-group ab:cd:ef //g`

這樣可以避免整行匹配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM