![]() |
|
|
|
#1 |
|
Guest
Posts: n/a
|
select-string and .split
Hello everyone, I am still getting my head around Powershell, I love it but it is giving me a headache too! When using select-string .split does not appear possible as the returned type is a MatchInfo type. Is it possible to convert this to a string so .split works on it? Or some other method. To show the problem I'm having at the moent : If there is a file test.txt containing : one,1 two,2 three,3 four,4 I would like to find a specific line in the file and manipulate the data. Example of what I can and can not get to work : Write-Host "Example 1" Write-Host "=========" $line2 = "two,2" write-host $line2 $splitline2 = $line2.split(",") write-host $splitline2[1] Write-Host "Example 2" Write-Host "=========" $line1 = select-string D:\test.txt -pattern "two" -list write-host $line1 $splitline1 = $line1.split(",") write-host $splitline1[1] Example 1 ========= two,2 2 Example 2 ========= D:\test.txt:2:two,2 Method invocation failed because [Microsoft.PowerShell.Commands.MatchInfo] doesn't contain a method named 'split'. At D:\test.ps1:13 char:27 + $splitline1 = $line1.split( <<<< ",") Cannot index into a null array. At D:\test.ps1:14 char:24 + write-host $splitline1[1 <<<< ] Any suggestions greatfuly received! Many thanks, Alastair. Advertisement |
|
|
#2 |
|
Guest
Posts: n/a
|
Re: select-string and .split
> $splitline1 = $line1.split(",")
Try this: $splitline1 = $line1.tostring().split(",") Marco -- Microsoft MVP - Windows PowerShell http://www.microsoft.com/mvp PowerGadgets MVP http://www.powergadgets.com/mvp Blog: http://marcoshaw.blogspot.com |
|
|
#3 |
|
Guest
Posts: n/a
|
Re: select-string and .split
On 12 Oct, 15:23, "Marco Shaw [MVP]" <marco.shaw@_NO_SPAM_gmail.com>
wrote: > > $splitline1 = $line1.split(",") > > Try this: > $splitline1 = $line1.tostring().split(",") > > Marco > > -- > Microsoft MVP - Windows PowerShellhttp://www.microsoft.com/mvp > > PowerGadgets MVPhttp://www.powergadgets.com/mvp > > Blog:http://marcoshaw.blogspot.com Thanks for the quick reply! |
|
|
#4 |
|
Guest
Posts: n/a
|
Re: select-string and .split
Advertisement The MatchInfo object has a Line property, this is the line found in the file that matched the pattern. By using the -list switch Select-String outputs the first line found in a file or, when searching in multiple files, the first line matched in each file.
Since you are searching in one file Select-String returns a [matchInfo]. So you can do: ((select-string test1.txt -pattern "two" -list).line).split(',')[1] # ...or, assign the line to a variable a split it $line = (select-string test1.txt -pattern "two" -list).line $line.split(',')[1] -- Kiron Advertisement |
| Thread Tools | |
| Display Modes | |
|
|
< Home - Windows Help - MS Office Help - Hardware Support >
| New To Site? | Need Help? |