Sunday, December 2, 2007

Counting # of matching lines in file

I wanted to scan a large number XML files and determine which ones had more than 20 elements of a particular type. Here was the PowerShell command-line I used to report on files that contains more than 20 "" elements:

gci *.xml |% {$fn=$_.name; gc $_ | where {$_ -match ""} |% {$count = 0}{++$count}{if ($count -gt 20) {$fn}}}

An easier, but not very good for performance if a large number of files are involved is:

gci f_*.xml | select-string "" | group filename | where {$_.count -gt 20} | select count, name

No comments:

Can't RDP? How to enable / disable virtual machine firewall for Azure VM

Oh no!  I accidentally blocked the RDP port on an Azure virtual machine which resulted in not being able to log into the VM anymore.  I did ...