将最后 n 行复制到新文件,然后从原始文件中删除 n 行

rsh*_*hah 5 linux macos bash shell

所以我有一个文件,我想将最后 3000 行移动到另一个不同的文件,然后从原始文件创建一个没有最后 3000 行的新文件。

我使用的是Mac,使用的命令如下:

tail -n 3000 fer2017-testing-reduced.arff >> fer2017-training-reduced-3000-more-instances.arff; head -n -3000 fer2017-testing-reduced.arff > fer2017-testing-reduced-3000-less-instances.arff
Run Code Online (Sandbox Code Playgroud)

但是,当我运行此命令时,我收到错误:

head: illegal line count -- -3000
Run Code Online (Sandbox Code Playgroud)

我不确定我哪里出了问题,或者这是否是 mac 的问题?

CGA*_*123 5

并非所有版本都head支持负行数。macOS 上默认安装的版本没有。

如果你已经coreutils安装了(如果你安装了Homebrew,你可以这样做:)brew install coreutils你应该能够使用ghead -n -3000.