Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 be:

One
Two
Three
like image 458
TrueWill Avatar asked Jul 11 '11 15:07

TrueWill


3 Answers

select-string -path <filepath> -pattern 'Path=\"(.*?)\"' -allmatches  |
  foreach-object {$_.matches} |
   foreach-object {$_.groups[1].value} |
    Select-Object -Unique
like image 106
mjolinor Avatar answered Nov 06 '22 21:11

mjolinor


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
like image 34
Shay Levy Avatar answered Nov 06 '22 21:11

Shay Levy


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!

like image 26
JPBlanc Avatar answered Nov 06 '22 22:11

JPBlanc