bash Unix:合并多个文件,同时删除所有文件的第一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10103619/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:55:24  来源:igfitidea点击:

Unix: merge many files, while deleting first line of all files

bashmerge

提问by Abdel

I have >100 files that I need to merge, but for each file the first line has to be removed. What is the most efficient way to do this under Unix? I suspect it's probably a command using catand sed '1d'. All files have the same extension and are in the same folder, so we probably could use *.extension to point to the files. Many thanks!

我有超过 100 个文件需要合并,但对于每个文件,必须删除第一行。在 Unix 下执行此操作的最有效方法是什么?我怀疑这可能是一个使用catsed '1d'的命令。所有文件都有相同的扩展名并且都在同一个文件夹中,所以我们可能可以使用 *.extension 来指向这些文件。非常感谢!

回答by xpapad

Assuming your filenames are sorted in the order you want your files appended, you can use:

假设您的文件名按您希望文件附加的顺序排序,您可以使用:

ls *.extension | xargs -n 1 tail -n +2

EDIT: After Sorin and Gilles comments about the possible dangers of piping lsoutput, you could use:

编辑:在 Sorin 和 Gilles 评论管道ls输出的可能危险之后,您可以使用:

find . -name "*.extension" | xargs -n 1 tail -n +2

回答by sorpigal

Everyone has to be complicated. This is really easy:

每个人都必须是复杂的。这真的很容易:

tail -q -n +2 file1 file2 file3

And so on. If you have a large number of files you can load them in to an array first:

等等。如果您有大量文件,您可以先将它们加载到数组中:

list=(file1 file2 file3)
tail -q -n +2 "${list[@]}"

All the files with a given extension in the current directory?

当前目录中具有给定扩展名的所有文件?

list=(*.extension)
tail -q -n +2 "${list[@]}"

Or just

要不就

tail -q -n +2 *.extension

回答by Douglas Leeder

Just append each file after removing the first line.

删除第一行后只需附加每个文件。

#!/bin/bash

DEST=/tmp/out
FILES=space separated list of files

echo "" >$DEST
for FILE in $FILES
do
    sed -e'1d' $FILE >>$DEST
done

回答by Gilles 'SO- stop being evil'

tailoutputs the last lines of a file. You can tell it how many lines to print, or how many lines to omit at the beginning (-n +Nwhere N is the number of the first line to print, counting from 1 — so +2omits one line). With GNU utilities (i.e. under Linux or Cygwin), FreeBSD or other systems that have the -qoption:

tail输出文件的最后几行。您可以告诉它要打印多少行,或者在开头省略多少行(-n +N其中 N 是要打印的第一行的编号,从 1 开始计数——因此+2省略了一行)。使用 GNU 实用程序(即在 Linux 或 Cygwin 下)、FreeBSD 或其他具有以下-q选项的系统:

tail -q -n +2 *.extension

tailprints a header before each file, and -qis not standard. If your implementation doesn't have it, or to be portable, you need to iterate over the files.

tail在每个文件之前打印一个标题,-q这不是标准的。如果您的实现没有它,或者是可移植的,您需要遍历文件。

for x in *.extension; do tail -n +2 <"$x"; done

Alternatively, you can call Awk, which has a way to identify the first line of each file. This is likely to be faster if you have a lot of small files and slower if you have many large files.

或者,您可以调用 Awk,它有一种方法可以识别每个文件的第一行。如果您有很多小文件,这可能会更快,如果您有很多大文件,这可能会更慢。

awk 'FNR != 1' *.extension

回答by Vijay

ls -1 file*.txt | xargs nawk 'FNR!=1'