原出处:http://hi.baidu.com/pmghong/item/95f48d203c61572543634a62
http://hi.baidu.com/pmghong/item/344f9b038e81ebbfa2df4362
使用awk对
/etc/passwd的数据进行处理:
1、 编写一个 awk 脚本,功能是打印所有输入行。
[root@student04 ~]# awk {print} /etc/passwd
2、 编写一个 awk 脚本,打印输入文件第八行。
[root@student04 ~]# awk '(NR==8){print $0}' /etc/passwd
[root@student04 ~]# awk '(NR==8){print $0}' /etc/passwd
3、 用 awk 命令打印文件所有行的第一个字段。
[root@student04 ~]# awk -F: '{print $1}' /etc/passwd
4、打印输入行总数。
[root@student04 ~]# awk 'END{print NR}' /etc/passwd
37
37
5、打印每行字段数。
[root@student04 ~]# awk -F: '{print NF}' /etc/passwd
6、打印最后一行的最后一个字段的值。
[root@student04 ~]# awk -F: 'END{print $7}' /etc/passwd
/bin/bash
[root@student04 ~]# awk -F: 'END{print $7}' /etc/passwd
/bin/bash
7、打印出最后一行,并打印出行号
[root@student04 ~]# awk 'END{print NR,$0}' /etc/passwd
37 chuyue:x:500:500:chuyue:/home/chuyue:/bin/bash
37 chuyue:x:500:500:chuyue:/home/chuyue:/bin/bash
8、打印 uid 在 30~50 范围内的用户名。
[root@student04 ~]# awk -F: '($3>=30){print $1;next}($3<=50){print $1}' /etc/passwd
9、倒序排列文件的所有字段。
[root@student04 ~]# awk -F: '{print $7,$6,$5,$4,$3,$2,$1}' /etc/passwd
10、打印 5 到 56 行。
[root@student04 ~]# cat -n /etc/passwd |awk -F: '(NR>=5 && NR<=56){print $0}'
11、 在文件顶部加上标题“Document” 。
[root@student04 ~]# awk 'BEGIN {print "Document"}{print}' /etc/passwd
12、隔行删除。(即只显示单数行或者只显示偶数行)
[root@student04 ~]# cat -n /etc/passwd |awk '(NR%2!=0){print $0}'
13、每行抽取第一个单词。
[root@student04 ~]# awk -F: '{print $1}' /etc/passwd
14、打印每行的第一个和第三个单词。
[root@student04 ~]# awk -F: '{print $1"\t\t"$3}' /etc/passwd
15、打印字段数大于 5 个的行总数。
1. Print all the phone numbers.
[root@student04 ~]# awk -F: '(NR>=5){x++;next;}END{print x}' /etc/passwd
待处理材料
=================================
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
=================================
[root@student04 tmp]# awk -F: '{print $2}' awk02.txt
2. Print Dan's phone number.
[root@student04 tmp]# awk -F: '($1=="Dan Savage"){print $2}' awk02.txt
(406) 298-7744
(406) 298-7744
3. Print Susan's name and phone number.
[root@student04 tmp]# awk -F: '($1=="Susan Dalsass"){print $1,$2}' awk02.txt
Susan Dalsass (206) 654-6279
4. Print all last names beginning with D.
[root@student04 tmp]# awk -F'[ :]' '$2~/^D/{print $1,$2}' awk02.txt
Christian Dobbins
Susan Dalsass
Christian Dobbins
Susan Dalsass
5. Print all first names beginning with either a C or E.
[root@student04 tmp]# awk -F: '$1~/^[CE]/{print $1}' awk02.txt
Christian Dobbins
Chet Main
[root@student04 tmp]# awk -F'[ :]' '$1=="Mike"{print "$"$5,"$"$6,"$"$7}' awk02.txt
Christian Dobbins
Chet Main
Elizabeth Stachelin
6. Print all first names containing only four characters.
John
Elizabeth
8. Print Mike's campaign contributions. Each value should be printed
[root@student04 tmp]# awk --re-interval '$1~/\<[a-zA-Z]{4}\>'/{print $1}' awk02.txt
Jody
John
Chet
7.Print the first names of all those in the 916 area code.
Guy
[root@student04 tmp]# awk -F'[ :]' '$3=="(916)"{print $1}' awk02.txt
John
Elizabeth
8. Print Mike's campaign contributions. Each value should be printed
$250 $100 $175
9. Print last names followed by a comma and the first name.
[root@student04 tmp]# awk -F'[ :]' '{print $2","$1}' awk02.txt
Harrington,Mike
[root@student04 tmp]# awk -F'[ :]' '{print $2","$1}' awk02.txt
Harrington,Mike
10.Write an awk script called facts that
Dan Savage (406) 298-7744
Tom Savage (408) 926-3456
a. Prints full names and phone numbers for the Savages.
Jody Savage (206) 548-1278
[root@student04 tmp]# awk -F'[ :]' '($2=="Savage"){print $1,$2,$3,$4}' awk02.txt
Dan Savage (406) 298-7744
Tom Savage (408) 926-3456
b. Prints Chet's contributions.
[root@student04 tmp]# awk -F: '($1=="Chet Main"){print $3,$4,$5}' awk02.txt
50 95 135
50 95 135
c. Prints all those who contributed $250 the first month.
[root@student04 tmp]# awk -F: '($3=="250"){print $0}' awk02.txt
Mike Harrington:(510) 548-1278:250:100:175
Mike Harrington:(510) 548-1278:250:100:175
Susan Dalsass:(206) 654-6279:250:60:50
使用awk处理以上的数据:
Susan Dalsass (206) 654-6279
Dan Savage
1. Print the first and last names of those who contributed more than $100 in the second month.
[root@student04 tmp]# awk -F: '($4>=100){print $1}' awk02.txt
2. Print the names and phone numbers of those who contributed less than $85 in the last month.
[root@student04 tmp]# awk -F: '($5<=85){print $1,$2}' awk02.txt
Susan Dalsass (206) 654-6279
Nancy McNeil (206) 548-1278
3. Print the names of those who contributed between $75 and $150 in the first month.
[root@student04 tmp]# awk -F: '($3>=75 && $3<=150){print $1}' awk02.txt
4. Print the names of those who contributed less than $800 over the three-month period.
[root@student04 tmp]# awk -F: '($3+$4+$5<=800){print $1}' awk02.txt
5. Print the names and addresses of those with an average monthly contribution greater than $200.
[root@student04 tmp]# awk -F: '(($3+$4+$5)/3>=200){print $1}' awk02.txt
Dan Savage
Tom Savage
6. Print the first name of those not in the 916 area code.
[root@student04 tmp]# awk -F'[ :]' '($3!="(916)"){print $1}' awk02.txt
Mike
7. Print each record preceded by the number of the record.
[root@student04 tmp]# awk -F: '{print NR,$0}' awk02.txt
1 Mike Harrington:(510) 548-1278:250:100:175
2 Christian Dobbins:(408) 538-2358:155:90:201
[root@student04 tmp]# awk -F'[ :]' '($3!="(916)"){print $1}' awk02.txt
Mike
7. Print each record preceded by the number of the record.
[root@student04 tmp]# awk -F: '{print NR,$0}' awk02.txt
1 Mike Harrington:(510) 548-1278:250:100:175
2 Christian Dobbins:(408) 538-2358:155:90:201
8. Print the name and total contribution of each person.
[root@student04 tmp]# awk -F: '{print $1,"$"$3+$4+$5}' awk02.txt
Mike Harrington $525
[root@student04 tmp]# awk -F: '{print $1,"$"$3+$4+$5}' awk02.txt
Mike Harrington $525
9. Add $10 to Chet's second contribution.
[root@student04 tmp]# awk -F'[ :]' '($1=="Chet"){print $6+10}' awk02.txt
105
[root@student04 tmp]# awk -F'[ :]' '($1=="Chet"){print $6+10}' awk02.txt
10.Change Nancy McNeil's name to Louise McInnes.
[root@student04 tmp]# awk -F: '$1~/Nancy McNeil/{$1="Louise McInnes";print $0}' awk02.txt
Louise McInnes (206) 548-1278 250 80 75
[root@student04 tmp]# awk -F: 'BEGIN{OFS=":"}$1~/Nancy McNeil/{$1="Louise McInnes";print $0}' awk02.txt
Louise McInnes:(206) 548-1278:250:80:75
[root@student04 tmp]# awk -F: '{OFS=":"}$1~/Nancy McNeil/{$1="Louise McInnes";print $0}' awk02.txt
Louise McInnes:(206) 548-1278:250:80:75
[root@student04 tmp]# awk -F: '$1~/Nancy McNeil/{$1="Louise McInnes";print $0}' awk02.txt
Louise McInnes (206) 548-1278 250 80 75
[root@student04 tmp]# awk -F: 'BEGIN{OFS=":"}$1~/Nancy McNeil/{$1="Louise McInnes";print $0}' awk02.txt
Louise McInnes:(206) 548-1278:250:80:75
[root@student04 tmp]# awk -F: '{OFS=":"}$1~/Nancy McNeil/{$1="Louise McInnes";print $0}' awk02.txt
Louise McInnes:(206) 548-1278:250:80:75