I’m a Linux guy so PowerShell isn’t exactly my forte, but when I found myself on a Windows 10 box without my familiar tools, I had to improvise.
I was attempting to get a list of unique IP addresses from a text file. If I were on a *nix box, I’d issue the following command.
$ grep -iEo “[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}” <filename> | sort | uniq -c
Windows maintains findstr, but it’s severely limited. You can’t narrow the output to matched text only.
So if you’re searching for IP addresses, and a line of text contains one with some additional surrounding text, findstr will return the entire line. This isn’t helpful.
So I fired up PowerShell and issued the following command to get a similar output.
PS C:\Users\User\Desktop> $ips = get-content file.txt | select-string -pattern “[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}” | % {$_.matches} | % {$_.value} | group-object -noelement
PS C:\Users\User\Desktop> $ips
Count Name
—– —-
7 1.2.3.4
5 2.3.4.5
1 3.4.5.6