【问题标题】:Combine multiple files to one without headers将多个文件合并为一个没有标题
【发布时间】:2019-02-26 10:24:42
【问题描述】:

我是 Unix 脚本的新手,需要一些有关 Unix cmd 的信息 - 我们能否在一个文件中 cat 所有具有 abc_20190226_Part*.txt 之类的模式的文件,而在 unix 命令中没有它们的标题。

【问题讨论】:

  • 这是一个回复:是的,我们可以。
  • 除了玩笑,请更清楚,添加细节,展示你到现在为止的尝试。这是本网站的政策。
  • sed -s 1d abc*Part*txt > newFile怎么样
  • 谢谢马克,但 sed-s 给出了无效选项错误。虽然 sed 1D abc*part.txt 给了我连接,但它也只从第一个文件中删除标题。
  • 你是在 Mac 上吗?

标签: shell unix scripting


【解决方案1】:

首先是一些测试文件:

$ cat foo
11
12
13
$ cat bar
21
22
23

一种方法是使用tailman tail:

-n, --lines=[+]NUM
       output the last NUM lines, instead of the last 10; or use -n +NUM 
       to output starting  with  line  NUM

所以:

$ tail -n +2 foo
12
13

但是:

$ tail -n +2 foo bar
==> foo <==
12
13

==> bar <==
22
23

哦不,我们需要一个循环:

$ for f in foo bar ; do tail -n +2 "$f" ; done
12
13
22
23

另一种方法是使用awk:

GNU awk documentation 说:

FNR 
       FNR is the current record number in the current file. FNR is incremented 
       each time a new record is read (see section Explicit Input with getline). 
       It is reinitialized to zero each time a new input file is started.

所以:

$ awk 'FNR>1' foo bar
12
13
22
23

最后,需要将输出重定向到一个新文件(one_file),例如:

$ awk 'FNR>1' foo bar > one_file
$ cat one_file
12
13
22
23

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    • 2015-10-29
    • 1970-01-01
    • 2013-04-13
    • 2012-10-19
    • 2015-01-12
    相关资源
    最近更新 更多