Convert DOS line endings to Linux line endings in

2019-01-01 09:30发布

If I open files I created in Windows, the lines all end with ^M.
How do I delete these characters all at once?

26条回答
十年一品温如言
2楼-- · 2019-01-01 10:01

Convert directory of files from dos to Unix

Using command line and sed, find all files in current directory with the extension ".ext" and remove all "^M"

@ https://gist.github.com/sparkida/7773170

find $(pwd) -type f -name "*.ext" | while read file; do sed -e 's/^M//g' -i "$file"; done;

also, as mentioned above ^M = Ctrl+V + Ctrl+M (don't just type the caret "^" symbol and M)

查看更多
几人难应
3楼-- · 2019-01-01 10:02

if you create a file in NotePad or NotePad ++ in windows and bring it to Linux and open it by vim, you will see ^M at the end of each line. To remove this,

At your Linux terminal, type

dos2unix filename.ext

This will do the required magic.

查看更多
柔情千种
4楼-- · 2019-01-01 10:04

In vim, type:

:w !dos2unix %

This will pipe the contents of your current buffer to the dos2unix command and write the results over the current contents. Vim will ask to reload the file after

查看更多
几人难应
5楼-- · 2019-01-01 10:04

This is my way. I opened a file in dos EOL and when I save the file that will automatically convert to unix EOL

autocmd BufWrite * :set ff=unix
查看更多
爱死公子算了
6楼-- · 2019-01-01 10:05

Following steps can convert the file format for dos to unix:

:e ++ff=dos  Edit file again, using dos file format ('fileformats' is ignored).[A 1]
:setlocal ff=unix    This buffer will use LF-only line endings when written.[A 2]
:w   Write buffer using unix (LF-only) line endings.

Reference: http://vim.wikia.com/wiki/Change_end-of-line_format_for_dos-mac-unix

查看更多
荒废的爱情
7楼-- · 2019-01-01 10:06

Though this topic is very old, I'd like to put another stuff from wikia:

%s/\r\+$//g

that fill find all carriage return signs (one and more reps) up to the end of line and delete, so just \n will stay at eol.

查看更多
登录 后发表回答