![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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 |
| | #2 (permalink) |
| 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 news E381330-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 |
| | #3 (permalink) |
| 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 |
| | #4 (permalink) |
| 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 |
| | #5 (permalink) |
| 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 news E417379-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 > |
| | #6 (permalink) |
| 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 > |
| | #7 (permalink) |
| 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 > news E417379-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 >> > |
| | #8 (permalink) |
| 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 > > > > > |
| | #9 (permalink) |
| 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 >> > >> >> >> |
| | #10 (permalink) |
| 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 >> news E417379-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 >>> >> > |
| |
| |
![]() |
| 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 |