簡體   English   中英

使用sed查找和替換字符串

[英]Find and replace strings using sed

我正在嘗試將單引號引起來的文檔中的字符串替換為沒有字符串。

'test' -> test

有沒有辦法使用sed我可以做到這一點?

謝謝

這將刪除所有帶引號的單詞周圍的單引號,並保留其他引號:

sed -E "s/'([a-zA-Z]*)'/\1/g" 

經過測試:

foo 'bar' it's 'OK' --> foo bar it's OK

請注意,它留下的報價在it's完整。

說明:

搜索正則表達式'([a-zA-Z]*)'與任何用引號引起來的單詞(字母,無空格)匹配,並使用方括號,它捕獲其中的單詞。 替換正則表達式\\1表示“組1”-第一個捕獲的組(即,搜索模式中的方括號組-單詞)

僅供參考,這是測試:

echo "foo 'bar' it's 'OK'" | sed -E "s/'([a-zA-Z]*)'/\1/g"

刪除某些單詞的單引號(示例中的文本):

kent$  echo "foo'text'000'bar'"|sed "s/'text'/text/g"
footext000'bar'

這個怎么樣?

$> cat foo
"test"
"bar"
baz "blub"

"one" "two" three

$> cat foo | sed -e 's/\"//g'
test
bar
baz blub

one two three

更新由於您只想替換“測試”,因此很有可能是:

$> cat foo | sed -e 's/\"test\"/test/g'
test
"bar"
baz "blub"

"one" "two" three

暫無
暫無

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

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