bash 如何将两个文件依次分类但分别省略最后一行/第一行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3600170/
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
How to cat two files after each other but omit the last/first line respectively?
提问by bastibe
I have two files I want to cat together. However, the last line of the first file and the first line of the last file should be omitted.
我有两个文件我想放在一起。但是,应该省略第一个文件的最后一行和最后一个文件的第一行。
I am sure this can be done in a UNIX shell (or rather, Cygwin). But how?
我确信这可以在 UNIX shell(或者更确切地说,Cygwin)中完成。但是如何?
回答by Frank
$ head --lines=-1 file1 > res
$ tail --lines=+2 file2 >> res
回答by Marco
you can use:
您可以使用:
head -n -1 file1 && tail -n +2 file2
head shows the first lines of a file, the param -n shows the first n lines of the file, but if you put a - before the number, it shows all the file except the last n lines.
head 显示文件的第一行,参数 -n 显示文件的前 n 行,但如果在数字之前放置 -,它将显示除最后 n 行之外的所有文件。
tail is analog..
尾巴是模拟的..
for better reference:
为了更好的参考:
manhead
男人head
mantail
男人tail
回答by codaddict
head -n -1 file1
will print all lines in file1except the last line.
将打印file1除最后一行之外的所有行。
tail -n +2 file2
will print all lines in file2except the first line.
将打印file2除第一行之外的所有行。
Combine the two as:
将两者结合为:
head -n -1 file1 ; tail -n +2 file2
回答by ghostdog74
awk 'FNR>2{print p}{p=head -n -1 file1
}' file1 file2
Explanation as requested:
按要求解释:
FNR is the current record number of the current file being processed. At the start of each iteration, the line is stored in "p" variable, but it is not printed out, yet. As the record number reaches 3, the variable "p" is printed out, which contains record 2. This means the 2nd line. As awk reaches end of file, the last line is not printed out and then it goes to the next file.
FNR 是当前正在处理的文件的当前记录号。在每次迭代开始时,该行存储在“p”变量中,但尚未打印出来。当记录数达到 3 时,将打印出变量“p”,其中包含记录 2。这意味着第 2 行。当 awk 到达文件末尾时,最后一行不会打印出来,然后转到下一个文件。
回答by gumption
I am not able to use the following command on my Mac (more specifically, in a bash shell running in a Terminal window on Mac OS X 10.7.5):
我无法在 Mac 上使用以下命令(更具体地说,在 Mac OS X 10.7.5 的终端窗口中运行的 bash shell 中):
head: illegal line count -- -1
The command above generates the following error message:
上面的命令会生成以下错误消息:
sed -e '$d' file1
It appears negative values for the -n parameter are not accepted in the Mac OS X version of head. However, you can use sed instead:
Mac OS X 版本的 head 似乎不接受 -n 参数的负值。但是,您可以使用 sed 代替:
{ sed '$d' file1; sed '1d' file2; } > combined
["tail -n +2 file2" appears to work as described above on Mac OS X]
[“tail -n +2 file2”似乎在 Mac OS X 上按上述方式工作]
回答by glenn Hymanman
I often use sedfor head|tail actions:
我经常sed用于 head|tail 动作:
pax> cat file1.txt
1.1
1.2
1.3
1.4
1.5
pax> cat file2.txt
2.1
2.2
2.3
2.4
2.5
pax> head --lines=-1 file1.txt ; tail --lines=+2 file2.txt
1.1
1.2
1.3
1.4
2.2
2.3
2.4
2.5
回答by paxdiablo
The following transcript shows how to acheive this:
以下成绩单显示了如何实现这一点:
##代码##Giving the headcommand a negative count goes up to that far from the end. Similarly, a count starting with +causes tailto start at that line rather than a certain number of lines from the end.
给head命令一个负计数会增加到离结束很远的地方。类似地,开始与计数+的原因tail,以在该行,而不是一定数目从端线的开始。

