Grep倒数第二行

Mad*_*gan 0 bash grep

就像标题所说的那样,如何使用grep(或类似的bash工具)过滤(可变长度)文件的最后一行?

也就是说,除倒数第二行外,显示所有内容.

谢谢

Mat*_*Mat 7

您可以使用的组合headtail这样的例子:

$ cat input 
one
two
three
HIDDEN
four
$ head -n -2 input ; tail -n 1 input 
one
two
three
four
Run Code Online (Sandbox Code Playgroud)

来自coreutils头文档:

'-n k'
' - lines = k'
Output the first k lines. However, if k starts with a ‘-’, print all but the last k lines of each file. Size multiplier suffixes are the same as with the -c option.

So the head -n -2部分除了输入的最后两行之外的所有部分.

遗憾的是,这不是便携式的.(POSIX不允许-n参数中的负值.)