免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: ghostgorst
打印 上一主题 下一主题

awk的next使用 [复制链接]

论坛徽章:
0
21 [报告]
发表于 2009-08-21 14:09 |只看该作者

回复 #19 kwokcn 的帖子

awk '/^a/{print $0} /^a/{print $0}' 那这样不加next语句,awk怎么处理呢?
1.读入一行,从第一个模式开始匹配一直到最后一个模式,所有模式都匹配一边么?
2.还是处理一个模式时候,把所有匹配行都读入处理,然后再处理下一个模式?

按照大家回帖我理解多数都是第1种情况,但想不出来例子证明这点.

论坛徽章:
0
22 [报告]
发表于 2009-08-21 14:19 |只看该作者
伪码来表示完整的AWK处理流程:


for (i = 0; i <启动参数中 -v Assignment 数目; i++)
{
    执行第 i -v Assignment,为相应的变量赋值
}

for (i = 0; i < BEGIN模式的数目; i++)
{
    处理第iBEGIN模式对应的操作
}

for (i = 0; i < 启动参数中 File Assignment 参数的总个数; i++)
{

if (
i 个参数是 Assignment)


{

        执行对应的Assignment,为相应的变量赋值

continue;


}


    打开第i个参数对应的待处理文件

while ( not
文件结束)


{



根据RS,从待处理文件中读取一条记录



根据FS,将读取的记录分隔成不同的字段


for (j = 0; j < pattern
的数目; j++)


{


if (
jpattern匹配成功) //如果没有指定pattern,则总是匹配成功


{

                执行对应的action
//
如果没有指定action,缺省为 print $0


}


}
//
所有pattern匹配结束


}
//
一个文件处理结束

}
//
所有文件都处理结束


for (i = 0; i < END模式的数目; i++)
{
    处理第iEND模式对应的操作
}

论坛徽章:
23
15-16赛季CBA联赛之吉林
日期:2017-12-21 16:39:27白羊座
日期:2014-10-27 11:14:37申猴
日期:2014-10-23 08:36:23金牛座
日期:2014-09-30 08:26:49午马
日期:2014-09-29 09:40:16射手座
日期:2014-11-25 08:56:112015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:0315-16赛季CBA联赛之山东
日期:2017-12-21 16:39:1915-16赛季CBA联赛之广东
日期:2016-01-19 13:33:372015亚冠之山东鲁能
日期:2015-10-13 09:39:062015亚冠之西悉尼流浪者
日期:2015-09-21 08:27:57
23 [报告]
发表于 2009-08-21 14:20 |只看该作者

回复 #20 ghostgorst 的帖子

man awk

AWK PROGRAM EXECUTION
       An  AWK program consists of a sequence of pattern-action statements and
       optional function definitions.
              pattern   { action statements }
              function name(parameter list) { statements }
       Gawk first reads the program source from the program-file(s) if  speci-
       fied, from arguments to --source, or from the first non-option argument
       on the command line.  The -f and --source options may be used  multiple
       times  on  the command line.  Gawk reads the program text as if all the
       program-files and command  line  source  texts  had  been  concatenated
       together.   This  is  useful  for  building libraries of AWK functions,
       without having to include them in each new AWK program that uses  them.
       It also provides the ability to mix library functions with command line
       programs.
       The environment variable AWKPATH specifies a search path  to  use  when
       finding  source  files named with the -f option.  If this variable does
       not exist, the default path is ".:/usr/local/share/awk".   (The  actual
       directory  may  vary, depending upon how gawk was built and installed.)
       If a file name given to the -f option contains a "/" character, no path
       search is performed.
       Gawk executes AWK programs in the following order.  First, all variable
       assignments specified via the -v option are performed.  Next, gawk com-
       piles  the program into an internal form.  Then, gawk executes the code
       in the BEGIN block(s) (if any), and then proceeds  to  read  each  file
       named  in  the  ARGV array.  If there are no files named on the command
       line, gawk reads the standard input.
       If a filename on the command line has the form var=val it is treated as
       a  variable  assignment.   The  variable var will be assigned the value
       val.  (This happens after any BEGIN block(s) have been  run.)   Command
       line  variable assignment is most useful for dynamically assigning val-
       ues to the variables AWK uses to  control  how  input  is  broken  into
       fields  and records.  It is also useful for controlling state if multi-
       ple passes are needed over a single data file.
       If the value of a particular element of ARGV is empty (""), gawk  skips
       over it.
       For  each record in the input, gawk tests to see if it matches any pat-
       tern in the AWK program.  For each pattern that the record matches, the
       associated  action  is  executed.  The patterns are tested in the order
       they occur in the program.

       Finally, after all the input is exhausted, gawk executes  the  code  in
       the END block(s) (if any).

论坛徽章:
0
24 [报告]
发表于 2009-08-21 14:34 |只看该作者

回复 #20 ghostgorst 的帖子

是第一种,每读一行做一遍处理。

这个貌似没必要证明吧……如果非要确定,一个简单的方式就是在awk内部没有存储操作的前提下,直接看一下awk占用的内存是否线性增长就行了。

论坛徽章:
0
25 [报告]
发表于 2009-08-21 14:41 |只看该作者

回复 #23 kwokcn 的帖子

感谢各位帮忙了.非常感谢

论坛徽章:
0
26 [报告]
发表于 2009-08-21 14:43 |只看该作者

回复 #21 wwmstone 的帖子

谢谢大你的伪代码了.

论坛徽章:
0
27 [报告]
发表于 2009-08-21 14:49 |只看该作者
先说你的后面两个问题,为了方便,print $0都写成print(因为效果一样)
2,awk '/^a/{print ;next}' t
如果匹配以a开头则顺序进行花括号里面的操作,而匹配以a开头的记录后的默认操作是print 然后读入下一条记录(next),  
因此,这个花括号和里面的东西都是多余的,效果和awk '/^a/' t 一样。 读两个文件也一样的道理

3,awk '/^a/{next;print}' t
道理跟上面说的一样,如果匹配以a开头的行,则进行{}里的操作(也就是说{}里的操作都是在匹配/^a/的前提下进行的)
里面的print是没有意义的。因为在print之前已经next(读下一记录)了,下一条记录不匹配以a开头的话就不进行任何操作(默认)
匹配的话就next了。

你的本意应该是想用awk '/^a/{next;print}' 来输出不匹配以a开头的行,这是不正确的,因为你没有为不匹配以a开头的行
指定一个动作。因此得到的是什么都没有。应该改成这样来输出不以a开头的行
awk '/^a/{next}{print}' t
或者
awk '/^a/{next}1' 1表示默认打印操作

第一个问题
1 ,awk '/^a/{print;next}/^a/{print}' t
在这里两个pattern都是一样的,awk只认为他们是一个,但是操作是连续起来的
相当于
awk '/^a/{print;next;print}' t   
这个时候很清楚了,后面一个print没有意义了。
awk '/^a/{print}/^a/{print}' t  <=>  awk '/^a/{print;print}' t
awk '/^a/{print}/^b/{print}/^a/{print}' t  <=> awk '/^a/{print;print}/^b/{print}' t

论坛徽章:
0
28 [报告]
发表于 2009-08-21 15:01 |只看该作者

回复 #26 lucash 的帖子

awk '/^a/{print}/^b/{print}/^a/{print}' t  <=> awk '/^a/{print;print}/^b/{print}' t


这个不大对吧?

telfort1:/kfbuild/x144378>cat file
a
b
a
c
d
e
f
如果向你说的那样,模式相同的只保留一份模式.

telfort1:/kfbuild/x144378>awk '/^a//^b//^a/' file
a
a
b
a
a

结果应该是
a
a
a
a
b
才对吧?

论坛徽章:
0
29 [报告]
发表于 2009-08-21 15:11 |只看该作者
原帖由 ghostgorst 于 09-8-21 15:01 发表
awk '/^a/{print}/^b/{print}/^a/{print}' t   awk '/^a/{print;print}/^b/{print}' t


这个不大对吧?

telfort1:/kfbuild/x144378>cat file
a
b
a
c
d
e
f
如果向你说的那样,模式相同的只保留一 ...


我这里是这样的:(你那里 awk '/^a//^b//^a/' t 不报错吗)

Crys t:# cat k
a
b
a
c
d
e
f
Crys t:# awk '/^a/{print}/^b/{print}/^a/{print}' k
a
a
b
a
a
Crys t:# awk '/^a/{print;print}/^b/{print}' k
a
a
b
a
a

论坛徽章:
0
30 [报告]
发表于 2009-08-21 15:15 |只看该作者

回复 #28 lucash 的帖子

awk '/^a//^b//^a/' file
我这没出错啊,不是说模式的默认动作是打印么?我就没写.

反正没出现
a
a
a
a
b
这样的结果!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP