是/启动/,/结束/范围EX pression在AWK曾经有用吗? [英] Is a /start/,/end/ range expression ever useful in awk?

查看:152
本文介绍了是/启动/,/结束/范围EX pression在AWK曾经有用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直主张,你永远不应该使用一系列的前pression这样的:

  /开始/,/结束/

在awk中,因为虽然它使平凡的情况下,你只想打印匹配的文本包括起始和终止行比其他稍*简短的:

  /开始/ {F = 1} F到{打印;如果(/结束/)F = 0}

当你想调整它甚至稍微做别的事,它需要在复制或以其他方式不可取code一个完全重新编写或结果。例如如果你想使用第二种形式上面你刚刚调整它来移动组件,打印不包括分隔符范围内匹配的文本:

 ˚F{如果(/结束/)F = 0;否则打印} /开始/ {F = 1}

但如果你开始与 /开始/,/结束/ 你需要放弃赞成什么,我只是张贴或你必须编写方法是这样的:

  /开始/,/结束/ {如果(/启动!|结束/)打印}

即。重复的条件,这是不可取

然后我看到一个问题,发布,需要确定最后的结束在一个文件中并在解决方案中使用了一系列的前pression,我还以为它似乎这样可能有一定的价值(见<一href=\"http://stackoverflow.com/a/21145009/1745001\">http://stackoverflow.com/a/21145009/1745001).

现在,虽然,我回想着这只是不值得在所有范围内的前pressions和不使用范围前pressions会工作一样好为解决困扰案例。

所以 - ?没有任何人有一个例子,其中一个范围内的前pression实际上增加了显着的价值的解决方案。

*我以前用的:

  /开始/ {F = 1} F; /结束/ {F = 0}

但太多的时候,我发现我不得不做一些额外的当˚F的真实, /挂断/ 是发现(或者换一种方式,当 /挂断/ 发现IF ˚F仅做一些事情是真实的),所以现在我只是想坚持到略少短暂但更强大和可扩展的:

  /开始/ {F = 1} F到{打印;如果(/结束/)F = 0}


解决方案

有趣。我也经常开始了一系列的前pression再后来就切换到使用变量。

我觉得这哪里可能是有用的,除了纯粹的唯一范围局势的情况是,如果你想打印的比赛,但只有当它在于在一定范围内。也因为它是显而易见它做什么。例如:

 的awk'/开始/,/结束/ {如果(/ PPP /)打印}'文件

此输入:

 启动
DFGD GD
PPP 1
gfdg
FD GFD
结束
PPP 2
PPP 3
开始
4 PPP
PPP 5
结束
PPP 6
PPP 7
gfdgdgd

会产生:

  PPP 1
4 PPP
PPP 5

-
当然,人们也可以使用:

 的awk'/开始/ {F = 1} / PPP /&放大器;&安培; F; /结束/ {F = 0}'文件

但它更长,稍差可读的。

I've always contended that you should never use a range expression like:

/start/,/end/

in awk because although it makes the trivial case where you only want to print matching text including the start and end lines slightly briefer than the alternative*:

/start/{f=1} f{print; if (/end/) f=0}

when you want to tweak it even slightly to do anything else, it requires a complete re-write or results in duplicated or otherwise undesirable code. e.g. if you want to print the matching text excluding the range delimiters using the second form above you'd just tweak it to move the components around:

f{if (/end/) f=0; else print} /start/{f=1}

but if you started with /start/,/end/ you'd need to abandon that approach in favor of what I just posted or you'd have to write something like:

/start/,/end/{ if (!/start|end/) print }

i.e. duplicate the conditions which is undesirable.

Then I saw a question posted that required identifying the LAST end in a file and where a range expression was used in the solution and I thought it seemed like that might have some value (see http://stackoverflow.com/a/21145009/1745001).

Now, though, I'm back to thinking that it's just not worth bothering with range expressions at all and a solution that doesn't use range expressions would have worked just as well for that case.

So - does anyone have an example where a range expression actually adds noticeable value to a solution?

*I used to use:

/start/{f=1} f; /end/{f=0}

but too many times I found I had to do something additional when f is true and /end/ is found (or to put it another way ONLY do something when /end/ is found IF f were true) so now I just try to stick to the slightly less brief but much more robust and extensible:

/start/{f=1} f{print; if (/end/) f=0}

解决方案

Interesting. I also often start with a range expression and then later on switch to using a variable..

I think a situation where this could be useful, aside from the pure range-only situations is if you want to print a match, but only if it lies in a certain range. Also because it is immediately obvious what it does. For example:

awk '/start/,/end/{if(/ppp/)print}' file

with this input:

start
dfgd gd
ppp 1
gfdg
fd gfd
end
ppp 2 
ppp 3
start
ppp 4
ppp 5
end
ppp 6
ppp 7
gfdgdgd

will produce:

ppp 1
ppp 4
ppp 5

-- One could of course also use:

awk '/start/{f=1} /ppp/ && f; /end/{f=0}' file

But it is longer and somewhat less readable..

这篇关于是/启动/,/结束/范围EX pression在AWK曾经有用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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