Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Parsing a test file

Closed Thread
 
Thread Tools Display Modes
Old 02-08-2007   #1 (permalink)
Paul Hinsberg
Guest


 

Parsing a test file

I need to parse a simple text file, get-content doesn't seem to provide a
straightforward way.

Consider contents of server.txt as :

ServerA,5
ServerB,6
ServerC,12


If I do FOR /F "tokens=* delims=," %I in (server.txt) do echo %I has a
value of %J
ServerA has a value of 5
ServerB has a value of 6
ServerC has a value of 12

So the variable %I is equal to the first part and %J equal to the second.
How do I accomplish this in PowerShell???

--
Paul Hinsberg
Old 02-08-2007   #2 (permalink)
Brandon Shell
Guest


 

Re: Parsing a test file

cat test.txt | %{Write-Host $_.Split(",")[0] "has value" $_.Split(",")[1]}

--
Brandon Shell
---------------
Stop by my blog some time
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject
--------------------------------------

"Paul Hinsberg" <paulhins(antispam)@comcast.net> wrote in message
newsE381330-2947-4518-AEC0-2AE05A4DFFCD@microsoft.com...
>I need to parse a simple text file, get-content doesn't seem to provide a
> straightforward way.
>
> Consider contents of server.txt as :
>
> ServerA,5
> ServerB,6
> ServerC,12
>
>
> If I do FOR /F "tokens=* delims=," %I in (server.txt) do echo %I has a
> value of %J
> ServerA has a value of 5
> ServerB has a value of 6
> ServerC has a value of 12
>
> So the variable %I is equal to the first part and %J equal to the second.
> How do I accomplish this in PowerShell???
>
> --
> Paul Hinsberg


Old 02-08-2007   #3 (permalink)
Sung M Kim
Guest


 

Re: Parsing a test file

This one-liner i have come up with is similiar to the batch scripting
version in concept;
This one should look fairly straight-forward to read(I have also
refrained myself from using aliases )

# retrieve content of "servers.txt"
# For each line in "servers.txt", split a string with a delimiter ","
# and assign splitted strings to $name and $value for print out

[^_^]PS[88]>(get-content servers.txt) | foreach { $name, $value =
$_.Split(","); "{0} has a value of {1}" -f $name, $value }
ServerA has a value of 5
ServerB has a value of 6
ServerC has a value of 12

Old 02-08-2007   #4 (permalink)
Keith Hill
Guest


 

Re: Parsing a test file


"Sung M Kim" <DontBotherMeWithSpam@gmail.com> wrote in message
news:1170964488.426864.160060@k78g2000cwa.googlegroups.com...
> This one-liner i have come up with is similiar to the batch scripting
> version in concept;
> This one should look fairly straight-forward to read(I have also
> refrained myself from using aliases )
>
> # retrieve content of "servers.txt"
> # For each line in "servers.txt", split a string with a delimiter ","
> # and assign splitted strings to $name and $value for print out
>
> [^_^]PS[88]>(get-content servers.txt) | foreach { $name, $value =
> $_.Split(","); "{0} has a value of {1}" -f $name, $value }
> ServerA has a value of 5
> ServerB has a value of 6
> ServerC has a value of 12


Yet another way is:

switch -regex -file foo.txt { '(.*),(.*)' { $matches[1] + " has value of " +
$matches[2] }}

--
Keith

Old 02-08-2007   #5 (permalink)
Brandon Shell
Guest


 

Re: Parsing a test file

Contest time... who can make it the shortest!
--
Brandon Shell
---------------
Stop by my blog some time
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject
--------------------------------------

"Keith Hill" <r_keith_hill@mailhot.nospamIdotcom> wrote in message
newsE417379-56EE-4C1E-9DEE-D3806570B155@microsoft.com...
>
> "Sung M Kim" <DontBotherMeWithSpam@gmail.com> wrote in message
> news:1170964488.426864.160060@k78g2000cwa.googlegroups.com...
>> This one-liner i have come up with is similiar to the batch scripting
>> version in concept;
>> This one should look fairly straight-forward to read(I have also
>> refrained myself from using aliases )
>>
>> # retrieve content of "servers.txt"
>> # For each line in "servers.txt", split a string with a delimiter ","
>> # and assign splitted strings to $name and $value for print out
>>
>> [^_^]PS[88]>(get-content servers.txt) | foreach { $name, $value =
>> $_.Split(","); "{0} has a value of {1}" -f $name, $value }
>> ServerA has a value of 5
>> ServerB has a value of 6
>> ServerC has a value of 12

>
> Yet another way is:
>
> switch -regex -file foo.txt { '(.*),(.*)' { $matches[1] + " has value of "
> + $matches[2] }}
>
> --
> Keith
>


Old 02-08-2007   #6 (permalink)
Marcel J. Ortiz [MSFT]
Guest


 

Re: Parsing a test file

If you its possible to add a header line you could use import-csv.

PS>type temp.txt
Server,Value
ServerA,5
ServerB,6
ServerC,12
PS>import-csv temp.txt

Server Value
------ -----
ServerA 5
ServerB 6
ServerC 12


PS>


"Sung M Kim" <DontBotherMeWithSpam@gmail.com> wrote in message
news:1170964488.426864.160060@k78g2000cwa.googlegroups.com...
> This one-liner i have come up with is similiar to the batch scripting
> version in concept;
> This one should look fairly straight-forward to read(I have also
> refrained myself from using aliases )
>
> # retrieve content of "servers.txt"
> # For each line in "servers.txt", split a string with a delimiter ","
> # and assign splitted strings to $name and $value for print out
>
> [^_^]PS[88]>(get-content servers.txt) | foreach { $name, $value =
> $_.Split(","); "{0} has a value of {1}" -f $name, $value }
> ServerA has a value of 5
> ServerB has a value of 6
> ServerC has a value of 12
>



Old 02-08-2007   #7 (permalink)
/\\/\\o\\/\\/ [MVP]
Guest


 

Re: Parsing a test file

> Contest time... who can make it the shortest!

ipcsv list.csv

;-)

add headers like this :

Name,Value
ServerA,5
ServerB,6
ServerC,12

PoSH> sc list.csv @'
>> Name,Value
>> ServerA,5
>> ServerB,6
>> ServerC,12
>> '@
>>

PoSH> ipcsv list.csv

Name Value
---- -----
ServerA 5
ServerB 6
ServerC 12

Greetings /\/\o\/\/

"Brandon Shell" <tshell.mask@gmail.com> wrote in message
news:OqUH247SHHA.5012@TK2MSFTNGP04.phx.gbl...
> Contest time... who can make it the shortest!
> --
> Brandon Shell
> ---------------
> Stop by my blog some time
> Blog: http://www.bsonposh.com/
> PSH Scripts Project: www.codeplex.com/psobject
> --------------------------------------
>
> "Keith Hill" <r_keith_hill@mailhot.nospamIdotcom> wrote in message
> newsE417379-56EE-4C1E-9DEE-D3806570B155@microsoft.com...
>>
>> "Sung M Kim" <DontBotherMeWithSpam@gmail.com> wrote in message
>> news:1170964488.426864.160060@k78g2000cwa.googlegroups.com...
>>> This one-liner i have come up with is similiar to the batch scripting
>>> version in concept;
>>> This one should look fairly straight-forward to read(I have also
>>> refrained myself from using aliases )
>>>
>>> # retrieve content of "servers.txt"
>>> # For each line in "servers.txt", split a string with a delimiter ","
>>> # and assign splitted strings to $name and $value for print out
>>>
>>> [^_^]PS[88]>(get-content servers.txt) | foreach { $name, $value =
>>> $_.Split(","); "{0} has a value of {1}" -f $name, $value }
>>> ServerA has a value of 5
>>> ServerB has a value of 6
>>> ServerC has a value of 12

>>
>> Yet another way is:
>>
>> switch -regex -file foo.txt { '(.*),(.*)' { $matches[1] + " has value of
>> " + $matches[2] }}
>>
>> --
>> Keith
>>

>


Old 02-08-2007   #8 (permalink)
Paul Hinsberg
Guest


 

Re: Parsing a test file

Sooo many squiggly brackets, so little time...

The import-csv looks cool, but how would you assign each value in the
perspective column to a variable? I am afraid the judges have ruled that
this is one the parameters for the contest. : )

--
Paul Hinsberg


"Marcel J. Ortiz [MSFT]" wrote:

> If you its possible to add a header line you could use import-csv.
>
> PS>type temp.txt
> Server,Value
> ServerA,5
> ServerB,6
> ServerC,12
> PS>import-csv temp.txt
>
> Server Value
> ------ -----
> ServerA 5
> ServerB 6
> ServerC 12
>
>
> PS>
>
>
> "Sung M Kim" <DontBotherMeWithSpam@gmail.com> wrote in message
> news:1170964488.426864.160060@k78g2000cwa.googlegroups.com...
> > This one-liner i have come up with is similiar to the batch scripting
> > version in concept;
> > This one should look fairly straight-forward to read(I have also
> > refrained myself from using aliases )
> >
> > # retrieve content of "servers.txt"
> > # For each line in "servers.txt", split a string with a delimiter ","
> > # and assign splitted strings to $name and $value for print out
> >
> > [^_^]PS[88]>(get-content servers.txt) | foreach { $name, $value =
> > $_.Split(","); "{0} has a value of {1}" -f $name, $value }
> > ServerA has a value of 5
> > ServerB has a value of 6
> > ServerC has a value of 12
> >

>
>
>

Old 02-08-2007   #9 (permalink)
/\\/\\o\\/\\/ [MVP]
Guest


 

Re: Parsing a test file

It already is ;-)
the $_ variable

PoSH> ipcsv list.csv |% {"$($_.name) = ";$_.value}
ServerA =
5
ServerB =
6
ServerC =
12

Greetings /\/\o\/\/

"Paul Hinsberg" <paulhins(antispam)@comcast.net> wrote in message
news:A183EB9F-68D6-4C31-98D6-65EBCA1D834E@microsoft.com...
> Sooo many squiggly brackets, so little time...
>
> The import-csv looks cool, but how would you assign each value in the
> perspective column to a variable? I am afraid the judges have ruled that
> this is one the parameters for the contest. : )
>
> --
> Paul Hinsberg
>
>
> "Marcel J. Ortiz [MSFT]" wrote:
>
>> If you its possible to add a header line you could use import-csv.
>>
>> PS>type temp.txt
>> Server,Value
>> ServerA,5
>> ServerB,6
>> ServerC,12
>> PS>import-csv temp.txt
>>
>> Server Value
>> ------ -----
>> ServerA 5
>> ServerB 6
>> ServerC 12
>>
>>
>> PS>
>>
>>
>> "Sung M Kim" <DontBotherMeWithSpam@gmail.com> wrote in message
>> news:1170964488.426864.160060@k78g2000cwa.googlegroups.com...
>> > This one-liner i have come up with is similiar to the batch scripting
>> > version in concept;
>> > This one should look fairly straight-forward to read(I have also
>> > refrained myself from using aliases )
>> >
>> > # retrieve content of "servers.txt"
>> > # For each line in "servers.txt", split a string with a delimiter ","
>> > # and assign splitted strings to $name and $value for print out
>> >
>> > [^_^]PS[88]>(get-content servers.txt) | foreach { $name, $value =
>> > $_.Split(","); "{0} has a value of {1}" -f $name, $value }
>> > ServerA has a value of 5
>> > ServerB has a value of 6
>> > ServerC has a value of 12
>> >

>>
>>
>>


Old 02-12-2007   #10 (permalink)
Techstarts
Guest


 

Re: Parsing a test file

Yeah I too was wondering when there is CSV file management made so easy why
don't we use them and what it requries to create CSV file.

Header makes life really easy.

http://Techstarts.blogspot.com

"/\/\o\/\/ [MVP]" <mow001@hotmail.NoSpam> wrote in message
news:268FEA86-7405-4936-AAD2-DEEC426C6F6C@microsoft.com...
>> Contest time... who can make it the shortest!

>
> ipcsv list.csv
>
> ;-)
>
> add headers like this :
>
> Name,Value
> ServerA,5
> ServerB,6
> ServerC,12
>
> PoSH> sc list.csv @'
>>> Name,Value
>>> ServerA,5
>>> ServerB,6
>>> ServerC,12
>>> '@
>>>

> PoSH> ipcsv list.csv
>
> Name
> Value
> ---- -----
> ServerA 5
> ServerB 6
> ServerC 12
>
> Greetings /\/\o\/\/
>
> "Brandon Shell" <tshell.mask@gmail.com> wrote in message
> news:OqUH247SHHA.5012@TK2MSFTNGP04.phx.gbl...
>> Contest time... who can make it the shortest!
>> --
>> Brandon Shell
>> ---------------
>> Stop by my blog some time
>> Blog: http://www.bsonposh.com/
>> PSH Scripts Project: www.codeplex.com/psobject
>> --------------------------------------
>>
>> "Keith Hill" <r_keith_hill@mailhot.nospamIdotcom> wrote in message
>> newsE417379-56EE-4C1E-9DEE-D3806570B155@microsoft.com...
>>>
>>> "Sung M Kim" <DontBotherMeWithSpam@gmail.com> wrote in message
>>> news:1170964488.426864.160060@k78g2000cwa.googlegroups.com...
>>>> This one-liner i have come up with is similiar to the batch scripting
>>>> version in concept;
>>>> This one should look fairly straight-forward to read(I have also
>>>> refrained myself from using aliases )
>>>>
>>>> # retrieve content of "servers.txt"
>>>> # For each line in "servers.txt", split a string with a delimiter ","
>>>> # and assign splitted strings to $name and $value for print out
>>>>
>>>> [^_^]PS[88]>(get-content servers.txt) | foreach { $name, $value =
>>>> $_.Split(","); "{0} has a value of {1}" -f $name, $value }
>>>> ServerA has a value of 5
>>>> ServerB has a value of 6
>>>> ServerC has a value of 12
>>>
>>> Yet another way is:
>>>
>>> switch -regex -file foo.txt { '(.*),(.*)' { $matches[1] + " has value of
>>> " + $matches[2] }}
>>>
>>> --
>>> Keith
>>>

>>

>



Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
File parsing on steroids needed - Part II Highlander VB Script 5 07-28-2008 11:15 AM
Parsing Text File and returning variables Mícheál PowerShell 4 04-18-2008 12:18 PM
Parsing a text file Marco Shaw PowerShell 4 07-05-2007 12:13 PM
How to test file-filter-driver in Driver Test Manager(DTM)? Cui Wei Vista General 8 01-18-2007 07:59 AM
Parsing a log file Koko PowerShell 2 11-21-2006 03:47 PM








Vistax64.com 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 2005-2008

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 47 48 49 50