Shell編程(week4_day5)--技術流ken


 

本節內容

 

1. 三劍客簡介

2. sed命令詳解

3. awk命令詳解

 

文本處理三劍客

 

在 Shell 下使用這些正則表達式處理文本最多的命令有下面幾個工具:

                命令                  

               描述                      

grep

                           

默認不支持擴展表達式,加-E 選項開啟 ERE。如果不加-E 使用花括號要加轉義符\{\}

                                                                                        

egrep

支持基礎和擴展表達式

awk

支持 egrep 所有的正則表達式

sed

默認不支持擴展表達式,加-r 選項開啟 ERE。如果不加-r 使用花括號要加轉義符\{\}

 

 

 sed詳解

 

1. 前言

 

  • 我們都知道,在Linux中一切皆文件,比如配置文件,日志文件,啟動文件等等。如果我們相對這些文件進行一些編輯查詢等操作時,我們可能會想到一些vi,vim,cat,more等命令。但是這些命令效率不高,而在linux中有三種工具:頂配awk,中配sed,標配grep。使用這些工具,我們能夠在達到同樣效果的前提下節省大量的重復性工作,提高效率。

  • 文件內容可以是來自文件,也可以直接來自鍵盤或者管道等標准輸入,最后的結果默認情況下是顯示到終端的屏幕上,但是也可以輸出到文件中。

  • 編輯文件也是這樣,以前我們修改一個配置文件,需要移動光標到某一行,然后添加點文字,然后又移動光標到另一行,注釋點東西.......可能修改一個配置文件下來需要花費數十分鍾,還有可能改錯了配置文件,又得返工。這還是一個配置文件,如果數十個數百個呢?因此當你學會了sed命令,你會發現利用它處理文件中的一系列修改是很有用的。只要想到在大約100多個文件中,處理20個不同的編輯操作可以在幾分鍾之內完成,你就會知道sed的強大了。

 

2. 語法格式

 

sed [選項]  [sed命令]  [輸入文件]

說明: 

1,注意sed軟件以及后面選項,sed命令和輸入文件,每個元素之間都至少有一個空格。 

2,sed -commands(sed命令)是sed軟件內置的一些命令選項,為了和前面的options(選項)區分,故稱為sed命令 

3,sed -commands 既可以是單個sed命令,也可以是多個sed命令組合。

4,input -file (輸入文件)是可選項,sed還能夠從標准輸入如管道獲取輸入。

 

3. sed的工作原理

 

sed讀取一行,首先將這行放入到緩存中

然后,才對這行進行處理

處理完成以后,將緩沖區的內容發送到終端

存儲sed讀取到的內容的緩存區空間稱之為:模式空間(Pattern Space)

 

4. 選項說明

 

option[選項]

解釋說明(帶*的為重點)

-n (no)

取消默認的sed軟件的輸出,常與sed命令的p連用。*

-e (entry)

一行命令語句可以執行多條sed命令   *

-r (ruguler)

使用擴展正則表達式,默認情況sed只識別基本正則表達式  *

-i (inside)

直接修改文件內容,而不是輸出到終端,如果不使用-i選項sed軟件只是修改在內存中的數據,並不會影響磁盤上的文件*

 
sed -commands[sed命令]

解釋說明(帶*的為重點)

a (append)

追加,在指定行后添加一行或多行文本 *                                                      

c (change)

取代指定的行

d (delete)

刪除指定的行  *  

i (insert)

插入,在指定行前添加一行或多行文本 *

p (print)

打印模式空間內容,通常p會與選項-n一起使用*

特殊符號

解釋說明(帶*的為重點)   

!                                         

對指定行以外的所有行應用命令*                                                              

 

 sed增刪改查

 

1. 增

 

這里我們需要用到2個sed命令,分別是:

  •   “a”:追加文本到指定行后,記憶方法:a的全拼是apend,意思是追加。
  •    “i“:插入文本到指定行前,記憶方法:i的全拼是insert,意思是插入。

 

實例1:a

[root@ken ~]# sed "2a 這是新添加的一行" test
this is the first line
this is the second line
這是新添加的一行
this is the third line
this is the forth line
this is the fivth line
this is the sixth line
this is the seventh line
this is the eighth line
this is the ninth line
this is the tenth line
  1. 2代表指定對第2行操作,其他的行忽略
  2. a代表插入的意思,2i即在第2行前插入文本
  3. 2a后面加上空格,然后跟上你想要插入的文本即可

 

實例2:i

[root@ken ~]# sed "2i 我又新添加了一行" test
this is the first line
我又新添加了一行
this is the second line
this is the third line
this is the forth line
this is the fivth line
this is the sixth line
this is the seventh line
this is the eighth line
this is the ninth line
this is the tenth line

 

實例3:同時增加多行(/n)

[root@ken ~]# sed "2i 這是第一條記錄\n這是第二條記錄\n這是第三條記錄" test
this is the first line
這是第一條記錄
這是第二條記錄
這是第三條記錄
this is the second line
this is the third line
this is the forth line
this is the fivth line
this is the sixth line
this is the seventh line
this is the eighth line
this is the ninth line
this is the tenth line

 

2.刪

 

  • 這個功能也是非常得有用,比如我們想刪除文件中的某些行,以前最常用的是vi或vim命令,但現在我們知道了sed命令,就應該使用這個高逼格的命令完成任務了。
  • “d”:刪除文本,記憶方法:d的全拼是delete,意思是刪除。
  • sed軟件可以對單行或多行文本進行處理。如果在sed命令前面不指定地址范圍,那么默認會匹配所有行。

 

實例1:刪除所有的行

[root@ken ~]# cp test{,.bak}
[root@ken ~]# sed 'd' test

命令說明:如果在sed命令前面不指定地址范圍,那么默認會匹配所有行,然后使用d命令刪除功能就會刪除這個文件的所有內容

 

實例2:刪除指定的行

[root@ken ~]# cat test.bak >test
[root@ken ~]# sed '2d' test
this is the first line
this is the third line
this is the forth line
this is the fivth line
this is the sixth line
this is the seventh line
this is the eighth line
this is the ninth line
this is the tenth line

 

實例3:刪除指定范圍行

[root@ken ~]# sed '2,5d' test
this is the first line
this is the sixth line
this is the seventh line
this is the eighth line
this is the ninth line
this is the tenth line

 

實例4:刪除匹配的行

[root@ken ~]# sed '/sixth/d' test
this is the first line
this is the second line
this is the third line
this is the forth line
this is the fivth line
this is the seventh line
this is the eighth line
this is the ninth line
this is the tenth line

 

命令說明:在sed軟件中,使用正則的格式和awk一樣,使用2個”/“包含指定的正則表達式,即“/正則表達式/”。

 

實例5:刪除指定行到行尾的內容

[root@ken ~]# sed '2,$d' test
this is the first line

第二行也會被刪掉

 

實例6:取反

一、

[root@ken ~]# sed '2,3!d' test
this is the second line
this is the third line

 

二、

[root@ken ~]# sed '/tenth/!d' test
this is the tenth line

 

3.改

 

  • “c”:用新行取代舊行,記憶方法:c的全拼是change,意思是替換。
[root@ken ~]# sed '2c 改過之后的第二行' test
this is the first line
改過之后的第二行
this is the third line
this is the forth line
this is the fivth line
this is the sixth line
this is the seventh line
this is the eighth line
this is the ninth line
this is the tenth line
this is sixth line

 

文本替換

 

    • 接下來說的這個功能,有工作經驗的同學應該非常的熟悉,因為使用sed軟件80%的場景就是使用替換功能。
    • 這里用到的sed命令,選項:
      “s”:單獨使用-->將每一行中第一處匹配的字符串進行替換==>sed命令
      “g”:每一行進行全部替換-->sed命令s的替換標志之一(全局替換),非sed命令。
      “-i”:修改文件內容-->sed軟件的選項,注意和sed命令i區別。

 

sed軟件替換模型

 

sed -i 's/目標內容/替換內容/g'  ken.log
sed -i 's#目標內容#替換內容#g'

 

實例1:

[root@ken ~]# sed 's/line/hang/g' test
this is the first hang
this is the second hang
this is the third hang
this is the forth hang
this is the fivth hang
this is the sixth hang
this is the seventh hang
this is the eighth hang
this is the ninth hang
this is the tenth hang
this is sixth hang

命令說明:從上面命令的結果我們就知道sed命令默認不會修改文件的內容

 

實例2:

[root@ken ~]# sed -i 's/line/hang/g' test
[root@ken ~]# cat test
this is the first hang
this is the second hang
this is the third hang
this is the forth hang
this is the fivth hang
this is the sixth hang
this is the seventh hang
this is the eighth hang
this is the ninth hang
this is the tenth hang
this is sixth hang

命令說明:如果想真正的修改文件內容,我們就需要使用選項“-i”,這個要和sed命令“i”區分開來。同時我們可以發現命令執行后的結果是沒有任何輸出的。

 

4.查

 

  • 這個功能也是非常得有用,比如我們想查看文件中的某些行,以前最常用的是cat或more或less命令等,但這些命令有些缺點,就是不能查看指定的行。而我們用了很久的sed命令就有了這個功能了。而且我們前面也說過使用sed比其他命令vim等讀取速度更快!
  • 這里我們需要用到1個sed命令
  • “p”:輸出指定內容,但默認會輸出2次匹配的結果,因此使用-n選項取消默認輸出,記憶方法:p的全拼是print,意思是打印。

 

實例1:

[root@ken ~]# sed '2p' test
this is the first hang
this is the second hang
this is the second hang
this is the third hang
this is the forth hang
this is the fivth hang
this is the sixth hang
this is the seventh hang
this is the eighth hang
this is the ninth hang
this is the tenth hang
this is sixth hang
[root@ken ~]# sed -n '2p' test
this is the second hang

 

實例2:

[root@ken ~]# sed -n '2,5p' test
this is the second hang
this is the third hang
this is the forth hang
this is the fivth hang

 

實例3:

[root@ken ~]# sed -n '/ninth/p' test
this is the ninth hang

 

補充:-e多點操作

 

實例1:

[root@ken ~]# sed -e '2d' -e '5d' test
this is the first hang
this is the third hang
this is the forth hang
this is the sixth hang
this is the seventh hang
this is the eighth hang
this is the ninth hang
this is the tenth hang
this is sixth hang

 

實例2:

[root@ken ~]# sed -n -e '2p' -e '5p' test
this is the second hang
this is the fivth hang

 

 

sed用法總結

 

1.查找指定的字符串

例子:顯示/etc/passwd中保含root的行(顯示模式空間中的內容)

方法1:set '/root/p' /etc/passwd

方法2:cat /etc/passwd | sed '/root/p'

 

 

2.在指定的位置做增刪

例子:刪除以root為開頭的行

# sed '/^root/d' a.txt

例子:在包含root的行后添加一行 i am ken

# sed '/root/a i am ken' a.txt

 

3.按行替換

例子:將5到9行的內容替換為 i am ken

# sed '5,9c i am ken' a.txt

 

4.按照字符替換

例子:將/etc/selinux/config中的SELINUX=enforcing改成 disabled

寫法1:# sed -i 's/SELINUX=disabled/SELINUX=enforcing/g' config

寫法2:# sed -r -i 's/(SELINUX=)disabled/\1enforcing/g' config

 

5.查找指定的內容再做替換

例子:將以r開頭的行中的oo替換為qq

# sed '/^r/{s/oo/qq/g}' passwd

 

6.多點編輯

例子:去除文件中的注釋行和空白行

# grep -v -E "(^#)|(^$)" passwd.bak >passwd

# cat passwd.bak | sed -e '/^#/d' -e '/^$/d' >passwd

 

7)取反操作

顯示非1-3行

# sed -n '1,3!p' passwd

 

awk詳解

 

awk不僅僅時linux系統中的一個命令,而且是一種編程語言,可以用來處理數據和生成報告(excel)。處理的數據可以是一個或多個文件,可以是來自標准輸入,也可以通過管道獲取標准輸入,awk可以在命令行上直接編輯命令進行操作,也可以編寫成awk程序來進行更為復雜的運用。

 

awk的格式

 

  • awk指令是由模式,動作,或者模式和動作的組合組成。
  • 模式既pattern,可以類似理解成sed的模式匹配,可以由表達式組成,也可以是兩個正斜杠之間的正則表達式。比如NR==1,這就是模式,可以把他理解為一個條件。
  • 動作即action,是由在大括號里面的一條或多條語句組成,語句之間使用分號隔開。比如awk使用格式:

 

awk處理的內容可以來自標准輸入(<),一個或多個文本文件或管道。

    • pattern既模式,也可以理解為條件,也叫找誰,你找誰?高矮,胖瘦,男女?都是條件,既模式。
    • action既動作,可以理解為干啥,找到人之后你要做什么。
      模式和動作的詳細介紹我們放在后面部分,現在大家先對awk結構有一個了解。

 

awk參數

 

-F:指定分隔符

 

幾個小概念

 

記錄(record):一行就是一個記錄

分隔符(field separator):進行對記錄進行切割的時候所使用的字符

字段(field):將一條記錄分割成的每一段

FILENAME:當前處理文件的文件名

FS(Field Separator):字段分隔符(默認是以空格為分隔符=)

NR(Number of Rrecord):記錄的編號(awk每讀取一行,NR就加1==)

NF(Number of Field):字段數量(記錄了當前這條記錄包含多少個字段==)

ORS(Output Record Separator):指定輸出記錄分隔符(指定在輸出結果中記錄末尾是什么,默認是\n,也就是換行)

OFS(Output Field Separator):輸出字段分隔符

RS:記錄分隔符

 

輸出字段的表示方式

 

$1 $2 ... $n 輸出一個指定的字段

$NF 輸出最后一個字段

$0 輸出整條記錄

 

awk執行過程

 

[root@ken ~]# awk 'NR>=2&&NR<=5{print $0}' /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
 

命令說明: 條件NR>=2,表示行號大於等於2時候,執行{print $0}顯示整行。 awk是通過一行一行的處理文件,這條命令中包含模式部分(條件)和動作部分(動作),awk將處理模式(條件)指定的行

 

1)awk讀入第一行內容

2)判斷是否符合模式中的條件NR>=2

a,如果匹配則執行對應的動作{print $0}
b,如果不匹配條件,繼續讀取下一行

3)繼續讀取下一行
4)重復過程1-3,直到讀取到最后一行(EOF:end of file)

 

 

准備測試文件

[root@ken ~]# head /etc/passwd > test
[root@ken ~]# cat test
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

 

實例1:打印行號

[root@ken ~]# awk '{print NR,$0}' test
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10 operator:x:11:0:operator:/root:/sbin/nologin

 

實例2:輸出有多余5個字段的行的第三個字段

[root@ken ~]# awk -F ':' 'NF>=5{print $3}' test
0
1
2
3
4
5
6
7
8
11

 

實例3:輸出每行行號和該行有幾個字段

[root@ken ~]# awk -F ':' '{print NR,NF}' test
1 7
2 7
3 7
4 7
5 7
6 7
7 7
8 7
9 7
10 7

 

 awk進階--正則

 

  • 正則表達式的運用,默認是在行內查找匹配的字符串,若有匹配則執行action操作,但是有時候僅需要固定的列來匹配指定的正則表達式,比如:我想取/etc/passwd文件中第五列{$5}這一列查找匹配mail字符串的行,這樣就需要用另外兩個匹配操作符,並且awk里面只有這兩個操作符來匹配正則表達式。

 

實例1:匹配整行

[root@ken ~]# awk '/^root/' test
root:x:0:0:root:/root:/bin/bash

 

和下面的效果是一樣的

[root@ken ~]# awk '$0~/^root/' test
root:x:0:0:root:/root:/bin/bash

 

注意:awk只用正則表達式的時候是默認匹配整行的即‘$0~/^root/’和‘/^root/’是一樣的。

 

實例2:匹配一行中的某一列

[root@ken ~]# awk -F ':' '$5~/root/' test
root:x:0:0:root:/root:/bin/bash

 

提示:

  • $5表示第五個區域(列)
  • ~表示匹配(正則表達式匹配)
  • /root/表示匹配root這個字符串

$5~/root/表示第五個區域(列)匹配正則表達式/root/,既第5列包含root這個字符串,則顯示這一行。

 

實例3:匹配行尾為sync

[root@ken ~]# awk -F ':' '/sync$/{print $0}' test
sync:x:5:0:sync:/sbin:/bin/sync

 

實例4:顯示名字和登錄類型

[root@ken ~]# awk -F ':' '{print $1,$NF}' test
root /bin/bash
bin /sbin/nologin
daemon /sbin/nologin
adm /sbin/nologin
lp /sbin/nologin
sync /bin/sync
shutdown /sbin/shutdown
halt /sbin/halt
mail /sbin/nologin
operator /sbin/nologin

 

$NF:表示匹配的末尾部分,這里也可以寫成$7

 

實戰: 取出網卡IP地址(企業面試題)

[root@ken ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 1000
    link/ether 00:0c:29:99:ea:a6 brd ff:ff:ff:ff:ff:ff
    inet 172.20.10.6/24 brd 172.20.10.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 2408:84f4:86:47e1:20c:29ff:fe99:eaa6/64 scope global mngtmpaddr dynamic 
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe99:eaa6/64 scope link 
       valid_lft forever preferred_lft forever

 

第一種方法:

[root@ken ~]# ip a | awk -F ' +' 'NR==9{print $3}' | awk -F '/' '{print $1}'
172.20.10.6

 

第二種方法:

[root@ken ~]# ip a | grep -E '^ +.*inet\>.*' | awk -F ' +|/' 'NR==2{print $3}'
172.20.10.6

 

第三種方法:

[root@ken ~]# hostname -i | awk -F ' ' '{print $3}'
172.20.10.6

 

 第四種方法:

[root@ken ~]# ip a | grep brd.*glo | awk -F ' +|/' '{print $3}'
172.20.10.6

 

第五種方法:

[root@ken ~]# ip a | grep "scope" | awk 'NR==3{print $0}' | awk -F "( |/)+" '{print $3}'
172.20.10.6

 

方法還有很多很多,大家如果對自己有高要求的話,要至少寫出來十種以上的方法哦!

 

 awk特殊模式-BEGIN模式與END模式

 

  • BEGIN模塊再awk讀取文件之前就執行,一般用來定義我們的內置變量(預定義變量,eg:FS,RS)
  • 需要注意的是BEGIN模式后面要接跟一個action操作塊,包含在大括號內。awk必須在輸入文件進行任何處理前先執行BEGIN里的動作(action)。我們可以不要任何輸入文件,就可以對BEGIN模塊進行測試,因為awk需要先執行完BEGIN模式,才對輸入文件做處理。BEGIN模式常常被用來修改內置變量ORS,RS,FS,OFS等值。

 

BEGIN模塊

 

實例1:

[root@ken ~]# ifconfig eth0 | awk -F "[ :]+" 'NR==2{print $3}'
172.20.10.6
[root@ken ~]# ifconfig eth0 | awk -F "[^0-9.]+" 'NR==2{print $2}'
172.20.10.6

#上面的也可以寫成
[root@ken ~]# ifconfig eth0 | awk 'BEGIN{FS="[ :]+"}NR==2{print $3}'
172.20.10.6
[root@ken ~]# ifconfig eth0 | awk 'BEGIN{FS="[^0-9.]+"}NR==2{print $2}'
172.20.10.6

 

實例2:在讀取文件之前,輸出些提示性信息(表頭)。

[root@ken ~]# awk -F ':' 'BEGIN{print "username","bash type"}{print $1,$NF}' test
username bash type
root /bin/bash
bin /sbin/nologin
daemon /sbin/nologin
adm /sbin/nologin
lp /sbin/nologin
sync /bin/sync
shutdown /sbin/shutdown
halt /sbin/halt
mail /sbin/nologin
operator /sbin/nologin

 

END模塊

 

EHD在awk讀取完所有的文件的時候,再執行END模塊,一般用來輸出一個結果(累加,數組結果),也可以是和BEGIN模塊類似的結尾標識信息

與BEGIN模式相對應的END模式,格式一樣,但是END模式僅在awk處理完所有輸入行后才進行處理。

 

實例1:

[root@ken ~]# awk -F ':' 'BEGIN{print "username","bash type"}{print $1,$NF}END{print "end of file"}' test
username bash type
root /bin/bash
bin /sbin/nologin
daemon /sbin/nologin
adm /sbin/nologin
lp /sbin/nologin
sync /bin/sync
shutdown /sbin/shutdown
halt /sbin/halt
mail /sbin/nologin
operator /sbin/nologin
end of file

 

實例2:統計包含root的行的數量

 

方法一:

[root@ken ~]# cat test | grep root| wc -l
2

 

方法二:

[root@ken ~]# cat test | grep -c root
2

 

方法三:

[root@ken ~]# cat /etc/passwd | awk 'BEGIN{i=0}/root/{i++}END{print i}'
2
[root@ken ~]# cat /etc/passwd | awk '/root/{i++}END{print i}'
2

 

總結awk執行過程

 

回顧一下awk的結構

awk -F 指定分隔符 ‘BRGIN{}END{}’,如下圖

 

awk數組

 

數組構成:

數組名[元素名]=值

如圖不難發現,awk數組就和酒店一樣。數組的名稱就像是酒店名稱,數組元素名稱就像酒店房間號碼,每個數組元素里面的內容就像是酒店房間里面的人。

 

實戰:統計域名出現的次數(百度和搜狐面試題)

 

[root@ken ~]# cat test
http://www.qq.com/ken
http://www.qq.com/ken
http://www.qq.com/ken
http://www.qq.com/ken
http://www.qq.com/ken
http://www.qq.com/ken
http://www.qq.com/ken
http://www.sina.com/ken
http://www.sina.com/ken
http://www.sina.com/ken
http://www.sina.com/ken
http://www.sina.com/ken
http://www.sina.com/ken
http://www.sina.com/ken
http://www.sina.com/ken
http://www.sina.com/ken
http://www.sina.com/ken
http://www.sina.com/ken
http://www.sina.com/ken
http://www.sina.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken

 

方法一:

[root@ken ~]# cat test | awk -F '/+' '{print $2}' | sort | uniq -c
      7 www.qq.com
     13 www.sina.com
     25 www.taobao.com

 

方法二:

[root@ken ~]# cat test | awk -F '/+' '{h[$2]++}END{for (i in h) print i,h[i]}'
www.sina.com 13
www.qq.com 7
www.taobao.com 25

 

awk用法總結

 

1. 結合內置變量,打印指定的幾行,以及字段數量

例子;輸出有多余5個字段的行的第三個字段

# cat a.sh | awk -F ":" 'NF>=5{print $3}'

例子:輸出每行行號和該行有幾個字段

# cat a.sh | awk -F ":" '{print NR,NF}'

 

例子:輸出用戶名,要求所有用戶顯示在同一行,而且用空格分隔

# cat mypwd | awk 'BEGIN{FS=":"; ORS=" "}{print $1}'

 

 

2. 結合正則來匹配一行或者某個字段

例子:輸出用戶名以s為開頭的用戶的uid

# cat mypwd | awk -F ":" '/^s/{print $}'

例子:輸出第五個字段是以t為結尾的用戶的姓名

# cat mypwd | awk -F ":" '$5~/t$/{print $1}'

 

3. 采用比較符號來進行打印指定的某些行

例子:實現僅僅輸出3-5的內容,每行前面添加一個行號

# cat mypwd | awk 'NR>=3&&NR<=5{print NR,$1}'

# cat mypwd | awk 'NR==3,NR==5{print NR,$1}'

 

例子:實現僅僅輸出和 和 7行的內容,每行前面添加一個行號

# cat mypwd | awk 'NR==3||NR==5||NR==7{print NR,$1}'

 

4. END

例子:統計mypwd中以#開頭的行有多少行

# cat mypwd | awk 'BEGIN{n=0}/^#/{n+=1}END{print n}'

 

統計:mypwd中,以:為分隔符,字段數量在3-5的行的數目

# cat mypwd  | awk 'BEGIN{FS=":"}NF>=3&&NF<=5{n+=1}END{print n}'

 

5. ip

例子:統計IP

[root@ken]# cat url.txt | awk -F "/+" '{urls[$2]++}END{for(key in urls)print key, urls[key]}’

www.baidu.com 12

haha.baidu.com 1

ftp.baidu.com 6

mail.baidu.com 7

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM