how to convert a file from DOS to Unix [closed]

不羁的心 提交于 2019-12-02 05:28:18

问题


I need to convert a file from DOS to Unix in PowerShell.

I know this can be very much easily done in Unix:

dos2unix file newfile

回答1:


It could be as simple as

Get-Content in.csv -raw | % {$_ -replace "`r", ""} | Set-Content -NoNewline out.csv

Above method will work on powershell version 3+. If you are below that you can use below method instead. Almost same as one of the other answers here.

$csvdata = [io.file]::ReadAllText('in.csv') | % {$_ -replace "`r",""}
[io.file]::WriteAllLines('out.csv', $csvdata)



回答2:


Try this:

$txt = (Get-Content -Raw myFile.txt) -replace "`r`n","`n"
[io.file]::WriteAllText('c:\path\to\myFile.unix.txt', $txt)

I used WriteAllText rather than Set-Content because Set-Content adds a carriage return and a line feed to the end of the file, and there doesn't seem to be any way to suppress that.

I think that the -Raw parameter to Get-Content might be a PowerShell 3 thing, so if you are using an earlier version, you might need to do it like this:

$txt = [io.file]::ReadAllText('c:\path\to\myFile.txt') -replace "`r`n","`n"
[io.file]::WriteAllText('c:\path\to\myFile.unix.txt', $txt)


来源:https://stackoverflow.com/questions/32063968/how-to-convert-a-file-from-dos-to-unix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!