Split a column in a text file

Andrea

I have a system which generates 3 text (.txt) files on a daily basis, with 1000's of entries within each.

Once the text files are generated we run a vbscript (below) that modifies the files by entering data at specific column positions.

I now need this vbscript to do an additional task which is to separate a column in one of the text files.

So for example the TR201501554s.txt file looks like this:

6876786786  GFS8978976        I
6786786767  DDF78676          I
4343245443  SBSSK67676        I
8393372263  SBSSK56565        I
6545434347  DDF7878333        I
6757650000  SBSSK453          I

With the additional task of seperating the column, data will now look like this, with the column seperated at a specific position.

6876786786  GFS    8978976      I
6786786767  DDF    78676        I
4343245443  SBSSK  67676        I
8393372263  SBSSK  56565        I
6545434347  DDF    7878333      I
6757650000  SBSSK  453          I

I was thinking maybe I could add another "case" to accomplish this with maybe using a "regex" pattern, since the pattern would be only 3 companies to find (DDF, GFS and SBSSK).

But after looking at many examples, I am not really sure where to start.

Could someone let me know how to accomplish this additional task in our vbscript (below)?

Option Explicit
Const ForReading = 1
Const ForWriting = 2


Dim objFSO, pFolder, cFile, objWFSO, objFileInput, objFileOutput,strLine
Dim strInputPath, strOutputPath , sName, sExtension
Dim strSourceFileComplete, strTargetFileComplete, objSourceFile, objTargetFile
Dim iPos, rChar
Dim fileMatch


'folder paths
strInputPath = "C:\Scripts\Test"
strOutputPath = "C:\Scripts\Test"

'Create the filesystem object
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get a reference to the processing folder
Set pFolder = objFSO.GetFolder(strInputPath)

'loop through the folder and get the file names to be processed
For Each cFile In pFolder.Files
ProcessAFile cFile
Next

Sub ProcessAFile(objFile)
fileMatch = false

Select Case Left(objFile.Name,2)
    Case "MV"
        iPos = 257
        rChar = "YES"
        fileMatch = true
    Case "CA"
        iPos = 45
        rChar = "OCCUPIED"
        fileMatch = true
    Case "TR"
        iPos = 162
        rChar = "EUR"
        fileMatch = true
End Select

If fileMatch = true Then

    Set objWFSO = CreateObject("Scripting.FileSystemObject")
    Set objFileInput = objWFSO.OpenTextFile(objFile.Path, ForReading)
    strSourceFileComplete = objFile.Path
    sExtension = objWFSO.GetExtensionName(objFile.Name)
    sName = Replace(objFile.Name, "." & sExtension, "")

    strTargetFileComplete = strOutputPath & "\" & sName & "_mod." & sExtension
    Set objFileOutput = objFSO.OpenTextFile(strTargetFileComplete, ForWriting, True) 

        Do While Not objFileInput.AtEndOfStream
        strLine = objFileInput.ReadLine
        If Len(strLine) >= iPos Then
            objFileOutput.WriteLine(Left(strLine,iPos-1) & rChar)
        End If

    Loop
    objFileInput.Close
    objFileOutput.Close
    Set objFileInput = Nothing
    Set objFileOutput = Nothing

    Set objSourceFile = objWFSO.GetFile(strSourceFileComplete)
    objSourceFile.Delete
    Set objSourceFile = Nothing

    Set objTargetFile = objWFSO.GetFile(strTargetFileComplete)
    objTargetFile.Move strSourceFileComplete    
    Set objTargetFile = Nothing
    Set objWFSO = Nothing
End If
End Sub
Ansgar Wiechers

You could add a regular expression replacement to your input processing loop. Since you want to re-format the columns I'd do it with a replacement function. Define both the regular expression and the function in the global scope:

...
Set pFolder = objFSO.GetFolder(strInputPath)

Set re = New RegExp
re.Pattern = "  ([A-Z]+)(\d+)( +)"

Function ReFormatCol(m, g1, g2, g3, p, s)
  ReFormatCol = Left("  " & Left(g1 & "    ", 7) & g2 & g3, Len(m)+2)
End Function

'loop through the folder and get the file names to be processed
For Each cFile In pFolder.Files
...

and modify the input processing loop like this:

...
Do While Not objFileInput.AtEndOfStream
  strLine = re.Replace(objFileInput.ReadLine, GetRef("ReFormatCol"))
  If Len(strLine) >= iPos Then
    objFileOutput.WriteLine(Left(strLine,iPos-1) & rChar)
  End If
Loop
...

Note that you may need to change your iPos values, since splitting and re-formatting the columns increases the length of the lines by 2 characters.

The callback function ReFormatCol has the following (required) parameters:

  • m: the match of the regular expression (used to determine the length of the match)
  • g1, g2, g3: the three groups from the expression
  • p: the starting position of the match in the source string (but not used here)
  • s: the source string (but not used here)

The function constructs the replacement for the match from the 3 groups like this:

  • Left(g1 & " ", 7) appends 4 spaces to the first group (e.g. GFS) and trims it to 7 characters. This is based on the assumption that the first group will always be 3-5 characters long.
    GFS    
  • " " & ... & g2 & g3 prepends the result of the above operation with 2 spaces and appends the other 2 groups (8978976 &         ).
      GFS    8978976        
  • Left(..., Len(m)+2) then trims the result string to the length of the original match plus 2 characters (to account for the additional 2 spaces inserted to separate the new second column from the former second, now third, column).
      GFS    8978976      

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Split text file and rename on the basis of first column

From Dev

A way to split a text file into arbitrary blocks based on first column?

From Dev

Replace specific column data with split and readlines in text file

From Dev

I want to read a text file and split it based on column value

From Dev

Batch split a text file

From Dev

Dataframe Split Text into new column

From Dev

Split name column to csv file

From Java

Split text file into blocks and save

From Dev

Split text file into chunks of lines

From Dev

Parsing a text file with Python and split()

From Dev

Split a text file into muliple files

From Dev

Split string from text file

From Dev

python split and reverse text file

From Dev

Parsing and split from a text file

From Dev

How to Split a text file with no pattern

From Dev

Split a text file based on title

From Dev

Split text file into chunks of lines

From Dev

python split a text file function

From Dev

Split string from text file

From Dev

Split text file into smaller chunks

From Dev

How to split a text file into multiple text files

From Dev

Split file based on text inside file

From Dev

Split irregular text column into multiple columns in r

From Dev

awk: split a column of delimited text in a row into lines

From Dev

MySQL: Split longtext column into multiple text columns

From Dev

Python Pandas - How to format and split a text in column ?

From Dev

Split text in column into two separate columns

From Dev

Pandas- Split text in column and search in rows

From Dev

Split text column into 2 using regexp_split_to_array