文本行是这样的:
12341234567 2635473234 123456789 sfhwruewtbdsvmhsgfiergn
(这是其中一条线)
12341234567 2635473234 xxxxxxxxx sfhwruewtbdsvmhsgfiergn
(我只希望将 X 替换为空格),并且它们各自位置的其他文本完好无损。
该文件是一个日志文件,有时多达 500 行。
文本行是这样的:
12341234567 2635473234 123456789 sfhwruewtbdsvmhsgfiergn
(这是其中一条线)
12341234567 2635473234 xxxxxxxxx sfhwruewtbdsvmhsgfiergn
(我只希望将 X 替换为空格),并且它们各自位置的其他文本完好无损。
该文件是一个日志文件,有时多达 500 行。
编辑:在 WriteLine 上删除 ()
我帮你一个忙
将输入输出文件路径改为全路径
Dim objFSO
dim objFile
dim thisLine
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("C:\Users\wangCL\Desktop\data.txt")
If objFile.Size > 0 Then 'make sure the input file is not empty
Set inputFile = objFSO.OpenTextFile("C:\Users\wangCL\Desktop\data.txt", 1) 'Replace the filename here
set outputFile = objFSO.CreateTextFile("C:\Users\wangCL\Desktop\output.txt", TRUE) 'replace it with output filename
do while not inputFile.AtEndOfStream
thisLine = inputFile.ReadLine ' Read an entire line into a string.
newLine = mid(thisLine,1,23) & " " & mid(thisLine,33)
outputFile.WriteLine newLine
loop
inputFile.Close
outputFile.Close
end if
正如您要求删除(不替换为空格),我建议:
Const csInF = "..\data\13930436.txt"
Const csOutF = "..\data\13930436-c.txt"
Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
Dim sAll : sAll = oFS.OpenTextFile(csInF).ReadAll()
WScript.Echo sAll
Dim reR : Set reR = New RegExp
reR.Global = True
reR.Multiline = True
reR.Pattern = "^(.{23})(.{10})"
WScript.Echo reR.Pattern
oFS.CreateTextFile(csOutF, True).Write reR.Replace(sAll, "$1")
WScript.Echo oFS.OpenTextFile(csOutF).ReadAll()
输出:
============================================================
12341234567 2635473234 123456789 sfhwruewtbdsvmhsgfiergn
12341234567 2635473234 XxxxxxxxX sfhwruewtbdsvmhsgfiergn
12341234567 2635473234 ......... sfhwruewtbdsvmhsgfiergn
^(.{23})(.{10})
12341234567 2635473234 sfhwruewtbdsvmhsgfiergn
12341234567 2635473234 sfhwruewtbdsvmhsgfiergn
12341234567 2635473234 sfhwruewtbdsvmhsgfiergn
============================================================
要在生产中使用“ReadAll, Replace using RegExp, Write back”策略,您应该学习 VBScript 文档 wrt FileSystemObject 和 RegExp。
更新(规格更改后):
利用
oFS.CreateTextFile(csOutF, True).Write reR.Replace(sAll, "$1" & Space(10))
要得到:
...
12341234567 2635473234 ......... sfhwruewtbdsvmhsgfiergn
^(.{23})(.{10})
12341234567 2635473234 sfhwruewtbdsvmhsgfiergn
...
更新 II(规格更改后:仅限 zap 编号):
将 RegExp 模式从
reR.Pattern = "^(.{23})(.{10})" ' look for 10 arbitrary characters (.)
至
reR.Pattern = "^(.{23})(\d{9} )" ' look for 9 digits (\d) plus 1 space
输出:
12341234567 2635473234 123456789 sfhwruewtbdsvmhsgfiergn
12341234567 2635473234 XxxxxxxxX sfhwruewtbdsvmhsgfiergn
12341234567 2635473234 987654321 sfhwruewtbdsvmhsgfiergn
12341234567 2635473234 abcdefghi sfhwruewtbdsvmhsgfiergn
^(.{23})(\d{9} )
12341234567 2635473234 sfhwruewtbdsvmhsgfiergn
12341234567 2635473234 XxxxxxxxX sfhwruewtbdsvmhsgfiergn
12341234567 2635473234 sfhwruewtbdsvmhsgfiergn
12341234567 2635473234 abcdefghi sfhwruewtbdsvmhsgfiergn
请参阅RegExp 语法以了解模式中的有趣字母。