awk\sed\grep的使用

 

1     grep

1.1   參數:

-v:html

取反、按行排除,【^abc-->abc以外,【abc】--->包含abcweb

-E:正則表達式

grep支持擴展正則,==egrep數組

-o:bash

顯示執行過程(顯示正則每次匹配到的內容)oracle

-c:app

統計行數,wc -l函數

-i:post

忽略大小寫 -ignore-case(find  -iname也是忽略大小寫)測試

-l:

過濾的時候只顯示文件名 ,不顯示文件內容

-A

After,[root@localhost files]# grep -A2 oldboy person.txt

顯示oldboy這行以及下面2行

-o

顯示執行過程

1.2   案例:

書寫腳本,判斷crond是否在運行

1)若是在運行提示

crond is running        [ok]

2)若是沒有在運行提示

crond is  not running    [Failed]

 

第一步:檢查進程時

[root@localhost files]# ps -ef |grep  crond

顯示正在運行的進程

root       1390      1  0 14:27 ?        00:00:00 crond

root       1634   1596  0 14:44 pts/0    00:00:00 grep crond

 

第二步:腳本名字不能包含服務名稱

法一:過濾grep本身

[root@localhost files]# ps -ef |grep  crond|grep -v grep

root       1390      1  0 14:27 ?        00:00:00 crond

 

 法二:精確過濾想要的

[root@localhost files]# ps -ef |grep  '[c]rond'

root       1390      1  0 14:27 ?        00:00:00 crond

 

[root@localhost files]# ps -ef |grep -c  '[c]rond'

       -c:統計行數

第三步:寫腳本

/bin/bash

. /etc/init.d/functions   #加顏色,系統預約義的功能(函數)

count=`ps -ef|grep -c '[c]rond'`

 

if [ $count -eq 1 ];

        action "crond is running"  /bin/true

else

        action "crond is not running" /bin/false

fi

 

 

2     sed

2.1   參數:

-n:

取消默認輸出

-r:

支持擴展正則

-i:

先備份文件,在修改
不要寫成-ir-----》先備份爲   文件名r   文件名

Sed -n   '3p'    oldboy.txt

3p:條件命令,p是顯示

2.2   查

(1):顯示第2行

Sed -n   '2p'    oldboy.txt

(2):顯示文件passwd第3行和第10行

[root@localhost files]# sed -n '3p;10p' /etc/passwd

daemon:x:2:2:daemon:/sbin:/sbin/nologin

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

(3):顯示文件passwd第3行到第10行

[root@localhost files]# sed -n '3,10p' /etc/passwd

(4):找出person.txt文件中包含oldboy的行

Sed -n '//p'

[root@localhost files]# sed -n '/oldboy/p' oldboy.txt

5:顯示文件中包含103110的行

[root@localhost files]# sed -n '/103/,/110/p' person.txt

 

6:顯示包含yy的行到文件最後一行的內容

[root@localhost files]# sed -n '/yy/,$p' person.txt

104,yy,CFO

105,feixue,CIO

110,lidao,COCO

7:特別用法

顯示包含oldboy以及oldboy下面兩行

[root@localhost files]# sed -n /oldboy/,+2p person.txt

顯示第一行以及第一行下面的兩行

[root@localhost files]# sed -n '1~2p' person.txt

101,txt

103,Alex,COO

105,feixue,CIO

2.3   增

參數

含義

a

append追加  指定行後面追加

i

Insert 插入  指定行前面追加

c

Replace 替換  把這一行的內容替換爲你寫的

追加20,maolaoshi,UFO到3行後面

[root@localhost files]# sed '3a20,maolaoshi,UFO' person.txt

101,txt

102,ail

103,Alex,COO

20,maolaoshi,UFO

 

[root@localhost files]# sed '3a20,maolaoshi,UFO\n24,lao' person.txt

101,txt

102,ail

103,Alex,COO

20,maolaoshi,UFO

24,lao

[root@localhost files]# sed '3c120,maolaoshi,UFO\n24,lao' person.txt

101,txt

102,ail

120,maolaoshi,UFO

24,lao

104,yy,CFO

105,feixue,CIO

110,lidao,COCO

 

[root@localhost files]# sed '3i120,maolaoshi,UFO\n24,lao' person.txt

101,txt

102,ail

120,maolaoshi,UFO

24,lao

103,Alex,COO

2.4   刪

d

(delete)

1d

刪第一行

/oldboy/d

刪oldboy行

2.5   改

‘s###g’

後向引用

2.5.1 案例:

把/etc/passwd 第1列和最後一列調換位置

準備好環境

head /etc/passwd >passwd.txt 

修改

2.5.1.1  法一:

[root@localhost files]# sed -r 's#^(.*?):(.*):(.*?)$#\3\2\1#g' passwd.txt

^(.*?):(.*):(.*?)

(.*?):表示到第一個:前面,爲何加?,由於要取消正則的貪婪性
(.*)表示一直到最後一個:
(.*?)表示最後一個部分

 

/bin/bash:/root:root:x:0:0:root

/sbin/nologin:/bin:bin:x:1:1:bin

/sbin/nologin:/sbin:daemon:x:2:2:daemon

/sbin/nologin:/var/adm:adm:x:3:4:adm

/sbin/nologin:/var/spool/lpd:lp:x:4:7:lp

/bin/sync:/sbin:sync:x:5:0:sync

/sbin/shutdown:/sbin:shutdown:x:6:0:shutdown

/sbin/halt:/sbin:halt:x:7:0:halt

/sbin/nologin:/var/spool/mail:mail:x:8:12:mail

/sbin/nologin:/var/spool/uucp:uucp:x:10:14:uucp

https://www.processon.com/view/link/5b78e46be4b067df5a0f903f

2.5.1.2   法二: 

[root@localhost files]# egrep -o '^[^:]+'  passwd.txt

 

-o

顯示執行過程

[^:]+

取反

^[^:]+

取消貪婪性

root

bin

daemon

adm

lp

sync

shutdown

halt

mail

uucp

 

[root@localhost files]# sed -r 's#(^[^:]+)(.*:)(.*)#\3\2\1#g' passwd.txt

 

/bin/bash:x:0:0:root:/root:root

2.6    補充

‘’和「」的區別

‘:所見即所得

「:裏面的特殊符號有特殊意義

 

3     awk

3.1   參數:

-F:

指定分隔符,-F 修改awk內置變量FS(field-separator)

-v

修改或建立awk的變量

-f

指定腳本

 

3.2   案例:

1:統計磁盤使用率大於50%

[root@localhost files]# df -h

Filesystem            Size  Used Avail Use% Mounted on

/dev/mapper/VolGroup-lv_root

                       18G  4.7G   12G  29% /

tmpfs                 491M     0  491M   0% /dev/shm

/dev/sda1             477M   36M  417M   8% /boot

/dev/sr0              3.7G  3.7G     0 100% /media

[root@localhost files]# df -h | awk '$5>50'

Filesystem            Size  Used Avail Use% Mounted on

/dev/sda1             477M   36M  417M   8% /boot

 

2:-v的使用 

 

[root@localhost files]# df -h | awk 'NR >1&&$5>50'

NR:行)

 

/dev/sda1             477M   36M  417M   8% /boot

 

[root@localhost files]# awk -va=5 'BEGIN{print a}'

變量a=5

 

5

[root@localhost files]# awk -va=5 'BEGIN{print a^a}'

3125

 

 3:打印第一列

法一:

[root@localhost files]# awk -F:   '{print $1}' passwd.txt

-F:指定分隔符

root

bin

daemon

 法二:

[root@localhost files]# awk -vFS=:   '{print $1}' passwd.txt

root

bin

daemon

3.3   awk的格式:

awk -F:   'NR==1{print  $1,$3}'

                    '條件{動做}'

3.3.1 條件

3.3.1.1  正則表達式(支持擴展正則)

^

以…開頭的字符串

&

讓程序在後臺運行

$

以…結尾的字符串

*

0次或屢次

!

取反?????

.*

全部

[]

中括號裏面任意一個字符[abc]

+

出現1次以及1次以上

|

{}

前一個字符出現幾回{n,m}

()

分組

?

0次或1次

3.3.1.1.1     案例

環境:

mkdir -p /server/files/

cat >>/server/files/reg.txt<<EOF

Zhang Dandan    41117397   :250:100:175

Zhang Xiaoyu    390320151  :155:90:201

Meng  Feixue    80042789   :250:60:50

Wu    Waiwai    70271111   :250:80:75

Liu   Bingbing  41117483   :250:100:175

Wang  Xiaoai    3515064655 :50:95:135

Zi    Gege      1986787350 :250:168:200

Li    Youjiu    918391635  :175:75:300

Lao   Nanhai    918391635  :250:100:175

EOF

1顯示第3列中以4開頭的

[root@localhost files]# awk '$3~/^4/' reg.txt

'$3~/^4

~:表示匹配;匹配第3列中以4開頭的

 

Zhang Dandan    41117397   :250:100:175

Zhang Xiaoyu    390320151  :155:90:201

Meng  Feixue    80042789   :250:60:50

Wu    Waiwai    70271111   :250:80:75

Liu   Bingbing  41117483   :250:100:175

Wang  Xiaoai    3515064655 :50:95:135

Zi    Gege      1986787350 :250:168:200

Li    Youjiu    918391635  :175:75:300

Lao   Nanhai    918391635  :250:100:175

 

2:顯示Xiaoyu的姓氏和ID號碼

[root@localhost files]# awk '$2~/Xiaoyu/{print $1,$3}' reg.txt

Zhang 390320151

Zhang 390320151

 

[root@localhost files]# awk '/Xiaoyu/' reg.txt

 

注意:/Xiaoyu/至關於$0~/Xiaoyu/至關於/Xiaoyu/{print $0}

3:顯示全部以41開頭的ID號碼的人的全名和ID號碼

[root@localhost files]# awk '$3~/^41/{print $2,$3}' reg.txt

Dandan 41117397

Bingbing 41117483

Dandan 41117397

Bingbing 41117483

 

4:顯示全部ID號碼最後一位數字是15的人的全名

[root@localhost files]# awk '$3~/[15]$/{print $2,$3}' reg.txt

Xiaoyu 390320151

Waiwai 70271111

Xiaoai 3515064655

Youjiu 918391635

Nanhai 918391635

Xiaoyu 390320151

Waiwai 70271111

Xiaoai 3515064655

Youjiu 918391635

Nanhai 918391635

 

[root@localhost files]# awk '$3~/[15]$/{print $2,$3}' reg.txt|column -t

 

column -t

對齊顯示

Xiaoyu  390320151

Waiwai  70271111

Xiaoai  3515064655

Youjiu  918391635

Nanhai  918391635

Xiaoyu  390320151

Waiwai  70271111

Xiaoai  3515064655

Youjiu  918391635

Nanhai  918391635

 

 

5:顯示Xiaoyu的捐款.每一個值時都有以$開頭.$520$200$135

 

法一:

 

[root@localhost files]# awk '/Xiaoyu/{print $4}' reg.txt|tr ":" "$"

$155$90$201

$155$90$201

法2:

[root@localhost files]# awk '/Xiaoyu/{print $4}' reg.txt|sed 's#:#$#g'

's#:#$#g

s:substitite

 

$155$90$201

$155$90$201

 

法3:

[root@localhost files]# awk '{gsub(/:/,"$");print}' reg.txt

Zhang Dandan    41117397   $250$100$175

Zhang Xiaoyu    390320151  $155$90$201

Meng  Feixue    80042789   $250$60$50

 

[root@localhost files]# awk '/Xiaoyu/{gsub(/:/,"$");print}' reg.txt

Zhang Xiaoyu    390320151  $155$90$201

Zhang Xiaoyu    390320151  $155$90$201

 

 

gsub(//,"",$NF)

(把誰,替換成什麼,在哪裏替換)

gsub(//,"")

至關於gsub(//,"",$0)

 

3.3.1.2  比較表達式(如NR==1)

顯示出系統磁盤分區使用率大於20%的磁盤 設備名稱和掛載點

[root@localhost files]# df -h|awk '$5>20{print $1,$NF}'|column -t

Filesystem  on

/dev/sda1   /boot

去掉第一行的方法:

法1:

[root@localhost files]# df -h|awk '$5>20&&NR>1 {print $1,$NF}'|column -t

/dev/sda1  /boot

 法2:

強制轉換--》第5列的內容轉換爲數字

[root@localhost files]# df -h|awk 'int($5)>20 {print $1,$NF}'|column -t

/dev/sr0  /media

 

3.3.1.3  範圍的表達式(如1,3p)

1:顯示第幾行到第幾行

[root@localhost files]# awk 'NR==1,NR==3' reg.txt

Zhang Dandan    41117397   :250:100:175

Zhang Xiaoyu    390320151  :155:90:201

Meng  Feixue    80042789   :250:60:50

[root@localhost files]# awk '/101/,/104/' person.txt

101,txt

102,ail

103,Alex,COO

104,yy,CFO

 

2:顯示{}中lidao出現的次數

準備環境:

cat >>lidao.txt<<EOF

oldboy

lidao

oldboy

{

lidao 

lidao 

lidao 

lidao 

lidao 

}

lidao

oldboy

{

lidao 

lidao 

lidao 

lidao 

lidao 

}

EOF

[root@localhost files]# awk '/{/,/}/' lidao.txt

{

lidao

lidao

lidao

lidao

lidao

}

{

lidao

lidao

lidao

lidao

lidao

}

[root@localhost files]# awk '/{/,/}/' lidao.txt |grep lidao -c

10

 

3.3.1.4   特殊的條件

BEGIN{}

BEGIN{}裏面的內容 會在awk讀取文件以前執行
1:簡單計算
2:測試
3:修改awk內置變量
-vFS:至關於BEGIN{FS:}

END{}

END{}裏面的內容  會在awk讀取完文件內容以後運行
1:顯示計算結果
先計算,最後END{}顯示結果

1:統計/etc/services 文件中空行的數量

法一:

[root@localhost files]# awk '/^$/' /etc/services|wc -l

16

法二:

[root@localhost files]# grep '^$' /etc/services|wc -l

16

法三:

[root@localhost files]# awk '/^$/{i=i+1;print NR,i}' /etc/services

i =i+1 

 統計數量  一共有多少個         統計

 

22 1

266 2

299 3

320 4

326 5

393 6

461 7

474 8

479 9

486 10

494 11

506 12

512 13

518 14

583 15

584 16

[root@localhost files]# awk '/^$/{i=i+1}END{print i}' /etc/services

16

 

2access.log  1 ip地址  7列 訪問的網站的文件 第10列 每一個資源的大小(圖片 頁面)                  

  

統計一共使用了多少流量

 

[root@localhost files]# awk  '{i=i+$10}END{print i}' access.log

2478496663

 

[root@localhost files]# awk  '{i=i+$10}END{print i/1024^3"GB"}' access.log

2.30828GB

3:計算1+。。。。+10的結果

[root@localhost files]# seq 10|awk  '{i=i+$1}END{print i}'

55

 

小結:

i= i+1

計數i++

i=i+$xxx

計算求和 i += $xx

3.4   awk數組

h[120]="lidao;

數字名稱【元素/下標】

 

 

1:統計每一個域名出現的次數(去重統計次數)

環境:

http://www.etiantian.org/index.html

http://www.etiantian.org/1.html

http://post.etiantian.org/index.html

http://mp3.etiantian.org/index.html

http://www.etiantian.org/3.html

http://post.etiantian.org/2.html

 

[root@localhost files]# awk -F"[/.]+"  '{print $2}' url.txt

 

www

www

post

mp3

www

post

[root@localhost files]# awk -F"[/.]+"  '{print $2}' url.txt|sort

 

Sort

排序

 

mp3

post

post

www

www

www

 

[root@localhost files]# awk -F"[/.]+"  '{print $2}' url.txt|sort|uniq -c

Uniq

這個只在相鄰的去重

 

      1

      1 mp3

      2 post

      3 www

 

[root@localhost files]# awk -F"[/.]+"  '{print $2}' url.txt|sort|uniq -c|sort -rn

      3 www

      2 post

      1 mp3

      1

 

2:顯示awk數組內容

[root@localhost files]# awk 'BEGIN{h[120]="lidao";h[12306]="ma" ;print h[120],h[12306]             }'

lidao ma

 

[root@localhost files]# awk -F"[/.]+" '{h[$2]++}END{print h["www"],h["post"],h["mp3"]}' url.txt

 

3 2 1

 

 

[root@localhost files]# awk -F"[/.]+" '{h[$2]++}END{for (p in h) print p}' url.txt

 

www

mp3

post

 

[root@localhost files]# awk -F"[/.]+" '{h[$2]++}END{for (p in h) print p,h[p]}' url.txt

 1

www 3

mp3 1

post 2

3:統計secure文件中 每一個用戶被破解的次數

[root@localhost files]# awk '/Failed/{print $(NF-5)}' secure-20161219|head

support

admin

uucp

business

business

business

ftp

ftp

ftp

root

 

[root@localhost files]# awk '$6~/Failed/{h[$(NF-5)]++}END{for (k in h) {print k, h[k]}}' secure-20161219 | sort -nrk2 | head|column -t

root      364610

admin     733

user      246

oracle    119

support   104

guest     79

test      70

ubnt      47

pi        41

webadmin  36

 

 

4:統計access.log中每一個ip地址使用的流量(顯示前10名)

[root@localhost files]# awk '{h[$1]+=$10}END{for (p in h)print p,h[p]}' access.log |sort -hrk2|head

114.83.184.139 31362956

117.136.66.10 22431302

116.216.30.47 21466000

223.104.5.197 21464856

116.216.0.60 19145329

114.141.164.180 17219553

114.111.166.22 17121524

223.104.5.202 16911512

116.228.21.187 15969887

112.64.171.98 15255013

3.5   awk的執行過程 

 

 

3.6   查詢幫助

man  awk

info awk   ======GAWK:Effective AWK  Programming

相關文章
相關標籤/搜索