awk 中next的使用详解

来源:互联网 发布:mac文件复制到u盘里 编辑:程序博客网 时间:2024/05/21 01:52

直接看例子吧:

[root@centos-fuwenchao tmp]# more fileababcdef[root@centos-fuwenchao tmp]# 


[root@centos-fuwenchao tmp]# awk '/^a/{print $0} /^a/{print $0}' fileababaa[root@centos-fuwenchao tmp]# awk '/^a/{print $0;next} /^a/{print $0}' file aba[root@centos-fuwenchao tmp]# 

上面是不是要打印四行吖??????


[root@centos-fuwenchao tmp]# awk '/^a/{print $0;next} /^a/{print $0}' file fileabaaba[root@centos-fuwenchao tmp]# 

[root@centos-fuwenchao tmp]# awk '/^a/{print $0;next}' file fileabaaba[root@centos-fuwenchao tmp]# awk '/^a/{print $0}' file fileabaaba[root@centos-fuwenchao tmp]# awk '/^a/{print $0;next}' fileaba[root@centos-fuwenchao tmp]# awk '/^a/{print $0}' fileaba[root@centos-fuwenchao tmp]# 


awk '/^a/{next;print $0}' file这个什么也输出不来




======================

网上解释

遇到next时,读入下一行,然后从头开始处理,即next起到了{getline; goto head}的作用。

awk '/^a/{print $0;next} /^a/{print $0}' 用伪码可以这样来看:

:headif 匹配/^a/    print $0    读入下一行    goto headfiif 匹配/^a/    print $0figoto head

awk code: 'BEGIN{...}{Main Input}END{..}'
next 读入下一输入行并从(Main Input中的)第一个规则开始执行脚本。

现在可以解释为什么只有两行而不是四行了吧!


看下这个

[root@centos-fuwenchao tmp]# awk '/^a/{print $0;next} /^a/{print $0}' file fileabaadabaad[root@centos-fuwenchao tmp]# 

先读取第一个输入,在读取第二个输入


假如我再这样

[root@centos-fuwenchao tmp]# awk '/^a/{print $0;next} /^a/{print $0}' file -abaad

他会处于等待状态 ,等待你从标准输入中输入数据

如果你是输入以a开头的,则再原样打印,否则什么也不输出!






0 0
原创粉丝点击