AWK - 跳过条件最后一行 [英] awk - skip last line for condition

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

问题描述

当我写这个问题的答案我用下面的:

When I wrote an answer for this question I used the following:

something | sed '$d' | awk '$1>3{print $0}'

例如


  • 只打印线,其中第一场大于3(AWK)

  • 但省略了最后一行的sed'$ D'

  • print only lines where the 1st field is bigger than 3 (awk)
  • but omit the last line sed '$d'.

这似乎对我来说有点重复的工作,肯定是可以做到上面只与 AWK - 没有 SED

This seems for me a bit of duplicate work, surely it is possible to do the above only with awk - without the sed?

我是一个awkdiot - 所以,有人可以提出一个解决方案。

I'm an awkdiot - so, can someone suggest a solution?

推荐答案

下面是你可以做到这一点的一种方法:

Here's one way you could do it:

$ printf "%s\n" {1..10} | awk 'NR>1&&p>3{print p}{p=$1}'
4
5
6
7
8
9

基本上,打印的 previous 的行的第一个字段,而不是当前的。

Basically, print the first field of the previous line, rather than the current one.

由于Wintermute正确地在评论(感谢)指出,为了打印整个行,你可以修改code这样:

As Wintermute has rightly pointed out in the comments (thanks), in order to print the whole line, you can modify the code to this:

awk 'p { print p; p="" } $1 > 3 { p = $0 }'

这只有在第一个字段是大于3线的内容,内容分配给 P

This only assigns the contents of contents of the line to p if the first field is greater than 3.

这篇关于AWK - 跳过条件最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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