用于删除“减号”字符的模式,它们之间带有一些逗号

时间:2014-11-25 09:47:28

标签: regex perl awk sed

我正在寻找SED(首选)或其他实用程序的模式,可以处理regexp以从file.txt中删除不必要的-,个字符。

我想删除所有看起来像这样的内容:

-----------,-------------------------------------,---------,-,---------,---------

不删除任何不接近减号的逗号,并且如果后面跟着数字或字母,则不删除任何缺点。

我正在寻找的模式是-,,-以及--(我想是这样)

与模式不匹配的彗差和缩误应保持不变;)

输入如下:

xxxxxxxxxxxxxxxxxx,xxxxxxxxxxxxxxxxxx,xxxxxxxxxxxxxxxxxxxxxxxx,xxxxxxxxxxxxxxxxxxxxxx,xxxxxxxxxxx,xxxxxxxxx,xxxx
------------------,------------------,----------------------------------------------------------------------------
-----------------------------------------------------,-,---,---------------------------,-----------------,---------
1999,2014,591025,xöxxxxxxx,~,~,xxxxöxxx,xxx,1103469,27-xxx-76,x,01-xxx-99,31-xxx-01,,273,321,321,2
1999,2014,591025,xöxxxxxxx,~,~,xxxx,xxxxx,1103759,13-xxx-61,x,01-xxx-99,30-xxx-01,,~,321,321,1
1999,2014,591025,xöxxxxxxx,~,~,xxxxxxx,xxxx,1109909,01-xxx-70,x,01-xxx-99,31-xxx-00,,~,321,321,1

所需的输出:

xxxxxxxxxxxxxxxxxx,xxxxxxxxxxxxxxxxxx,xxxxxxxxxxxxxxxxxxxxxxxx,xxxxxxxxxxxxxxxxxxxxxx,xxxxxxxxxxx,xxxxxxxxx,xxxx
1999,2014,591025,xöxxxxxxx,~,~,xxxxöxxx,xxx,1103469,27-xxx-76,x,01-xxx-99,31-xxx-01,,273,321,321,2
1999,2014,591025,xöxxxxxxx,~,~,xxxx,xxxxx,1103759,13-xxx-61,x,01-xxx-99,30-xxx-01,,~,321,321,1
1999,2014,591025,xöxxxxxxx,~,~,xxxxxxx,xxxx,1109909,01-xxx-70,x,01-xxx-99,31-xxx-00,,~,321,321,1

3 个答案:

答案 0 :(得分:0)

您可以使用此sed:

sed -i.bak -r '/(-+,)+-+$/d' input
xxxxxxxxxxxxxxxxxx,xxxxxxxxxxxxxxxxxx,xxxxxxxxxxxxxxxxxxxxxxxx,xxxxxxxxxxxxxxxxxxxxxx,xxxxxxxxxxx,xxxxxxxxx,xxxx
1999,2014,591025,xöxxxxxxx,~,~,xxxxöxxx,xxx,1103469,27-xxx-76,x,01-xxx-99,31-xxx-01,,273,321,321,2
1999,2014,591025,xöxxxxxxx,~,~,xxxx,xxxxx,1103759,13-xxx-61,x,01-xxx-99,30-xxx-01,,~,321,321,1
1999,2014,591025,xöxxxxxxx,~,~,xxxxxxx,xxxx,1109909,01-xxx-70,x,01-xxx-99,31-xxx-00,,~,321,321,1

你可以使用这个awk:

awk -F '(-+,)+-+$' '$NF' input
xxxxxxxxxxxxxxxxxx,xxxxxxxxxxxxxxxxxx,xxxxxxxxxxxxxxxxxxxxxxxx,xxxxxxxxxxxxxxxxxxxxxx,xxxxxxxxxxx,xxxxxxxxx,xxxx
1999,2014,591025,xöxxxxxxx,~,~,xxxxöxxx,xxx,1103469,27-xxx-76,x,01-xxx-99,31-xxx-01,,273,321,321,2
1999,2014,591025,xöxxxxxxx,~,~,xxxx,xxxxx,1103759,13-xxx-61,x,01-xxx-99,30-xxx-01,,~,321,321,1
1999,2014,591025,xöxxxxxxx,~,~,xxxxxxx,xxxx,1109909,01-xxx-70,x,01-xxx-99,31-xxx-00,,~,321,321,1

甚至这个awk也可以:

awk -F '(-+,)+-+$' 'NF==1' input

答案 1 :(得分:0)

怎么样

$ sed 's/--\+\|-,\+\|,-//g; /^$/d' input
xxxxxxxxxxxxxxxxxx,xxxxxxxxxxxxxxxxxx,xxxxxxxxxxxxxxxxxxxxxxxx,xxxxxxxxxxxxxxxxxxxxxx,xxxxxxxxxxx,xxxxxxxxx,xxxx
1999,2014,591025,xöxxxxxxx,~,~,xxxxöxxx,xxx,1103469,27-xxx-76,x,01-xxx-99,31-xxx-01,,273,321,321,2
1999,2014,591025,xöxxxxxxx,~,~,xxxx,xxxxx,1103759,13-xxx-61,x,01-xxx-99,30-xxx-01,,~,321,321,1
1999,2014,591025,xöxxxxxxx,~,~,xxxxxxx,xxxx,1109909,01-xxx-70,x,01-xxx-99,31-xxx-00,,~,321,321,1

使用-r选项

看起来很好看的sed
$ sed -r 's/--+|-,+|,-//g; /^$/d' input

答案 2 :(得分:0)

另一个sed选项

sed '/^[-,]\+$/d'  file

另一个awk

awk '!/^[-,]+$/' file

这些将适用于您的输出