【问题标题】:Merge two csv files horizontally with different columns使用不同的列水平合并两个 csv 文件
【发布时间】:2014-05-26 20:23:20
【问题描述】:

我正在尝试水平合并两个 csv 文件。

我使用了这个命令:

paste -d ',' file1.csv file2.csv> results.csv

但它在第二行显示了与第二个 csv 输出的合并。像这样:

"Shares of Apple, Inc. near ex-dividend as it gears up to distribute $2.7 ...",Apple Inc. (AAPL),"May 8, 2014"

,0.125,0.0,pos

应该是这样的:

"Shares of Apple, Inc. near ex-dividend as it gears up to distribute $2.7 ...",Apple Inc. (AAPL),"May 8, 2014",0.125,0.0,pos

file1.csv

"Shares of Apple, Inc. near ex-dividend as it gears up to distribute $2.7 ...",Apple Inc. (AAPL),"May 8, 2014"
Apple Woos Dr. Dre With $3.2 Billion Offer for Beats to Fend Off Spotify as ...,Apple Inc. (AAPL),"May 8, 2014"

file2.csv

0.125,0.0,pos
0.0,0.75,neg

如何解决这个问题?

【问题讨论】:

  • 能否请您发布文件的第一行?
  • 您的输入文件可能有 CRLF,因为它在这里运行良好。
  • @alvits:合并的文件在行之间包含空格。

标签: shell csv


【解决方案1】:

是的@alvits。我使用以下方法将我的文件转换为 unix 文件:

 awk '{ gsub("\r", "\n"); print $0;}' macfile.txt > unixfile.txt

现在工作正常。

【讨论】:

    猜你喜欢
    • 2011-04-28
    • 2013-04-22
    • 2020-11-22
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-07
    相关资源
    最近更新 更多