Home > Mobile >  In Powershell read multiple files and count the no. of lines in each file. Then write all this infor
In Powershell read multiple files and count the no. of lines in each file. Then write all this infor

Time:01-04

I need to read multiple files and count the no. of lines in each file. Then write all this information in a single CSV file with the file path information.

I could write a code (with help from forums) that will help to read all files and print the information as required.

Script:

Get-ChildItem -Path C:\Logs\test *.log | Select-Object -Property FullName,@{Name="LineCount";$Expression={@(Get-Content -Path $_.FullName).Length}} 

Output of script:

FullName                    LineCount
--------                    ---------
C:\Logs\test\ANCD065.log          296
C:\Logs\test\ANCE066.log          287

Problems:

  1. The problem is not able to write the above information in a CSV file.
Write "what parameter need pass not clear to me" | Out-File C:\Log\OP.CSV
  1. The script reads all lines, is there a way to start reading line after certain lines? An example in the given file need is count lines from line 9 (start) to the end(could be 300).
1.     3.03           MET DATA                                RINEX VERSION / TYPE
2. cnvtToRINEX 3.14.0  convertToRINEX OPR  20220511 072939 UTC PGM / RUN BY / DATE 
3. ----------------------------------------------------------- COMMENT            
4. XYXTY3                                                       MARKER NAME         
5. UBWTZ3                                                       MARKER NUMBER       
6.     3    PR    TD    HR                                    # / TYPES OF OBSERV 
7.  0083019.4060  0025010.0967  0253356.6176        0.0000 PR SENSOR POS XYZ/H    
8.                                                            END OF HEADER      
9. 19 03 02 00 00 00  946.0    8.5   93.0
10. 19 03 02 00 05 00  946.0    8.4   93.4
11. 19 03 02 00 10 00  946.0    8.4   93.4
12. 19 03 02 00 15 00  946.0    8.4   94.2
13. 19 03 02 00 20 00  946.0    8.5   93.1
14. 19 03 02 00 25 00  946.1    8.7   90.2
15. 19 03 02 00 30 00  946.2    8.4   92.0
16. 19 03 02 00 35 00  946.3    8.3   93.2

CodePudding user response:

The problem is not able to write the above information in a CSV file.

This should be as simple as appending an Export-Csv command to the pipeline:

Get-ChildItem -Path C:\Logs\test *.log | 
    Select-Object -Property @(
        'FullName'
         @{ Name = "LineCount"; Expression = { 
             @(Get-Content -Path $_.FullName).Length
         }} 
    ) |
    Export-Csv output.csv -NoTypeInformation

Note that my Select-Object changes are just for code readability.

The script reads all lines, is there a way to start reading line after certain lines? An example in the given file need is count lines from line 9 (start) to the end(could be 300).

You may use Select-Object -Skip n to skip given number of lines from the beginning. Another improvement is to use Measure-Object to count number of items. In your current code, you have to read the whole log file into an array in memory, before being able to count lines. With Measure-Object, the file will be streamed, requiring much less memory for processing.

(Get-Content -Path $_.FullName | Select-Object -Skip 8 | Measure-Object).Count

Complete solution:

Get-ChildItem -Path C:\Logs\test *.log | 
    Select-Object -Property @(
        'FullName'
         @{ Name = "LineCount"; Expression = { 
             (Get-Content -Path $_.FullName | Select-Object -Skip 8 | Measure-Object).Count
         }} 
    ) |
    Export-Csv output.csv -NoTypeInformation
  • Related