bash 从 csv 文件中删除 ^M
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29473520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
removing ^M from a csv file
提问by Enric Agud Pique
I have a problem when I try to remove ^M from a csv
当我尝试从 csv 中删除 ^M 时遇到问题
When I type vim or vi file.csv, I get
当我输入 vim 或 vi file.csv 时,我得到
A, TK,2015-04-06,14.4^M,14.7,10.0,0.0,54.0^M,13.3^M,135.0^M,33.8
B, NV,2015-04-06,14.4^M,14.7,5.4,0.0,47.0^M,14.8^M,97.0^M,31.3
I have tried with
我试过
tr -d '^M' < file.csv > file2.csv
But it doesn't remove, also with sed.
但它不会删除,也不会删除。
回答by Idriss Neumann
You could use dos2unix
command which is provided to do that.
您可以使用dos2unix
提供的命令来执行此操作。
Using GNU/sed just for fun :
使用 GNU/sed 只是为了好玩:
sed -i -e "s/\r//g" file
Using tr
:
使用tr
:
tr -d '\r' <file1 >file2
回答by David W.
Where did you get that file from? It looks like an old System X file from a Mac. The old pre-OSX OS used <CR>
as line endings. Unix uses <LF>
, and Windows/DOS uses <CRLF>
.
你从哪里得到那个文件?它看起来像来自 Mac 的旧 System X 文件。旧的 OSX 之前的操作系统用作<CR>
行尾。Unix 使用<LF>
,Windows/DOS 使用<CRLF>
.
Do you have dos2unix. This program can convert line endings from Unix/Linux, DOS/Windows, or System X Macs to any of the formats you want. In your file, I take it you need to convert the ^M
which are Control Msand not a Caret-Mto NL
characters.
你有DOS2UNIX的。该程序可以将 Unix/Linux、DOS/Windows 或 System X Mac 的行尾转换为您想要的任何格式。在您的文件,我想你需要转换的^M
它们控制小姐而不是插入符-M ,以NL
字符。
回答by jm666
You can try:
你可以试试:
perl -pE 's/(\^M|\r)//g' < file >file2
should remove
应该删除
- the literal
^M
- sequence of two characters^
andM
- and the
^M
as\r
character.
- 在文字
^M
-两个字符序列^
和M
- 和
^M
as\r
字符。