Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $SourceFile = 'full path to the file here\input-file.txt'
- $DestFile = $SourceFile.Replace('input-file.txt', 'output-file.csv')
- # import and strip out unwanted 1st & 2nd lines
- $InFile = Get-Content -Path $SourceFile | Select-Object -skip 2
- # create a working file just in case i fubar spectacularly
- $WorkingFile = $InFile
- $Counter = 0
- while ($Counter -lt $WorkingFile.Count)
- {
- #debug info
- #$Counter
- #$WorkingFile[$Counter]
- #
- # fix embedded spaces in header line
- $WorkingFile[$Counter] = $WorkingFile[$Counter].Replace('T #', 'T_#').Replace('T N', 'T_N')
- # fix duplicate MINOR headers
- # O = Optional, A = Additional
- $WorkingFile[$Counter] = $WorkingFile[$Counter].Replace('MINOR MINOR MINOR MINOR', 'MINOR_O1 MINOR_O2 MINOR_A1 MINOR_A2')
- # temporarily replace the space in "FName MiddleInitial" items with an underscore
- # regex = [one or more letters][single space][single letter][literal .][one or more spaces]
- $WorkingFile[$Counter] = $WorkingFile[$Counter] -replace '(\w+)\s(\w\.\s+)', '$1_$2'
- # test to see if there are NO items in the MINOR sections
- # regex = [four digits][one letter][one or more spaces][one digit][literal .][two digits]
- if ($WorkingFile[$Counter] -match '\d{4}\w\s+\d\.\d{2}')
- {
- $WorkingFile[$Counter] = $WorkingFile[$Counter] -replace '(\d{4}\w)\s+(\d\.\d{2})', '$1,,,,,$2'
- }
- else
- {
- # nothing at this time since there is no data to test with
- }
- # replace multi-spaces with singletons
- # regex = [two or more spaces]
- $WorkingFile[$Counter] = $WorkingFile[$Counter] -replace '\s{2,}', ' '
- # trim any leading and/or trailing spaces
- $WorkingFile[$Counter] = $WorkingFile[$Counter].Trim()
- # replace any remaining spaces with a comma
- $WorkingFile[$Counter] = $WorkingFile[$Counter].Replace(' ',',')
- # undo the temporary underscore for "FName MiddleInitial" items
- # regex = [one or more letters][literal _][literal .][literal ,]
- $WorkingFile[$Counter] = $WorkingFile[$Counter] -replace '(\w+)_(\w\.\,)', '$1 $2'
- #debug info
- #$WorkingFile[$Counter]
- #
- $Counter ++
- }
- $OutFile = $WorkingFile | ConvertFrom-Csv
- $OutFile | Export-Csv -Path $DestFile -NoTypeInformation
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement