我只想将file中的几个字符串替换为空,但是sed替换了整个行。有人能帮我一下吗?
File.xml中的行:
<tag>sample text1 text2</tag>
我的代码:
sed "s/'text1 text2'//" file.xml 2>/dev/null || :
我也试过了
sed -i -e "s/'text1 text2'//" file.xml 2>/dev/null || :
预期结果:
<tag>sample</tag>
实际结果:
The whole line is removed from file.
其他:
text1 and text 2 are complex text with .=- characters in it
我能做些什么来解决这个问题呢?
提亚
发布于 2018-06-11 03:33:17
删除单引号:
sed "s/text1 text2//" file.xml
发布于 2018-06-11 18:27:01
你可以使用
sed 's/\([^ ]*\)[^<]*\(.*\)/\1\2/' filename
输出:
<tag>sample</tag>
使用分组。首先将空格之前的所有字符分组在一起,然后匹配到<
之前的所有字符,并将后面的所有字符分组到另一组中。
https://stackoverflow.com/questions/50787310
复制相似问题