我正在寻找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
答案 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 -r 's/--+|-,+|,-//g; /^$/d' input
答案 2 :(得分:0)
另一个sed选项
sed '/^[-,]\+$/d' file
另一个awk
awk '!/^[-,]+$/' file
这些将适用于您的输出