如何仅删除文本文件的所有行中的字符23到33?

时间:2012-12-18 10:01:24

标签: vbscript

文字行如下:

12341234567 2635473234 123456789 sfhwruewtbdsvmhsgfiergn(这是其中一行)

12341234567 2635473234 xxxxxxxxx sfhwruewtbdsvmhsgfiergn(我只希望用空格替换X)和各自位置的其他文本完整无缺。

该文件是一个日志文件,有时最多可达500行。

2 个答案:

答案 0 :(得分:1)

编辑:在WriteLine上删除()

我会帮你一个忙吗?

将输入和输出文件路径更改为完整路径

参考:Read and write into a file using VBScript

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

答案 1 :(得分:0)

当你要求删除(不用空格替换)时,我建议:

  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,使用RegExp替换,回写”,您应该研究VBScript docs 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 Syntax以了解模式中的有趣字母。