任何人都知道如何删除所有空格并使用Vim在文件中用逗号,
替换它们?
文件输入示例(单词可能无处不在!):
C1 TEST PROD
A1 BE
T1 B1
文件输出示例(属于同一行的所有单词都如下例所示):
C1,TEST,PROD
A1,BE
T1,B1
我找到了:
%s/\s\{1,}/,/gc
答案 0 :(得分:59)
首先删除空白行:
:g/^\s*$/d
然后在每一行(:s///
)上使用替换(%
),用逗号替换所有(g
)连续空格(\s\+
)({{1 }})。
,
答案 1 :(得分:4)
另一种方法:
%s/\s\{1,}/,/gc
答案 2 :(得分:0)
在转换带有标题的文本文件和带有空格的文本字段时,我使用了Option Explicit
'Sub EmphesizeSelectedText(color As Long)
Sub EmphesizeSelectedText()
Dim om_msg As Outlook.MailItem
Dim oi_insp As Outlook.Inspector
Dim ws_selec As Word.Selection
Dim wd_Document As Word.Document
Dim str_test As String
Dim lng_color As Long
lng_color = 255
'Zugriff auf aktive E-Mail
Set oi_insp = Application.ActiveInspector()
'Überprüft ob es sich wirklich um eine E-Mail handelt
If oi_insp.CurrentItem.Class = olMail Then
Set om_msg = oi_insp.CurrentItem
If oi_insp.EditorType = olEditorWord Then
' es gibt noch "olEditorHTML", "olEditorRTF", "olEditorText" und "olEditorWord"
' ist bei mir aber immer "olEditorWord" (= 4) - egal was ich im E-Mail Editor auswähle
' Set wd_Document = om_msg.Getinspector.WordEditor ' macht das gleiche wie nächste Zeile
Set wd_Document = oi_insp.WordEditor
Set ws_selec = wd_Document.Application.Selection
str_test = ws_selec.Text
Debug.Print ws_selec.Text
ws_selec.Text = "foo bar"
If om_msg.BodyFormat <> olFormatPlain Then
' auch wenn om_msg.BodyFormat = olFormatPlain ist, kann oi_insp.EditorType = olEditorWord sein
' doch dann gehen Formatierungen nicht -> Error !!!
With ws_selec.Font
.Bold = True
.color = lng_color ' = 255 = red
.color = wdColorBlue
End With
End If
ws_selec.Text = str_test
End If
End If
Set oi_insp = Nothing
Set ws_selec = Nothing
Set om_msg = Nothing
Set wd_Document = Nothing
End Sub
答案 3 :(得分:0)
如果文件包含n行,并且在每行的开头,每行的末尾以及之间包含空格。并且您要删除开头和结尾的空格,并在多个空格之间用逗号“,”替换。并重定向输出并保存到新文件,保持原始文件不变。
使用以下命令-
sed -e 's/^[ \t]*// ; s/[[:blank:]]*$// ; s/\s\+/,/g ; s/,$//' input-file-name | tee output-file-name
提供输入文件名的路径(如果不在同一目录中)。
或者您可以编写所有这些内容-
"s/^[ \t]*//
s/[[:blank:]]*$//
s/\s\+/,/g ; s/,$//"
保存在txt文件中。并使用-f选项。
因此命令变为-
sed -f commands.txt input-file-name.txt | tee output-file-name.txt
commnads.txt包含以上sed命令的条件