sed删除'。'只在部分字符串中

时间:2013-03-04 17:36:04

标签: regex replace sed

我可能会遗漏一些基本的东西。我想删除以下字符串中&和s之间的所有'。'。我应该在我的sed中使用什么正则表达式?

**&v15.0&** something.anything abc.deg
**&v15.03.15&** something.anything abc.deg
**&v15.01.16.01&** something.anything abc.deg
**&v15.000.155.123.125&** something.anything abc.deg

修改为:

**&v150&** something.anything abc.deg
**&v150315&** something.anything abc.deg
**&v15011601&** something.anything abc.deg
**&v15000155123125&** something.anything abc.deg

可以有任何数量的'。'和&',并且我不希望修改&。以外的'。'。

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

试试这个oneliner:

awk -F'&' '{gsub(/\./,"",$2)}1' file

如果你真的想用sed做,而你有 Gnu sed ,你可以这样做:

sed -r 's/([^&]*&)([^&]*)(&.*)/echo "\1"$(echo "\2"\|sed "s#\\.##g")"\3"/ge' file

测试sed线:

kent$  echo "**&v15.0&** something.anything abc.deg
**&v15.03.15&** something.anything abc.deg
**&v15.01.16.01&** something.anything abc.deg
**&v15.000.155.123.125&** something.anything abc.deg"|sed -r 's/([^&]*&)([^&]*)(&.*)/echo "\1"$(echo "\2"\|sed "s#\\.##g")"\3"/ge'
**&v150&** something.anything abc.deg
**&v150315&** something.anything abc.deg
**&v15011601&** something.anything abc.deg
**&v15000155123125&** something.anything abc.deg

答案 1 :(得分:1)

使用常规sed(也是非GNU)尝试:

sed -e :a -e 's/\(&.*\)\.\(.*&\)/\1\2/; ta' file