Why does my tool output overwrite itself and how do I fix it?

前端 未结 3 2072
梦如初夏
梦如初夏 2020-11-21 15:04

The intent of this question is to provide an answer to the daily questions whose answer is \"you have DOS line endings\" so we can simply close them as duplicates of this on

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-21 15:29

    Run dos2unix. While you can manipulate the line endings with code you wrote yourself, there are utilities which exist in the Linux / Unix world which already do this for you.

    If on a Fedora system dnf install dos2unix will put the dos2unix tool in place (should it not be installed).

    There is a similar dos2unix deb package available for Debian based systems.

    From a programming point of view, the conversion is simple. Search all the characters in a file for the sequence \r\n and replace it with \n.

    This means there are dozens of ways to convert from DOS to Unix using nearly every tool imaginable. One simple way is to use the command tr where you simply replace \r with nothing!

    tr -d '\r' < infile > outfile
    

提交回复
热议问题