Avoid line breaks when using out-file

前端 未结 2 1398
刺人心
刺人心 2021-01-07 22:27

I\'m getting a little frustrated on a little PowerShell script I\'m writing.

Basically I loop through text files to check every line against an array of regular expr

相关标签:
2条回答
  • 2021-01-07 22:47

    Keep in mind that Select-String outputs MatchInfo objects and not strings - as is shown by this command:

    gci $logdir -r *.txt | gc | select-string $patterns | format-list *
    

    You are asking for an implicit rendering of the MatchInfo object to string before being output to file. For some reason I don't understand, this is causing additional blank lines to be output. You can fix this by specifying that you only want the Line property output to the file e.g.:

    gci $logdir -r *.txt | gc | select-string $patterns | %{$_.Line} | 
        Out-File $csvpath -append -width 1000
    
    0 讨论(0)
  • 2021-01-07 22:51

    Why don't you use Add-Content?

    gci $logdir -rec *.txt | gc | select-string $pattern | add-content $csvpath
    

    You don't need to specify the width and -append switch, the file size is not doubled by default (although you can specify encoding) and it seems that there is no problem with the empty lines like you have.

    0 讨论(0)
提交回复
热议问题