-1

The text lines are like this:

12341234567 2635473234 123456789 sfhwruewtbdsvmhsgfiergn (this is one of the lines)

12341234567 2635473234 xxxxxxxxx sfhwruewtbdsvmhsgfiergn (I want only the X's to be replaced by spaces) and the other text in their respective places intact.

The file is a log file and is up to 500 lines some time.

J. Steen
  • 15,470
  • 15
  • 56
  • 63
Joe James
  • 1
  • 1

2 Answers2

1

EDIT: Remove () on WriteLine

I'll do you a favor

Change the input and output file path to be fullpath

Reference: 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
Community
  • 1
  • 1
Larry
  • 2,764
  • 2
  • 25
  • 36
  • I will upvote as soon as you delete the spurious () in the .WriteLine statement – Ekkehard.Horner Dec 18 '12 at 10:45
  • 1
    The .WriteLine statement is a Sub call (no return value obtained) and the errror 1044 "Cannot use parentheses when calling a Sub" is only hidden, because the 'compiler' sees the () as "please pass me by value" parentheses. – Ekkehard.Horner Dec 18 '12 at 11:07
  • sir, though that script worked perfectly, But there was on error that occurs while running that file – Joe James Dec 21 '12 at 10:11
  • sir that script worked perfectly but it should delete numbers and not text even if it is present in that particular spot.. – Joe James Dec 21 '12 at 10:35
  • @JoeJames Hi, please clarify a few cases. 1. you mean REPLACE by Spaces instead of DELETE? 2. how about partial number? e.g. a1b2c3d4e5 ? thanks – Larry Dec 21 '12 at 11:02
  • Hi, And thanx for responding.. here it goes.. sir if the entries are more the text continues on the other line as well in that case what the script does is it deletes the continuing text on 23 to 33 where as it should be like this i.. (it should delete anything between 0-9 and not A-Z).. I hope U got my point. thanx and Wishing U and you Family a Merry Christmas... – Joe James Dec 22 '12 at 12:04
0

As you asked for deletion (not replacing by spaces), I propose:

  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()

output:

============================================================
12341234567 2635473234 123456789 sfhwruewtbdsvmhsgfiergn
12341234567 2635473234 XxxxxxxxX sfhwruewtbdsvmhsgfiergn
12341234567 2635473234 ......... sfhwruewtbdsvmhsgfiergn

^(.{23})(.{10})
12341234567 2635473234 sfhwruewtbdsvmhsgfiergn
12341234567 2635473234 sfhwruewtbdsvmhsgfiergn
12341234567 2635473234 sfhwruewtbdsvmhsgfiergn

============================================================

To use the strategy "ReadAll, Replace using RegExp, Write back" in production, you should study the VBScript docs wrt FileSystemObject and RegExp.

Update (after spec change):

Use

  oFS.CreateTextFile(csOutF, True).Write reR.Replace(sAll, "$1" & Space(10))

to get:

...
12341234567 2635473234 ......... sfhwruewtbdsvmhsgfiergn

^(.{23})(.{10})
12341234567 2635473234           sfhwruewtbdsvmhsgfiergn
...

Update II (after spec change: zap numbers only):

Change the RegExp pattern from

  reR.Pattern = "^(.{23})(.{10})"   ' look for 10 arbitrary characters (.)

to

  reR.Pattern = "^(.{23})(\d{9} )" ' look for 9 digits (\d) plus 1 space

output:

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

See RegExp Syntax to learn about the funny letters in the pattern.

Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • sir though that script worked perfectly well, but the problem is it even delete text at the same spot in the next line. Can you please give me a solution that the script deletes only number in that particular place and not text – Joe James Dec 21 '12 at 10:12