![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| Guest | More text processing. Hi, I'm trying to turn a .txt which contains thousands of lines of this:- DSC 50000 FLEN 5 DSP DN RRPA NO RLI 120 NPA NXX DSC 50001 FLEN 5 DSP DN RRPA NO RLI 140 NPA NXX DSC 50002 FLEN 5 DSP DN RRPA NO RLI 120 NPA NXX Into something that looks like this:- DSC RLI ------- ---- 50000 120 50001 140 50002 120 I already have a program to do this in Excel VBA but I like the idea of running it in PoSH. I've been playing around with where-object, select-object etc and have no problem reading in the file and iterating line by line doing something like foreach ($line in $file) { if $line ....} looks for matching patterns then writes it to a file. The problem is that I don't understand how to format the output into what I need. Invariably I end up with something that looks like :- DSC RLI ------- ----- 50000 120 50001 140 50002 120 Would be grateful for any pointers. Thanks, Stuart |
My System Specs![]() |
| | #2 (permalink) |
| Guest | Re: More text processing. #create an empty array to hold the objects $col = @() # get the data in seven-line chuncks according to sequence on data sample get-content data.txt -readCount 7 | % { # create a new object $obj = new-object psObject # use Switch to create custom properties for the object switch -regex ($_) { # if $_ matches 'DSC' create the DSC property and assign its value # by replacing al non-numeric characters '^DSC' { add-member noteProperty DSC $($_ -replace '\D*') -in $obj } # if $_ matches 'RLI' create the RLI property and assign its value # by replacing al non-numeric characters '^RLI' { add-member noteProperty RLI $($_ -replace '\D*') -in $obj } # if $_ does not match 'DSC' or 'RLI' skip it default { continue } } # add the object to the $col array $col += $obj } $col -- Kiron |
My System Specs![]() |
| | #3 (permalink) |
| Guest | Re: More text processing. Another way to solve this problem: gc a.txt | % { "DSC`tRLI"; "------`t-----" } { if ($_ -match '^DSC') { $out = [regex]::split($_, '\s+')[1]; } elseif ($_ -match '^RLI' ) { $out + "`t" + [regex]::split($_, '\s+')[1] } } Best wishes! Tao Ma |
My System Specs![]() |
| | #4 (permalink) |
| Guest | Re: More text processing. Awesome as always Kiron! It never occured to me to read in all seven lines then turn it into an array. Thanks muchly! Stuart -- Posted via a free Usenet account from http://www.teranews.com |
My System Specs![]() |
| | #5 (permalink) |
| Guest | Re: More text processing. Thanks Tao, Works perfectly! Best wishes to you too! Stuart -- Posted via a free Usenet account from http://www.teranews.com |
My System Specs![]() |
| | #6 (permalink) |
| Guest | Re: More text processing. "Kryten" <Kryten68@xxxxxx> wrote in message news:fd2969ca-ee58-48c3-8d66-3e2f1f7462bd@xxxxxx Quote: > Hi, > I'm trying to turn a .txt which contains thousands of lines of this:- > > DSC 50000 > FLEN 5 > DSP DN > RRPA NO > RLI 120 > NPA > NXX > DSC 50001 > FLEN 5 > DSP DN > RRPA NO > RLI 140 > NPA > NXX > DSC 50002 > FLEN 5 > DSP DN > RRPA NO > RLI 120 > NPA > NXX > > Into something that looks like this:- > > DSC RLI > ------- ---- > 50000 120 > 50001 140 > 50002 120 > feature: 27> function newObj {$obj = new-object psobject; $obj | add-member NoteProperty DSC '' -pass | add-member NoteProperty RLI '' -pass} 28> switch -regex -file .\foo.dat {'^DSC\s+(\d+)' {$obj = newObj; $obj.DSC = $matches[1]} '^RLI\s+(\d+)' {$obj.RLI = $matches[1];$obj}} DSC RLI --- --- 50000 120 50001 140 50002 120 -- Keith |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Text processing problem | PowerShell | |||
| MVP's, text to speech!!, Can I dictate in other text input program | Vista General | |||
| Howto: Add lines of text from a specific point in a text file.. | VB Script | |||
| Clear-Content Cmdlet and Processing Lines in Text File | PowerShell | |||
| How do I read a text file and sort text by fixed positions? | PowerShell | |||