'^M' character at end of lines

2019-01-04 09:23发布

When I run a particular SQL script in Unix environments, I'm am seeing a '^M' character at the end of each line of the SQL script as it is echoed to the command-line. I don't know on which OS the SQL script was originally created.

What is causing this and how do I fix it?

16条回答
霸刀☆藐视天下
2楼-- · 2019-01-04 09:44

The SQL script was originally created on a Windows OS. The '^M' characters are a result of Windows and Unix having different ideas about what to use for an end-of-line character. You can use perl at the command line to fix this.

perl -pie 's/\r//g' filename.txt
查看更多
迷人小祖宗
3楼-- · 2019-01-04 09:44

In Perl, if you don't want to set the $/ variable and use chomp() you can also do:

$var =~ /\r\n//g;

My two cents

查看更多
Explosion°爆炸
4楼-- · 2019-01-04 09:46

It's caused by the DOS/Windows line-ending characters. Like Andy Whitfield said, the Unix command dos2unix will help fix the problem. If you want more information, you can read the man pages for that command.

查看更多
一纸荒年 Trace。
5楼-- · 2019-01-04 09:50

Convert DOS/Windows (\r\n) line endings to Unix (\n) line endings, with tr:

tr '\r\n' '\n' < dosFile.txt > unixFile.txt

Post about replacing newlines from the Unix command line

查看更多
登录 后发表回答