Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista Tutorial - More text processing.

Reply
 
Old 12-11-2007   #1 (permalink)
Kryten
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 SpecsSystem Spec
Old 12-11-2007   #2 (permalink)
Kiron
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 SpecsSystem Spec
Old 12-12-2007   #3 (permalink)
Tao Ma
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 SpecsSystem Spec
Old 12-12-2007   #4 (permalink)
Krayten
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 SpecsSystem Spec
Old 12-12-2007   #5 (permalink)
Krayten
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 SpecsSystem Spec
Old 12-17-2007   #6 (permalink)
Keith Hill [MVP]
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
>
Here's another solution that uses the very useful switch -regex -file
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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46