![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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 | [regex]::Split help I have this file: PS C:\> gc test1.csv marco,111 shaw,222 I *only* want to ouput the 1st part of each line. In other words, I just want this to show in the output: marco shaw Tried this, but it doesn't quite work: PS C:\> gc test1.csv|foreach {[regex]::Split(($_),"`,")} marco 111 shaw 222 PS C:\> |
| | #2 (permalink) |
| Guest | RE: [regex]::Split help "Marco Shaw" wrote: > I have this file: > > PS C:\> gc test1.csv > marco,111 > shaw,222 > > I *only* want to ouput the 1st part of each line. In other words, I just > want this to show in the output: > > marco > shaw This is another example which shows that you often don't need regex for simple tasks: PS> gc test1.csv | foreach { $_.split(',')[0] } marco shaw Also note that if your csv file had a header, you could simply use import-csv cmdlet: PS> gc test1.csv name,value marco,111 shaw,222 PS> import-csv test1.csv name value ---- ----- marco 111 shaw 222 PS> import-csv test1.csv | % {$_.name} marco shaw -- greetings dreeschkind |
| | #3 (permalink) |
| Guest | Re: ::Split help I truly wish import-csv could handle csv files without headers. Maybe it could come up with default field names or something... > > Also note that if your csv file had a header, you could simply use > import-csv cmdlet: > > PS> gc test1.csv > name,value > marco,111 > shaw,222 > > PS> import-csv test1.csv > > name > value > ---- > ----- > marco 111 > shaw 222 > > PS> import-csv test1.csv | % {$_.name} > marco > shaw > > -- > greetings > dreeschkind |
| | #4 (permalink) |
| Guest | Re: ::Split help > I truly wish import-csv could handle > csv files without headers. Maybe > it could come up with default field > names or something... > Perhaps use Microsoft's Log Parser (for sure if the files are large): An TSV demo: PS> netstat -ano Active Connections Proto Local Address Foreign Address State PID TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 848 TCP 0.0.0.0:445 0.0.0.0:0 LISTENING 4 (...) See the headers shown above? PS> netstat -ano | >> LogParser "SELECT * FROM STDIN" -i:TSV ` >> -iSeparator:space -nSep:2 -fixedSep ff `>> -nSkipLines:3 >> Filename RowNumber Proto Local Address Foreign Address State PID -------- --------- ----- ----------------- --------------- --------- ------ STDIN 5 TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 848 STDIN 6 TCP 0.0.0.0:445 0.0.0.0:0 LISTENING 4 (...) Now to get rid of Log Parsers headers shown above just add -headers ffPS> netstat -ano | >> LogParser "SELECT * FROM STDIN" -i:TSV ` >> -iSeparator:space -nSep:2 -fixedSep ff `>> -nSkipLines:3 -headers ff>> STDIN 5 TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 848 STDIN 6 TCP 0.0.0.0:445 0.0.0.0:0 LISTENING 4 Now get the fields you _only_ want: PS> netstat -ano | >> LogParser "SELECT Proto, State, Pid ` >> From STDIN" -i:TSV -iSeparator:space ` >> -nSep:2 -fixedSep ff -nSkipLines:3 `>> -headers ff>> TCP LISTENING 848 TCP LISTENING 4 (...) A CSV file: PS> get-content testnames.csv | >> LogParser "SELECT * ` >> From STDIN" -i:CSV -headers ff>> STDIN 2 Bob Smith 123 Main Any The State 23456 STDIN 3 Mary Jones 333 Maple None What State 66545 STDIN 4 Gerry Who 345 Dead End Road Big City Small State 22434 PS> get-content testnames.csv | >> LogParser "SELECT City,State ` >> From STDIN" -i:CSV -headers ff>> Any The State None What State Big City Small State Statistics: ----------- Elements processed: 3 Elements output: 3 Execution time: 0.00 seconds Try other Log Parser options too! |
| | #5 (permalink) |
| Guest | Re: ::Split help On 10 Nov 2006 09:41:51 -0800, mgrogan@gia.edu wrote: >I truly wish import-csv could handle csv files without headers. Maybe >it could come up with default field names or something... Hi, This function should do what you need, with a little adaptation perhaps. I have put comments in it so it should be fairly easy to see what it's doing. function global:MyimportCSV{ param($NoHeaderFile) # Finds the number of columns in the file with no headers $line = get-content $NoHeaderFile -totalCount 1 $array = $line.Split(',') # Constructs a header line of the right length $HeaderLine = "" for($i = 0; $i -lt ($array.length)-1; $i++){ $HeaderLine += "Header($i)," } $HeaderLine += "Last Header" # Gets the content of the file with no headers $array = get-content $NoHeaderFile # Outputs the header line to Temp.csv set-content Temp.csv $HeaderLine # Adds the content of the file with no headers to Temp.csv foreach ($element in $array){ add-content Temp.csv $element } # Imports CSV as if the file with no headers had headers. import-csv Temp.csv } I am sure that there are other ways of approaching this but the preceding code does work for me. Andrew Watt MVP |
| | #6 (permalink) |
| Guest | Re: ::Split help If you use Parse-TextObject, this becomes swimmingly easy: gc test.csv | parse-textobject -delimiter ',' | select Property1 http://www.leeholmes.com/blog/parset...Vengeance.aspx -- Lee Holmes [MSFT] Windows PowerShell Development Microsoft Corporation This posting is provided "AS IS" with no warranties, and confers no rights. "Andrew Watt [MVP]" <SVGDeveloper@aol.com> wrote in message news:0msbl2pe6fujkkv4ams3bv9herfepqbhm1@4ax.com... > On 10 Nov 2006 09:41:51 -0800, mgrogan@gia.edu wrote: > >>I truly wish import-csv could handle csv files without headers. Maybe >>it could come up with default field names or something... > > Hi, > > This function should do what you need, with a little adaptation > perhaps. > > I have put comments in it so it should be fairly easy to see what it's > doing. > > function global:MyimportCSV{ > param($NoHeaderFile) > > # Finds the number of columns in the file with no headers > $line = get-content $NoHeaderFile -totalCount 1 > $array = $line.Split(',') > > # Constructs a header line of the right length > $HeaderLine = "" > for($i = 0; $i -lt ($array.length)-1; $i++){ > $HeaderLine += "Header($i)," > } > $HeaderLine += "Last Header" > > # Gets the content of the file with no headers > $array = get-content $NoHeaderFile > > # Outputs the header line to Temp.csv > set-content Temp.csv $HeaderLine > > # Adds the content of the file with no headers to Temp.csv > foreach ($element in $array){ > add-content Temp.csv $element > } > > # Imports CSV as if the file with no headers had headers. > import-csv Temp.csv > > } > > I am sure that there are other ways of approaching this but the > preceding code does work for me. > > Andrew Watt MVP |
| |
| |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| regex help | tonyr | PowerShell | 4 | 07-02-2008 02:13 PM |
| strange behavior from Regex.Split & myString.IndexOf | mad.scientist.jr | .NET General | 4 | 04-04-2008 02:06 AM |
| Regex Help | Christopher Robin | .NET General | 1 | 03-31-2008 02:02 PM |
| [Regex]'s split method | Jobbsy | PowerShell | 17 | 11-26-2007 06:57 PM |
| Where are things like -replace and split([regex} documented? | =?Utf-8?B?RXJzdHdoaWxlSUlJ?= | PowerShell | 2 | 07-25-2006 10:59 AM |