用awk实现head、tail和tac

文件名称:actress_rankings.txt
文件内容:

1 Keira Knightley
2 Natalie Portman
3 Monica Bellucci
4 Bonnie Hunt
5 Cameron Diaz
6 Annie Potts
7 Liv Tyler
8 Julie Andrews
9 Lindsay Lohan
10 Catherine Zeta-Jones
11 CateBlanchett
12 Sarah Michelle Gellar
13 Carrie Fisher
14 Shannon Elizabeth
15 Julia Roberts
16 Sally Field
17 TeaLeoni
18 Kirsten Dunst
19 Rene Russo
20 JadaPinkett

 

模拟head打印文件前10行
[root@zhangshibo temp]# awk 'NR<=10' actress_rankings.txt
1 Keira Knightley
2 Natalie Portman
3 Monica Bellucci
4 Bonnie Hunt
5 Cameron Diaz
6 Annie Potts
7 Liv Tyler
8 Julie Andrews
9 Lindsay Lohan
10 Catherine Zeta-Jones

模拟tail命令打印文件后10行
[root@zhangshibo temp]# awk '{ buffer[NR % 10] = $0; } END { for(i=1;i<11;i++){print buffer[i%10]}}' actress_rankings.txt
11 CateBlanchett
12 Sarah Michelle Gellar
13 Carrie Fisher
14 Shannon Elizabeth
15 Julia Roberts
16 Sally Field
17 TeaLeoni
18 Kirsten Dunst
19 Rene Russo
20 JadaPinkett

 

模拟tac命令逆序打印输入文件所有行
[root@zhangshibo temp]# awk '{ buffer[NR] = $0; } END { for(i=NR;i>0;i--){print buffer[i]}}' actress_rankings.txt
20 JadaPinkett
19 Rene Russo
18 Kirsten Dunst
17 TeaLeoni
16 Sally Field
15 Julia Roberts
14 Shannon Elizabeth
13 Carrie Fisher
12 Sarah Michelle Gellar
11 CateBlanchett
10 Catherine Zeta-Jones
9 Lindsay Lohan
8 Julie Andrews
7 Liv Tyler
6 Annie Potts
5 Cameron Diaz
4 Bonnie Hunt
3 Monica Bellucci
2 Natalie Portman
1 Keira Knightley


[root@zhangshibo temp]#

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值