awk范围和文本选择 [英] awk range and selection of text

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

问题描述

我知道如何在awk中使用范围运算符

I know how to use the range operator in awk

awk '/start/,/stop/' file

是否可以在范围运算符内选择文本? ATM,我正在使用if语句

Is it possible to select text inside the range operator? ATM, I am using an if statement

awk '/start/,/stop/ { if ($1~/foo/) { } }' file

还有一种更理想的方式吗?

is there a more idomatic way of doing it?

推荐答案

切勿使用范围表达式,因为它会使琐碎的工作变得更简短,但是当任务变得更有趣时,则需要完整的重写或重复条件.

Never use a range expression as it makes trivial jobs very slightly briefer but then requires a complete rewrite or duplicate conditions when the task gets slightly more interesting.

代替:

awk '/start/,/stop/' file

使用:

awk '/start/{f=1} f{print} /stop/{f=0}' file

,然后您要做的就变得简单了:

and then what you want to do becomes simply:

awk '/start/{f=1} f{ if ($1~/foo/) { } } /stop/{f=0}' file

我假设您在空的{ }内有一些想法.

I'm assuming you have something in mind for inside the empty { }.

这篇关于awk范围和文本选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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