How to convert Windows end of line in Unix end of line (CR/LF to LF)

后端 未结 8 718
臣服心动
臣服心动 2020-11-29 18:59

I\'m a Java developer and I\'m using Ubuntu to develop. The project was created in Windows with Eclipse and it\'s using the Windows-1252 encoding.

To convert to UTF-8

相关标签:
8条回答
  • 2020-11-29 19:41

    sed cannot match \n because the trailing newline is removed before the line is put into the pattern space but can match \r, so you can convert \r\n (dos) to \n (unix) by removing \r

    sed -i 's/\r//g' file
    

    Warning: this will change the original file

    However, you cannot change from unix EOL to dos or old mac (\r) by this. More readings here:

    How can I replace a newline (\n) using sed?

    0 讨论(0)
  • 2020-11-29 19:42

    I'll take a little exception to jichao's answer. You can actually do everything he just talked about fairly easily. Instead of looking for a \n, just look for carriage return at the end of the line.

    sed -i 's/\r$//' "${FILE_NAME}"
    

    To change from unix back to dos, simply look for the last character on the line and add a form feed to it. (I'll add -r to make this easier with grep regular expressions.)

    sed -ri 's/(.)$/\1\r/' "${FILE_NAME}"
    

    Theoretically, the file could be changed to mac style by adding code to the last example that also appends the next line of input to the first line until all lines have been processed. I won't try to make that example here, though.

    Warning: -i changes the actual file. If you want a backup to be made, add a string of characters after -i. This will move the existing file to a file with the same name with your characters added to the end.

    0 讨论(0)
提交回复
热议问题