Search and count string occurrences in files using PowerShellβs Select-String, with examples for CSV parsing, pattern matching, and line/word counting.
Display occurences
Display occurences of lines ending with β,2018β in all CSV of a folder
select-string *.csv -pattern ",2018$"Display occurences of β{a word},{some digits}β in all CSV files in a folder and its subfolders (recursive version)
select-string -rec *.csv -pattern "\w+,\d+"Count occurences
Template to count occurences
(Get-Content my_file | select-string -pattern "my string").length
(gc my_file | select-string -pattern "my string").length # gc is a Get-Content aliasCount the number of functions in a PHP Class file
(gc MyClass.php | select-string -pattern " function ").lengthCount the number of function is all Java files in the actual folder
(gc *.php | select-string -pattern " function ").lengthCount Lines, Characters & Words
gc file.xml | Measure-Object βLine # Count Lines
gc file.xml | Measure-Object βCharacter # Count Characters
gc file.xml | Measure-Object βWord # Count Wordshead & tail
gc file.xml -head 10 # first 10 lines (head -n 10)
gc file.xml -tail 10 # last 10 lines (tail -n 10)
gc file.xml -tail 10 -wait # last 10 lines and keep open (tail -f)
[char[]](gc file.xml -Encoding byte -TotalCount 200) -join '' # first 200 char (head -c 200)