awk练习题(一)

文件中包含名字,电话号码和过去三个月里的捐款,用:分隔

具体内容如下:

Mike Harrington:[510] 548-1278:250:100:175
Christian Dobbins:[408] 538-2358:155:90:201
Susan Dalsass:[206] 654-6279:250:60:50
Archie McNichol:[206] 548-1348:250:100:175
Jody Savage:[206] 548-1278:15:188:150
Guy Quigley:[916] 343-6410:250:100:175
Dan Savage:[406] 298-7744:450:300:275
Nancy McNeil:[206] 548-1278:250:80:75
John Goldenrod:[916] 348-4278:250:100:175
Chet Main:[510] 548-5258:50:95:135
Tom Savage:[408] 926-3456:250:168:200
Elizabeth Stachelin:[916] 440-1763:175:75:300

练习:
1. 显示所有电话号码

 awk -F: '{print $2}' file.txt 

2.显示Dan的电话号码

awk -F: '$1~/Dan/{print $2}' file.txt 

3.显示Susan的名字和电话号码

 awk -F: '$1~/Susan/{print$1":"$2}' file.txt 

4.显示所有以D开头的姓

 awk '$1~/^D/{print $1}' file.txt 

5.显示所有以一个C或E开头的姓

awk -F: '$1~/^[C|E]/{print $1}' file.txt |awk '{print $2}'

6.显示所有只有四个字符的名

awk 'length($1)=="4"{print $1}' file.txt

7.显示所有区号为916的人名

 awk -F"[ :]" '$3~/916/{print $1" "$2}' file.txt 

第7题的另一种写法:

 awk -F '[] :[]' '{if($4==916) print $2}' telabulm.txt

8.显示Mike的捐款.显示每个值时都有以 . 250 100 175

awk -F:  '$1~/Mike/{print "$"$3" ""$"$4" ""$"$5}' file.txt 

9.显示姓,其后跟一个逗号和名,如Jody,Savage

awk -F:  '{print $1}' file.txt |awk '{print $1","$2}'

10.写一个awk的脚本,它的作用:显示Savage的全名和电话号码.显示Chet的捐款.显示所有头一个月捐款$250的人名.

#!/usr/bin/awk -f 
#show Savage Full name and telephone
BEGIN{
print("Start Run")
}
{
if($1~/Savage/) print($1,$2)
if($1~/Chet/) print("Chet Donate:""$"$3,"$"$4,"$"$5)
if($3==250) print($1,"donate 250$")
}
END{
print("end")
}

本题涉及到的一些知识点收集:
0.入门
http://man.linuxde.net/awk
1.正则
http://www.cnblogs.com/chengmo/archive/2010/10/11/1847772.html
2.awk脚本
http://blog.csdn.net/leeboy_wang/article/details/7941189
http://www.cnblogs.com/loveglory/archive/2012/11/25/2787588.html
3.awk命令
http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858470.html

你可能感兴趣的:(Hbase,Linux)