- 论坛徽章:
- 0
|
#1. 打印输出结果中的第3列和第9列(以空格分隔)
[dwapp@dw_testdb yuechaotian]$ ll
total 20
-rw-r--r-- 1 dwapp gpadmin 1530 Jun 13 11:12 data_monitor.err
-rw-r--r-- 1 dwapp gpadmin 5931 Jun 13 11:12 monitor_comm.pl
-rw-r--r-- 1 dwapp gpadmin 937 Jun 13 11:12 monitor_dispatch.pl
-rwxr--r-- 1 dwapp gpadmin 10 May 27 17:12 test1.sh
[dwapp@dw_testdb yuechaotian]$ ll | awk '{print $3"\t"$9}'
dwapp data_monitor.err
dwapp monitor_comm.pl
dwapp monitor_dispatch.pl
dwapp test1.sh
#2. 打印行号和第9列
[dwapp@dw_testdb yuechaotian]$ ll > ./ll.awk
[dwapp@dw_testdb yuechaotian]$ cat ll.awk
total 20
-rw-r--r-- 1 dwapp gpadmin 1530 Jun 13 11:12 data_monitor.err
-rw-r--r-- 1 dwapp gpadmin 0 Jun 13 11:15 ll.awk
-rw-r--r-- 1 dwapp gpadmin 5931 Jun 13 11:12 monitor_comm.pl
-rw-r--r-- 1 dwapp gpadmin 937 Jun 13 11:12 monitor_dispatch.pl
-rwxr--r-- 1 dwapp gpadmin 10 May 27 17:12 test1.sh
[dwapp@dw_testdb yuechaotian]$ awk '{print NR"\t"$9}' ./ll.awk
1
2 data_monitor.err
3 ll.awk
4 monitor_comm.pl
5 monitor_dispatch.pl
6 test1.sh
[color="#ff6600"]# 可以使用printf函数,类似C语言中的printf
[dwapp@dw_testdb yuechaotian]$ awk '{printf"%03d--%s\n", NR,$9}' ./ll.awk
001--
002--data_monitor.err
003--ll.awk
004--monitor_comm.pl
005--monitor_dispatch.pl
006--test1.sh
#3. 显示文件ll.awk中带pl的所有行
[dwapp@dw_testdb yuechaotian]$ awk '/pl/{print}' ll.awk
-rw-r--r-- 1 dwapp gpadmin 5931 Jun 13 11:12 monitor_comm.pl
-rw-r--r-- 1 dwapp gpadmin 937 Jun 13 11:12 monitor_dispatch.pl
#如果是打印整行,可以省略print
[dwapp@dw_testdb yuechaotian]$ awk '/pl/' ll.awk
-rw-r--r-- 1 dwapp gpadmin 5931 Jun 13 11:12 monitor_comm.pl
-rw-r--r-- 1 dwapp gpadmin 937 Jun 13 11:12 monitor_dispatch.pl
#4. 显示第一个匹配pl的行,与第一个匹配rwx的行之间的行
[dwapp@dw_testdb yuechaotian]$ awk '/pl/,/rwx/' ll.awk
-rw-r--r-- 1 dwapp gpadmin 5931 Jun 13 11:12 monitor_comm.pl
-rw-r--r-- 1 dwapp gpadmin 937 Jun 13 11:12 monitor_dispatch.pl
-rwxr--r-- 1 dwapp gpadmin 10 May 27 17:12 test1.sh
#5. 函数的使用
[dwapp@dw_testdb yuechaotian]$ cat ll.awk
total 20
-rw-r--r-- 1 dwapp gpadmin 1530 Jun 13 11:12 data_monitor.err
-rw-r--r-- 1 dwapp gpadmin 0 Jun 13 11:15 ll.awk
-rw-r--r-- 1 dwapp gpadmin 5931 Jun 13 11:12 monitor_comm.pl
-rw-r--r-- 1 dwapp gpadmin 937 Jun 13 11:12 monitor_dispatch.pl
-rwxr--r-- 1 dwapp gpadmin 10 May 27 17:12 test1.sh
[dwapp@dw_testdb yuechaotian]$ awk '$5>1000' ll.awk
-rw-r--r-- 1 dwapp gpadmin 1530 Jun 13 11:12 data_monitor.err
-rw-r--r-- 1 dwapp gpadmin 5931 Jun 13 11:12 monitor_comm.pl
[dwapp@dw_testdb yuechaotian]$ awk '$5>1000 {print NR, $0}' ll.awk
2 -rw-r--r-- 1 dwapp gpadmin 1530 Jun 13 11:12 data_monitor.err
4 -rw-r--r-- 1 dwapp gpadmin 5931 Jun 13 11:12 monitor_comm.pl
[dwapp@dw_testdb yuechaotian]$ awk 'length($9)>10 {print NR,$0}' ll.awk
2 -rw-r--r-- 1 dwapp gpadmin 1530 Jun 13 11:12 data_monitor.err
4 -rw-r--r-- 1 dwapp gpadmin 5931 Jun 13 11:12 monitor_comm.pl
5 -rw-r--r-- 1 dwapp gpadmin 937 Jun 13 11:12 monitor_dispatch.pl
[dwapp@dw_testdb yuechaotian]$ awk 'length($0)>20 {print NR, $0}' ll.awk
2 -rw-r--r-- 1 dwapp gpadmin 1530 Jun 13 11:12 data_monitor.err
3 -rw-r--r-- 1 dwapp gpadmin 0 Jun 13 11:15 ll.awk
4 -rw-r--r-- 1 dwapp gpadmin 5931 Jun 13 11:12 monitor_comm.pl
5 -rw-r--r-- 1 dwapp gpadmin 937 Jun 13 11:12 monitor_dispatch.pl
6 -rwxr--r-- 1 dwapp gpadmin 10 May 27 17:12 test1.sh
#6. 显示没有设置密码的用户
[dwapp@dw_testdb yuechaotian]$ cat passwd
root:xtt
yct:878
yxt:989
jlt:
ora:*
[dwapp@dw_testdb yuechaotian]$ awk -F : '$2=="" {printf("%s has no password!\n",$1)}' ./passwd
jlt has no password!
#7. 使用BEGIN,END表达式,计算总成绩和平均成绩
[dwapp@dw_testdb yuechaotian]$ cat score
yuechaotian:80
oratea:79
yuexingtian:90
hot_dog:89
[dwapp@dw_testdb yuechaotian]$ awk '
> BEGIN {FS=":"; print "统计总成绩和平均成绩"; total=0;count=0}
> {print $1"\t"$2;total=total+$2;count+=1}
> END {printf("总成绩:%.2f\n平均成绩:%.2f", total,total/count)}
> ' ./score
统计总成绩和平均成绩
yuechaotian 80
oratea 79
yuexingtian 90
hot_dog 89
总成绩:338.00
平均成绩:84.50
#8. 使用自定义函数
[dwapp@dw_testdb yuechaotian]$ awk '
> BEGIN {
> pageno=1;
> file="score";
> }
> {
> pageno=print_header(file,pageno);
> printf("The current page no is: %d\n", pageno);
> }
>
> function print_header(fileName, pageNo){
> printf("fileName: %s pageNo: %d\n", fileName, pageNo);
> pageNo++;
> return pageNo;
> }
> ' ./score
fileName: score pageNo: 1
The current page no is: 2
fileName: score pageNo: 2
The current page no is: 3
fileName: score pageNo: 3
The current page no is: 4
fileName: score pageNo: 4
The current page no is: 5
#9. 可以使用命令重定向,保存awk的输出结果
[dwapp@dw_testdb yuechaotian]$ awk '{print $3"\t"$9}' ./ll.awk >ll.out
[dwapp@dw_testdb yuechaotian]$ cat ll.out
dwapp data_monitor.err
dwapp ll.awk
dwapp monitor_comm.pl
dwapp monitor_dispatch.pl
dwapp test1.sh
[dwapp@dw_testdb yuechaotian]$ awk '{print $3"\t"$9}' ./ll.awk >>ll.out
[dwapp@dw_testdb yuechaotian]$ cat ll.out
dwapp data_monitor.err
dwapp ll.awk
dwapp monitor_comm.pl
dwapp monitor_dispatch.pl
dwapp test1.sh
dwapp data_monitor.err
dwapp ll.awk
dwapp monitor_comm.pl
dwapp monitor_dispatch.pl
dwapp test1.sh
参考:
http://fanqiang.chinaunix.net/program/other/2005-09-07/3621.shtml
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/30637/showart_1963627.html |
|