使用 grep | 查找并替换带引号的字符串sed
Find&Replace a quoted string with grep | sed
我需要删除文本文件中始终以相同开头但以不同方式结尾的所有引号:
'something.with.quotation.123' must be something.with.quotation.123
'something.with.quotation.456' must be something.with.quotation.456
但不应更改不以此开头的带引号的字符串。
我一直在使用 grep 查找并打印带引号的字符串:
grep -o "something\.with\.quotation\.[^']*" file.txt
现在我需要通过管道将结果传递给 sed,但它不起作用:
grep -o "something\.with\.quotation\.[^']*" file.txt | sed -i "s/'$'/$/g" file.txt
我一直在尝试其他选项("s/\'$\'/$/g"
、"s/'\$'/\$/g"
、...)并进行了很多谷歌搜索,但没有办法。
你能告诉我在 sed 中从管道获取结果的正确方法吗?
这里不需要 grep
。只需像这样使用 sed
:
cat file
'something.with.quotation.123'
'something.with.quotation.456'
'foo bar'
sed -E "s/'(something\.with\.quotation\.[^']*)'//g" file
something.with.quotation.123
something.with.quotation.456
'foo bar'
我需要删除文本文件中始终以相同开头但以不同方式结尾的所有引号:
'something.with.quotation.123' must be something.with.quotation.123
'something.with.quotation.456' must be something.with.quotation.456
但不应更改不以此开头的带引号的字符串。
我一直在使用 grep 查找并打印带引号的字符串:
grep -o "something\.with\.quotation\.[^']*" file.txt
现在我需要通过管道将结果传递给 sed,但它不起作用:
grep -o "something\.with\.quotation\.[^']*" file.txt | sed -i "s/'$'/$/g" file.txt
我一直在尝试其他选项("s/\'$\'/$/g"
、"s/'\$'/\$/g"
、...)并进行了很多谷歌搜索,但没有办法。
你能告诉我在 sed 中从管道获取结果的正确方法吗?
这里不需要 grep
。只需像这样使用 sed
:
cat file
'something.with.quotation.123'
'something.with.quotation.456'
'foo bar'
sed -E "s/'(something\.with\.quotation\.[^']*)'//g" file
something.with.quotation.123
something.with.quotation.456
'foo bar'