Tuesday, March 12, 2013

Line/Word Counting Files with Powershell



Line Counting a file
(gc ./myfile.txt | measure -line).Count

Word Counting a file
(gc ./myfile.txt | measure -word).Count

Character Counting a file
(gc ./myfile.txt | measure -char).Count

White Character Count for a file
(gc ./myfile.txt | % { [char[]]$_ | ? { $_ -match ‘[\s]’ } } | measure).Count

Black Character Count for a file
(gc ./myfile.txt | % { [char[]]$_ | ? { $_ -match ‘[\S]’ } } | measure).Count

Line counting many files (recurse from curdir) and getting the Sum
                (ls -recurse *.hl7 | % { (gc $_ | measure –line).Lines } | measure –sum).Sum

Line counting many files (recurse from curdir) and getting the Average
                (ls -recurse *.hl7 | % { (gc $_ | measure –line).Lines } | measure –average).Average

Count all lines containing “error” or “Error” in all log files from curdir down and getting the Sum
                (ls -recurse *.log | % { (gc $_ | ? {$_ -match ‘[Ee]rror’ } | measure –line).Lines } | measure –average).Sum

No comments: