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 09:56

I knew I'd seen this somewhere. Here is the FreeBSD login tip:

Need to remove all those ^M characters from a DOS file? Try

tr -d \\r < dosfile > newfile
    -- Originally by Dru <genesis@istar.ca>
查看更多
浪荡孟婆
3楼-- · 2019-01-01 09:56

:set fileformat=unix to convert from dos to unix.

查看更多
姐姐魅力值爆表
4楼-- · 2019-01-01 09:57

below command is used for reformat all .sh file in current directory, I tested it on my Fedora OS.

for file in *.sh; do awk '{ sub("\r$", ""); print }' $file >luxubutmp; cp -f luxubutmp $file; rm -f luxubutmp ;done
查看更多
后来的你喜欢了谁
5楼-- · 2019-01-01 09:57

I wanted newlines in place of the ^M's. Perl to the rescue:

perl -pi.bak -e 's/\x0d/\n/g' excel_created.txt

Or to write to stdout:

perl -p -e 's/\x0d/\n/g' < excel_created.txt
查看更多
墨雨无痕
6楼-- · 2019-01-01 10:00

I prefer to use the following command :

:set fileformat=unix

You can also use mac or dos to respectively convert your file to macintosh or MS-DOS/MS-Windows file convention. And it does nothing if the file is already in the correct format.

For more information, see the vim help :

:help fileformat
查看更多
登录 后发表回答