awk 练习

原出处: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

3、 用 awk 命令打印文件所有行的第一个字段。
[root@student04 ~]# awk -F: '{print $1}' /etc/passwd

4、打印输入行总数。
[root@student04 ~]# awk 'END{print NR}' /etc/passwd
37

5、打印每行字段数。
[root@student04 ~]# awk -F: '{print NF}' /etc/passwd

6、打印最后一行的最后一个字段的值。
[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 

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 个的行总数。
[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
=================================



1. Print all the phone numbers.
[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

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

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
Elizabeth Stachelin

6. Print all first names containing only four characters.
[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.
[root@student04 tmp]# awk -F'[ :]' '$3=="(916)"{print $1}' awk02.txt
Guy
John
Elizabeth

8. Print Mike's campaign contributions. Each value should be printed
[root@student04 tmp]# awk -F'[ :]' '$1=="Mike"{print "$"$5,"$"$6,"$"$7}' awk02.txt 
$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

10.Write an awk script called facts that
a. Prints full names and phone numbers for the Savages.
[root@student04 tmp]# awk -F'[ :]' '($2=="Savage"){print $1,$2,$3,$4}' awk02.txt
Jody Savage (206) 548-1278
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

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
Susan Dalsass:(206) 654-6279:250:60:50



使用awk处理以上的数据:
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

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

9. Add $10 to Chet's second contribution.
[root@student04 tmp]# awk -F'[ :]' '($1=="Chet"){print $6+10}' awk02.txt
105

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 1. 使用awk命令,将一个文件中的第一列和第二列相加,并输结果。 2. 使用sed命令,将一个文件中的所有空删除,并输结果。 3. 使用grep命令,查找一个文件中包含特定字符串的,并输结果。 4. 使用awk命令,将一个文件中的所有按照第二列进排序,并输结果。 5. 使用sed命令,将一个文件中的所有单词转换为大写,并输结果。 6. 使用grep命令,查找一个文件中不包含特定字符串的,并输结果。 7. 使用awk命令,将一个文件中的所有按照第一列进分组,并输结果。 8. 使用sed命令,将一个文件中的所有反转,并输结果。 9. 使用grep命令,查找一个文件中包含特定字符串的数,并输结果。 10. 使用awk命令,将一个文件中的所有按照第三列进筛选,并输结果。 ### 回答2: awk、sed和grep是Linux系统中常见的文本处理工具,它们可以对文本进搜索、替换、过滤、格式化等操作。以下是一些常见的练习题,可以帮助读者加深对这些工具的理解和掌握它们的使用技巧。 1. 使用awk命令统计日志文件中每个IP的访问次数。 awk '{a[$1]++} END {for(i in a) print i, a[i]}' logfile 其中,a[$1]++用于统计IP的访问次数,END子句用于在处理完整个文件后输结果。 2. 使用sed命令批量重命名文件夹中所有的.jpg文件为.png。 sed -i 's/\.jpg/\.png/g' $(find /path/to/folder -type f -name '*.jpg') 其中,-i选项表示直接修改原文件,使用find命令来查找所有.jpg文件,并将其替换为.png。 3. 使用grep命令查找一个目录下所有包含“Linux”关键字的文件,并输匹配结果。 grep -r "Linux" /path/to/directory 其中,-r选项表示递归查找所有文件,如果找到匹配项,则会输所在的文件名和匹配的。 4. 使用awk命令从一组数字中找最大值和最小值,并计算它们的平均数。 echo "1 2 3 4 5" | awk 'BEGIN {max=0;min=999999} {for(i=1;i<=NF;i++) if($i>max) max=$i;else if($i<min) min=$i;total+=$i} END {print "Max:",max,"Min:",min,"Avg:",total/NF}' 其中,BEGIN子句用于初始化max和min变量,NF表示输入字段数量,END子句用于计算平均数。 以上是关于awk、sed和grep的基础练习题,掌握了这些基本操作后,读者可以尝试更复杂的文本处理任务,如去重、排序、计数等。同时,多查阅相关文档和资料,不断练习,可以更好地掌握这些工具。 ### 回答3: awk、sed、grep三者是Linux中常用的文本处理命令,它们都可以用来处理大量的文本信息。下面是一些实践练习。 1. 使用grep 给定一个文件file.txt,其中的每都是一个字符串,找其中包含字符串“hello”的所有并输到一个新的文件new.txt。 grep "hello" file.txt > new.txt 2. 使用awk 给定一个包含三列的文件file.txt(英语、数学和科学的成绩),找其中科学成绩大于80分的所有学生。 awk '$3>80{print $0}' file.txt 3. 使用sed 给定一个文件file.txt,其中有若干英文句子,将其中所有的小写字母转换为大写字母。 sed 's/[a-z]/\U&/g' file.txt 4. 使用grep和awk 给定一个包含多个文件的目录dir,找其中含有文件名“error.log”的文件,并统计各个文件中“error”单词现的总次数。 grep -l "error.log" dir/* | xargs awk '{count +=gsub(/error/,"&")}END{print count}' 以上是一些常用的练习题,可以帮助大家熟悉和掌握这些常用命令的使用方法。需要注意的是,在实践过程中,应该多加尝试,有问题及时查阅相关文档或寻求帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值