说明awk命令 [英] Explain awk command

查看:192
本文介绍了说明awk命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我正在寻找一个命令的在线模式后,接下来的两行打印和我读到一篇awk命令这我无法理解来了。

Today I was searching for a command online to print next two lines after a pattern and I came across an awk command which I'm unable to understand.

$ /usr/xpg4/bin/awk '_&&_--;/PATTERN/{_=2}' input

有人能解释一下吗?

Can someone explain it?

推荐答案

有人用 _ 作为一个变量名时实施共同awk的成语(成语E下面) 。改变每 _ 计数(或 C )然后看它是否对你有意义。

Someone used _ as a variable name when implementing a common awk idiom (idiom "e" below). Change every _ to the word count (or c) then see if it makes sense to you.

一些背景:下面的成语描述了如何选择一定范围的记录给出了具体的正则表达式匹配:

Some background: The following idioms describe how to select a range of records given a specific regexp to match:

a) Print all records from some regexp:

    awk '/regexp/{f=1}f' file

b) Print all records after some regexp:

    awk 'f;/regexp/{f=1}' file

c) Print the Nth record after some regexp:

    awk 'c&&!--c;/regexp/{c=N}' file

d) Print every record except the Nth record after some regexp:

    awk 'c&&!--c{next}/regexp/{c=N}1' file

e) Print the N records after some regexp:

    awk 'c&&c--;/regexp/{c=N}' file

f) Print every record except the N records after some regexp:

    awk 'c&&c--{next}/regexp/{c=N}1' file

g) Print the N records from some regexp:

    awk '/regexp/{c=N}c&&c--' file

我是从F改为变量名的发现,以C为计数酌情因为这是一个什么样的变量实际上是比较前pressive。

I changed the variable name from "f" for "found" to "c" for "count" where appropriate as that's more expressive of what the variable actually IS.

这篇关于说明awk命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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