您可以使用的组合head和tail这样的例子:
$ 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参数中的负值.)