Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count number of rows in PowerShell

Tags:

powershell

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?

like image 373
Chris Muench Avatar asked Jan 20 '12 03:01

Chris Muench


People also ask

How do I count files in PowerShell?

If you want to count the files and folders inside that directory, run this command: (Get-ChildItem | Measure-Object). Count.

What does += in PowerShell mean?

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).

Can you grep in PowerShell?

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.

What is the PowerShell equivalent of grep?

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.


1 Answers

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.

like image 198
Aaron Avatar answered Sep 20 '22 21:09

Aaron