2

I need to create a file with Linux line endings in a Windows command script.

The closest I can get so far is this PowerShell one-liner:

powershell.exe -Command "Get-ChildItem -File "D:\path\file.sh" | % { $x = get-content -raw -path $_.fullname; $x -replace [char[]](13),'' | set-content -path $_.fullname }"

This is so close to what I want... the fly in the ointment is that it adds an extra CR+LF at the end of the file! Any suggestions for how I can fix this, or alternative techniques? I can't install any additional software such as Cygwin or dos2unix as this is for a locked-down production environment.

PhilHibbs
  • 205

2 Answers2

2

The most elegant solution I could find is:

((Get-Content $file) -join "`n") + "`n" | Set-Content -NoNewline $file

taken from here

adamency
  • 503
1

This could be what you need :

powershell.exe -noninteractive -NoProfile -ExecutionPolicy Bypass -Command "& {[IO.File]::WriteAllText('file_lfonly.txt', ([IO.File]::ReadAllText('file_crlf.txt') -replace \"`r`n\", \"`n\"))};"
Ramhound
  • 44,080
Marwen
  • 11
  • 1