开发者

Select-String to grep but just return unique groups

I have a text file that contains very long lines. I need one piece of information from each line, and need to see the unique values. My original thought was to use Select-String and specify a regular expression with a capture group. I've looked at several other posts but none worked. Here's the quick-and-dirty C# equivalent:

var text = File.ReadAllText(@"path\File.txt");
var r = new Regex("Path=\"(.*?)\"");
var matches = r.Matches(text);

var h = new HashSet<string>();

foreach(Match match in matches)
{
    h.Add(match.Groups[1].Value);
}

foreach (var s in h)
{
    Console.WriteLine(s);
}

How can I do this in PowerShell?

UPDATE:

Testing the answers, I realized there's an additional requirement. There can be multiple matches per source line. Example:

Path="One" Path="Two"
Path="Two" Path="Three"

Results should 开发者_如何学Cbe:

One
Two
Three


select-string -path <filepath> -pattern 'Path=\"(.*?)\"' -allmatches  |
  foreach-object {$_.matches} |
   foreach-object {$_.groups[1].value} |
    Select-Object -Unique


If I'm following you:

Get-Content file.txt | Foreach-Object { [regex]::match($_,'Path="(.*?)"').Groups[1].Value} | Select-Object -Unique

UPDATE:

PS > Select-String -Path file.txt -Pattern 'Path="([^"]+)"' -AllMatches | Select-Object -ExpandProperty Matches | Foreach-Object {$_.Groups[1].Value} | Select-Object -Unique

One
Two
Three


According to your comments :

${c:\silogix\t.txt} | % {[regex]::matches($_, 'Path="(.*?)"')} | % {$_.Groups[1].value} | Select-Object -Unique

Be careful: ${file-path} reads the file like Get-Content, but file-path must be absolute!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜