Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell: Why does Out-File break long line into smaller lines?

Tags:

powershell

Try this little experiment. Create a file Foo.txt with a very long line of text (say 500 chars long) like this:

// Foo.txt
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

Now issue the following command:

$ Get-Content Foo.txt | Select-String "a" | Out-File Foo2.txt

You will find that the long line of string has been broken down into smaller lines in Foo2.txt. The length of each smaller line is the same as the console width.

Why does Out-File break the long line into smaller line when the output is not headed to the console?

And why does Out-File not break down the lines for the following command?

$ Get-Content Foo.txt | Out-File Foo3.txt
like image 807
Ashwin Nanjappa Avatar asked Mar 02 '12 04:03

Ashwin Nanjappa


3 Answers

You can adjust where Out-File breaks lines using the -width parameter

$ Get-Content Foo.txt | Select-String "a" | Out-File -width 1000 Foo2.txt
like image 147
Polymorphix Avatar answered Oct 05 '22 22:10

Polymorphix


$ Get-Content Foo.txt | Select-String "a" | Add-Content Foo2.txt

Use Add-Content (or you could use Set-Content if you wish to overwrite the file).

like image 27
Tahir Hassan Avatar answered Oct 05 '22 22:10

Tahir Hassan


This can be explained by the fact that the result of Get-Content Foo.txt | Select-String "a" is of type MatchInfo, it's not a string.

just test :

Get-Content Foo.txt | Select-String "a" | Format-list *
like image 42
JPBlanc Avatar answered Oct 05 '22 22:10

JPBlanc