Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get filename and line count per file using powershell

Tags:

powershell

I have the following powershell script to count lines per file in a given directory:

dir -Include *.csv -Recurse | foreach{get-content $_ | measure-object -line}

This is giving me the following output:

Lines    Words    Characters    Property
-----    -----    ----------    --------
   27
   90
   11
   95
  449
  ...

The counts-per-file is fine (I don't require words, characters, or property), but I don't know what filename the count is for.

The ideal output would be something like:

Filename        Lines
--------        -----
Filename1.txt      27
Filename1.txt      90
Filename1.txt      11
Filename1.txt      95
Filename1.txt     449
  ...

How do I add the filename to the output?

like image 397
Kevin Hogg Avatar asked Dec 12 '22 06:12

Kevin Hogg


1 Answers

try this:

dir -Include *.csv -Recurse | 
   % { $_ | select name, @{n="lines";e={
       get-content $_ | 
         measure-object -line |
             select -expa lines }
                                       } 
     } | ft -AutoSize
like image 90
CB. Avatar answered Jan 19 '23 00:01

CB.