全部博文(136)
分类: LINUX
2009-06-13 14:42:56
[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 |
[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
# 可以使用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 |
[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 |
[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 |
[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
|
[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! |
[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 |
[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 |
[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 |