Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UNIX format files with Powershell

How do you create a unix file format in Powershell? I am using the following to create a file, but it always creates it in the windows format.

"hello world" | out-file -filepath test.txt -append

As I understand, the new line characters CRLF make it to be a Windows format file whereas the unix format needs only a LF at the end of the line. I tried replacing the CRLF with the following, but it didn't work

"hello world" | %{ $_.Replace("`r`n","`n") } | out-file -filepath test.txt -append
like image 606
aldrin Avatar asked Feb 24 '11 08:02

aldrin


2 Answers

There is a Cmdlet in the PowerShell Community Extensions called ConvertTo-UnixLineEnding

like image 189
Andy Schneider Avatar answered Oct 11 '22 01:10

Andy Schneider


One ugly-looking answer is (taking input from dos.txt outputting to unix.txt):

[string]::Join( "`n", (gc dos.txt)) | sc unix.txt

but I would really like to be able to make Set-Content do this by itself and this solution does not stream and therefore does not work well on large files...

And this solution will end the file with a DOS line ending as well... so it is not 100%

like image 43
Anders Zommarin Avatar answered Oct 11 '22 00:10

Anders Zommarin