在 MAC OSX 上跳过文件的最后几行的 head 命令 [英] head command to skip last few lines of file on MAC OSX

查看:45
本文介绍了在 MAC OSX 上跳过文件的最后几行的 head 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想输出文件的所有行,但跳过终端上的最后 4 行.

I want to output all lines of a file, but skip last 4, on Terminal.

根据 UNIX 手册页,以下可能是一个解决方案.

As per UNIX man page following could be a solution.

head -n -4 main.m

MAN 页面:

-n, --lines=[-]N打印前 N 行而不是前 10 行;与铅-ing '-', 打印每个文件除最后 N 行外的所有内容

-n, --lines=[-]N print the first N lines instead of the first 10; with the lead- ing '-', print all but the last N lines of each file

我在这里阅读了手册页.http://unixhelp.ed.ac.uk/CGI/man-cgi?head

但在 MAC OSx 上我收到以下错误.

But on MAC OSx I get following error.

head: 非法行数 -- -4

head: illegal line count -- -4

还可以做些什么来实现这一目标?

What else can be done to achieve this goal?

推荐答案

使用 awk 例如:

$ cat file
line 1
line 2
line 3
line 4
line 5
line 6
$ awk 'n>=4 { print a[n%4] } { a[n%4]=$0; n=n+1 }' file
line 1
line 2
$

它可以简化为 awk 'n>=4 { print a[n%4] } { a[n++%4]=$0 }' 但我不确定是否所有 awk实现支持它.

It can be simplified to awk 'n>=4 { print a[n%4] } { a[n++%4]=$0 }' but I'm not sure if all awk implementations support it.

这篇关于在 MAC OSX 上跳过文件的最后几行的 head 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆