是什么?
在哪里?
为什么用?
下课用于提问,
上课听课有效率
结论停留表面
1.在当前系统中打印出所有普通用户的用户名和家目录(/etc/passwd)
[root@quyunlong~]# awk -F ":"
'NR==21,NR==22{print $1,$6}' /etc/passwd
oldgirl /home/oldgirl
test /home/test
[root@quyunlong~]#
2.显示当前系统中容量使用率大于5%的文件系统名称和挂载点
[root@quyunlong~]# df -h|awk -F "[
%]+" '$5>5{print $1,$6}'
Filesystem Mounted
/dev/sda3 /
/dev/sda1 /boot
[root@quyunlong~]# df -h|awk -F "[
%]+" '$5>5&&NR==2,NR==3{print $1,$6}'
/dev/sda3 /
devtmpfs /dev
[root@quyunlong~]#
3.为/tmp/grub.conf文件中前三行的行首加#号
[root@quyunlong~]# sed -n '1,3p' n.txt |sed
-n 's#GRUB#\#GRUB#gp'
#GRUB_TIMEOUT=5
#GRUB_DISTRIBUTOR="$(sed 's, release
.*$,,g' /etc/system-release)"
#GRUB_DEFAULT=saved
[root@quyunlong~]#
[root@quyunlong~]# sed -n
'1,3s#GRUB#\#GRUB#gp' n.txt
#GRUB_TIMEOUT=5
#GRUB_DISTRIBUTOR="$(sed 's, release
.*$,,g' /etc/system-release)"
#GRUB_DEFAULT=saved
[root@quyunlong~]#
4.通过notepad++打开,把一下内容输入到test.txt文件中
姓名 区号 电话 三个月捐款数量
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)显示所有电话号码
[root@quyunlong~]# awk -F ":"
'{print $2}' d.txt
[510] 548-1278
[408] 538-2358
[206] 654-6279
[206] 548-1348
[206] 548-1278
[916] 343-6410
[406] 298-7744
[206] 548-1278
[916] 348-4278
[510] 548-5258
[408] 926-3456
[916] 440-1763
[root@quyunlong~]#
(2)显示Dan的电话号码
[root@quyunlong~]# awk -F ":"
'NR==11{print $2}' d.txt
[406] 298-7744
(3)显示Susan的名字和电话号码
[root@quyunlong~]# awk -F ":"
'NR==5{print $1,$2}' d.txt
Susan Dalsass [206] 654-6279
[root@quyunlong~]#
(4)显示所有以D开头的姓
[root@quyunlong~]# awk '/^D.*/' d.txt
Dan Savage:[406] 298-7744:450:300:275
[root@quyunlong~]# awk '/^D.*/' d.txt |awk
'{print $1}'
Dan
[root@quyunlong~]#
[root@quyunlong~]# sed -rn '11s#(.*)
Sav.*$#\1#gp' d.txt
Dan
[root@quyunlong~]#
(5)显示所有区号为916的人名
[root@quyunlong~]# sed -n '/916/p'
d.txt|awk -F "[: ]+" '{print $2}'
Quigley
Goldenrod
Stachelin
[root@quyunlong~]#
(6)显示Mike的捐款.显示每个值时都有以$开头.如$250$100$175
[root@quyunlong~]# sed -rn'2s#^.*1278(.*)#\1#gp' d.txt
:250:100:175
[root@quyunlong~]# sed -rn
'2s#^.*1278(.*)#\1#gp' d.txt|tr ":" "$"
$250$100$175
[root@quyunlong~]#
(7)显示姓,其后跟一个逗号和名
[root@quyunlong~]# awk -F ":"
'NR==2,NR==19{print $1}' d.txt |sed -n 's# #,#gp'
Mike,Harrington
Christian,Dobbins
Susan,Dalsass
Archie,McNichol
Jody,Savage
Guy,Quigley
Dan,Savage
Nancy,McNeil
John,Goldenrod
Chet,Main
Tom,Savage
Elizabeth,Stachelin
[root@quyunlong~]#
(8)在Jody开头的行前面添加oldboy
[root@quyunlong~]# sed -n
'8s#Jody#oldboyJody#gp' d.txt
oldboyJody Savage:[206] 548-1278:15:188:150
[root@quyunlong~]#
(9)删除空白行
[root@quyunlong~]# grep "." d.txt
姓名 区号 电话 三个月捐款数量
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@quyunlong~]#
[root@quyunlong~]# sed -n '/./p' d.txt
姓名 区号 电话 三个月捐款数量
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@quyunlong~]#
[root@quyunlong~]# awk '/./' d.txt
姓名 区号 电话 三个月捐款数量
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@quyunlong~]#