Let's say I have a file with a following content:
1,first_string,somevalue
2,second_string,someothervalue
n,n_nd_string,somemorevalue
I need to Get-Content of this file, get the last string and get the number before the "," symbol (n in this case). (I'll just increment it and append n+1 string to this file, but it does not matter right now). I want all this stuff be done with pipeline cascade
I have come to this solution so far:
[int]$number = Get-Content .\1.mpcpl | Select-Object -Last 1 | Select-String -Pattern '^(\d)' | ForEach-Object {$_.matches.value}
It actually works, but I wonder if there are any ways of addressing Select-String -Pattern '^(\d)' return object without using the foreach loop? Beacause I know that the return collection in my case will only consist of a 1 element (I'm selecting a single last string of a file and I get only one match)
-matchoperator to get the first match only. Or-replace:-replace '^(\d).*', '$1'