解释这种重复的行删除,为了固定,单行awk命令 [英] Explain this duplicate line removing, order retaining, one-line awk command

查看:152
本文介绍了解释这种重复的行删除,为了固定,单行awk命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学到了非常方便的方法,以消除重复的行保留从 http://goo.gl/sE9yf 的顺序。
比方说,如果你有以下文件,

I learned a really handy way to remove duplicate lines retaining the order from http://goo.gl/sE9yf. say, if you have the following file,

$cat file
a
a
b
b
a
c

您可以使用以下方法来删除重复的行

you can use the following to remove the duplicate lines

$awk '!x[$1]++' file
a
b
c

你能解释一下这是如何工作的业务precedence?

can you explain how this works in terms of precedence of operations?

推荐答案

这位前pression被解析为

The expression is parsed as

!(x[$(1)]++)

所以,从内到外,它是:

So, from the inside out, it's:


  • 取当前输入行的字段1, $(1)(注意, $ 是运营商awk的,不像在Perl)

  • 首页 X 与现场1的值;如果 X 是一个未绑定变量,将其绑定到一个新的关联数组

  • 后增 X [$(1)] ;一个类似于用C适用的规则,所以前pression的价值在于, x的[$(1)] 之前的增量,这将是零,如果 X [$(1)] 尚未分配值

  • 否定previous的价值,这将产生真理时, X [$(1)] 为零

  • 实际上做的增量,使 X [$(1)] 得到一个非零值。所以,接下来的时间, X [$(1)] $值相同(1)将返回1。

  • take field 1 of the current input line, $(1) (note that $ is an operator in Awk, unlike in Perl)
  • index x with the value of field 1; if x is an unbound variable, bind it to a new associative array
  • post-increment x[$(1)]; a rule similar to the one in C applies, so the value of the expression is that of x[$(1)] prior to the increment, which will be zero if x[$(1)] has not yet been assigned a value
  • negate the value of the previous, which will yield truth when x[$(1)] is zero
  • actually do the increment so that x[$(1)] gets a non-zero value. So, the next time, x[$(1)] for the same value of $(1) will return 1.

这前pression然后评估在输入的每一行,并决定是否 AWK 应该被执行,这是为了呼应线的隐含默认操作到标准输出

This expression is then evaluated for every line in the input and determines whether the implied default action of awk should be executed, which is to echo the line to stdout.

这篇关于解释这种重复的行删除,为了固定,单行awk命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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