I have a text file that I need to read in PowerShell.
The last character in each row is either a Y or a N.
I need to go through the file and output how many Y's and N's are in there.
Any ideas?
If you want to count the files and folders inside that directory, run this command: (Get-ChildItem | Measure-Object). Count.
The assignment by addition operator += either increments the value of a variable or appends the specified value to the existing value. The action depends on whether the variable has a numeric or string type and whether the variable contains a single value (a scalar) or multiple values (a collection).
Grep is used in Linux to search for regular expressions in text strings and files. There's no grep cmdlet in PowerShell, but the Select-String cmdlet can be used to achieve the same results. The Windows command line has the findstr command, a grep equivalent for Windows.
For PowerShell, we can use the grep equivalent Select-String . We can get pretty much the same results with this powerful cmdlet. Select-String uses just like grep regular expression to find text patterns in files and strings.
Assuming a file named "test.txt"... To get the number of lines ending in Y, you can do this:
get-content test.txt | select-string Y$ | measure-object -line
And to get the number of lines ending in N, you can do this:
get-content test.txt | select-string N$ | measure-object -line
Hope that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With