文件如下所示(您可以看到它是逗号分隔的文件,其中每行的第一列是“团队”名称,后面跟着^M
):
Team,League,Games Played,At Bats,Runs,Hits,Doubles,Triples,Home Runs,RBI,Stolen Bases,Caught Stealing,Walks,Strike Outs,Intentional Walks,Hit By Pitch,Sacrifice Hits,Sacrifice Flies,Grounded Into Double Play,Batting Average,On Base Percentage,Slugging Percentage,Salary,Position,x,y,Player Name,Plate Appearences,Singles,Total Bases,Runs Created,ExpWS,Win Shares Percentage,Win Shares Above Bat,Win Shares,Career Win Shares,OnBase + Slugging^MDetroit,AL,86,343,58,116,20,2,6,36,4,3,21,16,0,8,2,4,9,0.338,0.386,0.461,4600000,2B,2.8,4,Placido Polanco,378,88,158,57.26455026,10,0.749,8,15,96,0.846^MNY Mets,NL,87,275,36,66,13,0,11,29,0,1,32,39,7,2,2,2,12,0.24,0.322,0.407,3750000,1B,3.6,3.5,Doug Mientkiewicz,313,42,112,35.06709265,9,0.195,-3,3,66,0.729^MDetroit,AL,87,334,48,107,15,4,5,23,2,3,24,45,3,2,0,1,9,0.32,0.368,0.434,4000000,SS,1.2,4,Carlos Guillen,361,83,145,52.61772853,9,0.418,1,8,79,0.803^MWashington,NL,87,309,38,85,21,2,7,32,0,0,31,30,3,1,2,4,9,0.275,0.339,0.424,7000000,2B,2.8,4,Jose Vidro,347,55,131,43.7925072,9,0.589,4,11,130,0.763^MNY Mets,NL,87,267,31,68,9,4,3,24,6,1,14,43,1,5,5,4,2,0.255,0.3,0.352,7033333,2B,2.8,4,Kazuo Matsui,295,52,94,26.12881356,8,0.294,-1,5,19,0.652^MArizona,NL,88,161,23,37,6,1,4,20,3
我如何使其成为一个普通文件,即我不认为这些行应该以 结尾^M
。
答案1
^M
是 0x15,又称回车符。您的行以 DOS 风格的 CRLF 对结尾,而不是 Unix 文本文件中使用的 LF。可以使用以下命令修复此问题tr
:
tr -d '\r' <dosfile >unixfile
包中还有fromdos
(和) 命令以及包中的(和) 命令。后两个命令比 更完整,它们还可以从 DOS/Windows 代码页进行转换(甚至可以从 Windows UTF-16 转换为 UTF-8)。todos
tofrodos
dos2unix
unix2dos
dos2unix
tr
答案2
可能的问题是该文件是在 Windows 系统上创建的。解决方法:
vi file
<ESC>
确保您处于命令模式
:%s/<CTRL>-V<CTRL>-M/\r/g
:wq
这会将每个 ^M 实例替换为换行符。
PS:我建议在编辑之前复制一份文件,以防出现问题。