Automatically edit a text file using logic

I don't have a clue on how to do this.   I have excel, powershell, and ultraedit available on my pc.

I have a text file populated with multiple variable in a fixed width layout.

the logic I need to produce is.

if columns 19 - 27 are blank, then please insert the value of columns 319 - 227 into columns 19 - 27.

obviously if there is a value in 19 - 27 then I don't want it to be edited.
is there a way I can do this.   the file is a .txt
jamesmetcalf74Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
Best way to get help with this is to provide some sample files of what the input will be and expected output (only has to have a few lines).  That will eliminate a lot of uncertainty in meaning.
Bill PrewIT / Software Engineering ConsultantCommented:
Here's a starting point in a small VBS file.  Save it, and then run as follows.

cscript EE28727659.vbs in.txt out.txt

or

cscript EE28727659.vbs in.txt

The first format will read from the first text file, and apply the changes, and write to the second file on the command line.  In the second form, it will read from the file specified on the command line, and overwrite it.

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
 
' Get input file name from command line parm
If (WScript.Arguments.Count > 0) Then
   strInFile = WScript.Arguments(0)
Else
   WScript.Echo "No input filename specified."
   WScript.Quit
End If

' Get output file name from command line parm
If (WScript.Arguments.Count > 1) Then
   strOutFile = WScript.Arguments(1)
Else
   strOutFile = strInFile
End If

' Create filesystem object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Make sure input file exists
If Not objFSO.FileExists(strInFile) Then
   Wscript.Echo "ERROR: Input file """ & strInFile & """ does not exist."
   Wscript.Quit
End If

' Read input file into array of lines
Set objFile = objFSO.OpenTextFile(strInFile, ForReading, False, TriStateUseDefault)
strData = objFile.ReadAll
arrData = Split(strData, vbCrLf)
objFile.Close

' Make desired changes
For i = 0 to UBound(arrData)
   strLine = arrData(i)
   intLen = Len(strLine)
   ' Is it long enough to possibly adjust?
   If intLen > 226 Then
      ' Are there spaces that need replacing?
      If Mid(strLine, 19, 9) = "         " Then
         ' Rebuild line as needed
         strLine = Mid(strLine, 1, 18) & Mid(strLine, 219, 9) & Mid(strLine, 28)
         arrData(i) = strLine
      End If
   End If
Next

' Write output file
Set objFile = objFSO.OpenTextFile(strOutFile, ForWriting, True)
objFile.Write Join(arrData, vbCrLf)
objFile.Close

Open in new window

~bp
jamesmetcalf74Author Commented:
Hi Bill-
My knowledge of vb.script is basically zero.  I tried to run your function but it asked me for file name but didn't give me  a place to submit it.

Also attached, is before and after of desired fix
experts-exchange.jpg
Discover the Answer to Productive IT

Discover app within WatchGuard's Wi-Fi Cloud helps you optimize W-Fi user experience with the most complete set of visibility, troubleshooting, and network health features. Quickly pinpointing network problems will lead to more happy users and most importantly, productive IT.

Bill PrewIT / Software Engineering ConsultantCommented:
You need to run it from a command line, or BAT script, using the command line syntax I posted about the solution.  You have to pass it the name of the file on the command line itself.

~bp
jamesmetcalf74Author Commented:
ok i ran it,
no changes identified.  it did generate a file that looks exactly like the inputfile
Bill PrewIT / Software Engineering ConsultantCommented:
Can you post the file here please?

~bp
jamesmetcalf74Author Commented:
Here you go bill

here are examples of the before file and an example of desired after file.

this file has been trimmed in size dramatically and sanitizied
before.txt
after.txt
Bill PrewIT / Software Engineering ConsultantCommented:
Interesting, those files look harmless enough in a hex viewer, but are crashing my default editor (SlickEdit) and well as Notepad++, so there seems to be something interesting going on.  I will poke at them further...

~bp
Bill PrewIT / Software Engineering ConsultantCommented:
In your original post you said:

if columns 19 - 27 are blank, then please insert the value of columns 319 - 227 into columns 19 - 27.

I took judgement on the typo and interpreted it as 219 - 227?  Was that wrong, should it be something different?

~bp
jamesmetcalf74Author Commented:
sorry Bill
its 319 - 327
the before and after text files should show you 1 row that meets the criteria for the editing of data.
the rest of the rows should be left alone because they have data in them.
Bill PrewIT / Software Engineering ConsultantCommented:
Okay, I adjusted, try this version.

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
 
' Get input file name from command line parm
If (WScript.Arguments.Count > 0) Then
   strInFile = WScript.Arguments(0)
Else
   WScript.Echo "No input filename specified."
   WScript.Quit
End If

' Get output file name from command line parm
If (WScript.Arguments.Count > 1) Then
   strOutFile = WScript.Arguments(1)
Else
   strOutFile = strInFile
End If

' Create filesystem object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Make sure input file exists
If Not objFSO.FileExists(strInFile) Then
   Wscript.Echo "ERROR: Input file """ & strInFile & """ does not exist."
   Wscript.Quit
End If

' Read input file into array of lines
Set objFile = objFSO.OpenTextFile(strInFile, ForReading, False, TriStateUseDefault)
strData = objFile.ReadAll
arrData = Split(strData, vbCrLf)
objFile.Close

' Make desired changes
For i = 0 to UBound(arrData)
   strLine = arrData(i)
   intLen = Len(strLine)
   ' Is it long enough to possibly adjust?
   If intLen > 326 Then
      ' Are there spaces that need replacing?
      If Mid(strLine, 19, 9) = "         " Then
         ' Rebuild line as needed
         strLine = Mid(strLine, 1, 18) & Mid(strLine, 319, 9) & Mid(strLine, 28)
         arrData(i) = strLine
      End If
   End If
Next

' Write output file
Set objFile = objFSO.OpenTextFile(strOutFile, ForWriting, True)
objFile.Write Join(arrData, vbCrLf)
objFile.Close

Open in new window

~bp
footechCommented:
Here's a PS version.
$inFile = "before.txt"
$outFile = "after.txt"
Get-Content $inFile | ForEach `
{
    If ( $_.Substring(19,8) -match " {8}" )
    {
        $line  = $_ -split ""
        18..26 | ForEach { $line[$_] = $line[$_+300] }
        -join $line
    }
    Else
    { $_ }
} | Set-Content $outFile

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jamesmetcalf74Author Commented:
It totally worked
Bill
wish i could give you a thousand points.
saved me  a BOATLOAD of work
jamesmetcalf74Author Commented:
Awesome
footechCommented:
Just so you realize, Bill and I are two different people.  Not sure if you meant to split points or not, depending on whichever solution worked for you.
Bill PrewIT / Software Engineering ConsultantCommented:
@jamesmetcalf74,

Just wanted to double check, did you close this question the way you wanted?  You are using the PS solution, correct.

~bp
jamesmetcalf74Author Commented:
Im sorry guys......
i used the ps solution from footech but i didn't realize that another person had joined the thread of help.  I thought it was you bill that provided the solution.
sorry for the confusion gentlemen
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.