【问题标题】:tail multiple CSV files to new file?将多个 CSV 文件拖尾到新文件?
【发布时间】:2016-02-06 19:16:06
【问题描述】:

我有 2 个文件:testfile1.csv 和 testfile2.csv。

testfile2.csv:

time,topic3,topic4
2015-10-01,40,50
2015-10-02,45,55
country,uk,uk

testfile1.csv:

time,topic1,topic2
2015-10-01,20,30
2015-10-02,25,35
country,usa,usa

我想将它们组合成 1 个大文件,测试文件 1 被复制并且只有 1 个标题,所以我这样做了:

cat test1/testfile1.csv > testfile3.csv
tail -n +2 test1/testfile1.csv  test2/testfile2.csv >> testfile3.csv 

除了显示哪组数据来自哪个文件的指示符之外,输出看起来正确:

time,topic1,topic2
2015-10-01,20,30
2015-10-02,25,35
country,usa,usa
==> test1/testfile1.csv <==
2015-10-01,20,30
2015-10-02,25,35
country,usa,usa

==> test2/testfile2.csv <==
2015-10-01,40,50
2015-10-02,45,55
country,uk,uk

如何删除指标?我写错了tail -n + 2 部分吗? 当我使用tail -n +2 test2/testfile2.csv &gt;&gt; testfile2.csv 时,输出看起来不错,但我不想逐个文件手动执行。

预期输出:

time,topic1,topic2
2015-10-01,20,30
2015-10-02,25,35
country,usa,usa
2015-10-01,20,30
2015-10-02,25,35
country,usa,usa
2015-10-01,40,50
2015-10-02,45,55
country,uk,uk

【问题讨论】:

    标签: shell unix cat tail


    【解决方案1】:

    来自手册(man tail):

         -q      Suppresses printing of headers when multiple files are being examined.
    

    因此,命令将是:

    tail -qn +2 test1/testfile1.csv  test2/testfile2.csv >> testfile3.csv 
    

    【讨论】:

    • 啊,我明白了。感谢-q的解释。
    猜你喜欢
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-11
    • 2017-07-08
    相关资源
    最近更新 更多