码迷,mamicode.com
首页 > 其他好文 > 详细

awk 用法

时间:2018-11-25 14:32:59      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:内容   冒号   动作   and   files   zhang   div   执行   end   

[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 用法

标签:内容   冒号   动作   and   files   zhang   div   执行   end   

原文地址:https://www.cnblogs.com/yangjuncheng0826/p/10015393.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!