Parse unstructered text file

I have text file generated every day as follow:

Column1                       Column2      Column With Whitespace    Column4

Data1                            Data 2          Data 3                           Data with whitespace

Data with whitespace      Data 2          Data 3                           Data

As you can see I have only column names fixed and data can be one word or combination of words with blank space. Some of my fixed column names also combines two or three words. (see 3rd column)

Any chance I can parse this data and create a hash table? I couldn`t find a logic to parse it. 

September 1st, 2015 5:24am

Will it work for you?

$Content = Get-Content -Path C:\test\parse.txt
$Ccontent = $C.split("`n")
foreach ($cont in $Content) {
$cont = $cont -Replace "  * ",","
$cont | where {$_}
}

I have result:

Column1,Column2,Column With Whitespace,Column4
Data1,Data 2,Data 3,Data with whitespace
Data with whitespace,Data 2,Data 3,Data

Free Windows Admin Tool Kit Click here and download it now
September 1st, 2015 8:43am

looks great. can I create a html table from this cont object?
September 1st, 2015 9:56am

Use substring.

Fixed width text output is best manipulated by substring it.

Get-Content $file |
    ForEach-Object{
         $col1=$_.SubString(0,10)
         $col2=$_.SubString(11,15)
        New-object PsObject -Property @{Column1=$col1;Column2=$col2}
   }

This pattern can convert any fixed width

Free Windows Admin Tool Kit Click here and download it now
September 1st, 2015 9:56am

$Content = Get-Content -Path C:\test\parse.txt
$Ccontent = $C.split("`n")
$C = foreach ($cont in $Content) {
$cont = $cont -Replace "  * ",","
$cont | where {$_}
}
$C | Out-File C:\test\parse.csv
Import-Csv C:\test\parse.csv | ConvertTo-Html | Set-Content C:\test\parse.html

September 1st, 2015 10:27am

thx worked.
Free Windows Admin Tool Kit Click here and download it now
September 1st, 2015 11:36am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics