我有一个巨大的文件大约半场演出,文件的记录如下所示:
44 ,1577,23GRE ,GREASE THE ENGINE
44 ,1577,23GRE ,GREASE THE ENGINE
44 ,1577,24GRE ,GREASE THE WHEELS
我想在内容" GREASE THE ENGINE"之后删除逗号和空格之间的空格。并使用vi:
转换如下所示的文件"44","1577","23GRE","GREASE THE ENGINE"
"44","1577","23GRE","GREASE THE ENGINE"
"44","1577","24GRE","GREASE THE WHEELS"
我尝试通过给出一个命令来删除空格:1,$ s / // g这将删除所有空格并呈现如下所示的文件,这违背了目的。我想用空格来润滑发动机。
44,1577,23GRE,GREASETHEENGINE
44,1577,23GRE,GREASETHEENGINE
44,1577,24GRE,GREASETHEWHEELS
感谢任何或所有帮助。
由于
答案 0 :(得分:1)
您可以在vi
:
:1,$s/ *,/,/g
:1,$s/ *$//
:1,$s/,/","/g
首先,我们替换尾随空格,然后将,
之后的所有空格替换为单个,
。最后,我们匹配不是逗号的每个字段并引用它们。
[[:blank:]]
将匹配空格或标签。
对于您的输入,它给出:
"44","1577","23GRE","GREASE THE ENGINE"
"44","1577","23GRE","GREASE THE ENGINE"
"44","1577","24GRE","GREASE THE WHEELS"
答案 1 :(得分:0)
另一种可能性:
%s/\s\+\([,$]\)/"\1"/g
%s/^/"/g
%s/$/"/g
"
"
如果文件末尾有一个空行,最后会有一行""
,最容易删除。