awk 用法

Posted yangjuncheng0826

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了awk 用法相关的知识,希望对你有一定的参考价值。

[[email protected] ~]# # awk 参数 ‘模式{动作}‘  文件
[[email protected] ~]# # awk 参数 ‘条件(找谁)  {干啥}‘ 文件
 
[[email protected] ~]# awk -F ":" ‘NR==2{print $2,$4}‘ /etc/passwd
x 1
[[email protected] ~]# cat /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin 
[[email protected] ~]# mkdir -p /server/files
[[email protected] ~]# cd /server/files/
[[email protected] 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
[[email protected] files]# ls
files
[[email protected] 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
[[email protected] 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
[[email protected] files]# #显示xiaoyu的姓氏和ID号码
[[email protected] files]# awk ‘/xiaoyu/{print$1,$2, $3}‘ files
Zhang xiaoyu 390320151:155:90:201
[[email protected] files]# awk ‘$2~/xiaoyu/‘ files (~号表示匹配xiaoyu的行)
Zhang xiaoyu 390320151:155:90:201
 awk ‘$2~/xiaoyu/{print $1,$2,$3}‘ files (表示第二列中包含xiaoyu的行,精确到某一列的内容)
Zhang xiaoyu 390320151
[[email protected] files]# awk ‘$0~/xiaoyu/‘ files ($0 表示这一整行)
Zhang xiaoyu 390320151  :155:90:201
[[email protected] files]# awk ‘/xiaoyu/‘ files
Zhang xiaoyu 390320151  :155:90:201
[[email protected] files]# awk ‘$3 ~/^41/‘ files
Zhang Dandan 41116397  :250:100:175
Meng Feifei  4180042789   :250:60:50
[[email protected] files]# awk ‘$3 ~/^41/‘ files (第三列以41开头的行)
Zhang Dandan 41116397  :250:100:175
Meng Feifei  4180042789   :250:60:50
# #显示所有已41开头的行的ID号码的人的全名和ID号码
[[email protected] files]# awk ‘$3 ~/^41/{print $1,$2,$3}‘ files
Zhang Dandan   41116397
Meng Feifei       4180042789
## 显示第三例结尾是1或5行的人的全名和ID号码
[[email protected] 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
[[email protected] files]# awk ‘$3~/[1,5]$/{print $1,$2}‘ files
Zhang xiaoyu
wu Waiwai
wang xiaoai
Li Youjiu
Lao Nanhai
显示第三例结尾是1或5行的人的全名和ID号码
[[email protected] 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
:冒号替换成$符号
[[email protected] 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}‘ 固定格式
[[email protected] 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/,并将:替换为$
[[email protected] files]# awk ‘$2~/xiaoyu/{gsub(/:/,"$");print}‘ files (gsub 类似于sed的功能 gsub 表示替换的意思)
Zhang xiaoyu 390320151  $155$90$201
[[email protected] files]# awk ‘/^$/{print NR}‘ /etc/services
22
266
299
320
326
[[email protected] 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
 
[[email protected] files]# awk ‘/^$/{i=i+1}END{print i}‘ /etc/services (统计总共有多少行,END表示在awk 执行完毕后再进行计算总共有多少行)先计算后统计
16
 
 
 
 

以上是关于awk 用法的主要内容,如果未能解决你的问题,请参考以下文章

awk 的一些用法

awk的用法

awk用法

awk的基本概念,基础用法和高级用法

awk用法

awk用法