awk 中next的使用详解

直接看例子吧:

[root@centos-fuwenchao tmp]# more file
ab
a
b
c
d
e
f
[root@centos-fuwenchao tmp]# 


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

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


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

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


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




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

网上解释

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

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

:head
if 匹配/^a/
    print $0
    读入下一行
    goto head
fi
if 匹配/^a/
    print $0
fi
goto head

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

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


看下这个

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

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


假如我再这样

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

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

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






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值