标签:files 统计 显示 i+1 awk services sub file nbsp
[root@oldboy02 ~]# # awk 参数 ‘模式{动作}‘ 文件
[root@oldboy02 ~]# # awk 参数 ‘条件(找谁) {干啥}‘ 文件
[root@oldboy02 ~]# awk -F ":" ‘NR==2{print $2,$4}‘ /etc/passwd
x 1
[root@oldboy02 ~]# cat /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
[root@oldboy02 ~]# mkdir -p /server/files
[root@oldboy02 ~]# cd /server/files/
[root@oldboy02 files]# cat >>files<<EOF
> Zhang Dandan 41116397 :250:100:175
> Zhang xiaoyu 390320151:155:90:201
> Meng Feifei 80042789 :250:60:50
> wu Waiwai 70271111 :250:100:175
> wang xiaoai 3515064655 :50:95:135
> Zi Gege 1986786350 :250:168:200
> Li Youjiu 918391635 :175:75:300
> Lao Nanhai 918391685 :250:100:175
> EOF
[root@oldboy02 files]# ls
files
[root@oldboy02 files]# cat files
Zhang Dandan 41116397 :250:100:175
Zhang xiaoyu 390320151:155:90:201
Meng Feifei 80042789 :250:60:50
wu Waiwai 70271111 :250:100:175
wang xiaoai 3515064655 :50:95:135
Zi Gege 1986786350 :250:168:200
Li Youjiu 918391635 :175:75:300
Lao Nanhai 918391685 :250:100:175
[root@oldboy02 files]# awk ‘/1/‘ files
Zhang Dandan 41116397 :250:100:175
Zhang xiaoyu 390320151:155:90:201
wu Waiwai 70271111 :250:100:175
wang xiaoai 3515064655 :50:95:135
Zi Gege 1986786350 :250:168:200
Li Youjiu 918391635 :175:75:300
Lao Nanhai 918391685 :250:100:175
[root@oldboy02 files]# #显示xiaoyu的姓氏和ID号码
[root@oldboy02 files]# awk ‘/xiaoyu/{print$1,$2, $3}‘ files
Zhang xiaoyu 390320151:155:90:201
[root@oldboy02 files]# awk ‘$2~/xiaoyu/‘ files (~号表示匹配xiaoyu的行)
Zhang xiaoyu 390320151:155:90:201
awk ‘$2~/xiaoyu/{print $1,$2,$3}‘ files (表示第二列中包含xiaoyu的行,精确到某一列的内容)
Zhang xiaoyu 390320151
[root@oldboy02 files]# awk ‘$0~/xiaoyu/‘ files ($0 表示这一整行)
Zhang xiaoyu 390320151 :155:90:201
[root@oldboy02 files]# awk ‘/xiaoyu/‘ files
Zhang xiaoyu 390320151 :155:90:201
[root@oldboy02 files]# awk ‘$3 ~/^41/‘ files
Zhang Dandan 41116397 :250:100:175
Meng Feifei 4180042789 :250:60:50
[root@oldboy02 files]# awk ‘$3 ~/^41/‘ files (第三列以41开头的行)
Zhang Dandan 41116397 :250:100:175
Meng Feifei 4180042789 :250:60:50
# #显示所有已41开头的行的ID号码的人的全名和ID号码
[root@oldboy02 files]# awk ‘$3 ~/^41/{print $1,$2,$3}‘ files
Zhang Dandan 41116397
Meng Feifei 4180042789
## 显示第三例结尾是1或5行的人的全名和ID号码
[root@oldboy02 files]# awk ‘$3~/[1,5]$/‘ files
Zhang xiaoyu 390320151 :155:90:201
wu Waiwai 70271111 :250:100:175
wang xiaoai 3515064655 :50:95:135
Li Youjiu 918391635 :175:75:300
Lao Nanhai 918391685 :250:100:175
[root@oldboy02 files]# awk ‘$3~/[1,5]$/{print $1,$2}‘ files
Zhang xiaoyu
wu Waiwai
wang xiaoai
Li Youjiu
Lao Nanhai
显示第三例结尾是1或5行的人的全名和ID号码
[root@oldboy02 files]# awk ‘$3~/[1,5]$/{print $1,$2,$3}‘ files
Zhang xiaoyu 390320151
wu Waiwai 70271111
wang xiaoai 3515064655
Li Youjiu 918391635
Lao Nanhai 918391685
:冒号替换成$符号
[root@oldboy02 files]# sed ‘s#:#$#g‘ files
Zhang Dandan 41116397 $250$100$175
Zhang xiaoyu 390320151 $155$90$201
Meng Feifei 4180042789 $250$60$50
wu Waiwai 70271111 $250$100$175
wang xiaoai 3515064655 $50$95$135
Zi Gege 1986786350 $250$168$200
Li Youjiu 918391635 $175$75$300
Lao Nanhai 918391685 $250$100$17
awk ‘{gsub(/匹配/,"替换成什么",$4);print}‘ 固定格式
[root@oldboy02 files]# awk ‘{gsub(/:/,"$",$4);print}‘ files
Zhang Dandan 41116397 $250$100$175
Zhang xiaoyu 390320151 $155$90$201
Meng Feifei 4180042789 $250$60$50
wu Waiwai 70271111 $250$100$175
wang xiaoai 3515064655 $50$95$135
Zi Gege 1986786350 $250$168$200
Li Youjiu 918391635 $175$75$300
Lao Nanhai 918391685 $250$100$175
匹配第二列的/xiaoyu/,并将:替换为$
[root@oldboy02 files]# awk ‘$2~/xiaoyu/{gsub(/:/,"$");print}‘ files (gsub 类似于sed的功能 gsub 表示替换的意思)
Zhang xiaoyu 390320151 $155$90$201
[root@oldboy02 files]# awk ‘/^$/{print NR}‘ /etc/services
22
266
299
320
326
[root@oldboy02 files]# awk ‘/^$/{i=i+1;print i}‘ /etc/services (统计出现的次数)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@oldboy02 files]# awk ‘/^$/{i=i+1}END{print i}‘ /etc/services (统计总共有多少行,END表示在awk 执行完毕后再进行计算总共有多少行)先计算后统计
16
awk 用法详解
标签:files 统计 显示 i+1 awk services sub file nbsp
原文地址:https://www.cnblogs.com/yangjuncheng0826/p/10015395.html