sed & awk

基本正則
. 匹配任意一個字符,除了換行符,在sed中不能匹配換行符,可是在awk中能夠匹配換行符
* 表示前邊字符有0個或多個
[] [a-z] [0-9] [^0-9] 非數字的git

^ 行首 ^abc:以abc開頭
$ 行尾 world$:以world結尾
{}
\{2\}:表示前邊字符的重複次數是2。
\{2,\}:表示前邊字符的重複次數至少是2,也就是大於等於2。
\{2,9\}:表示前邊字符的重複次數大於2但小於9。正則表達式

擴展正則
? 前置字符有0個或1個
+ 前置字符有1個或多個
| 或
() 分組
{} 和基本正則表達式中的大括號意義是同樣的,只不過在使用時不用加"\"轉義符號。函數

 

echo ui

-e  處理特殊字符spa

#  echo -e '123\t456'
123 456server

# echo -e '123\n456'
123
456rpc

sed

sed [選項] ‘command’ 文件名稱it

選項:-n,-e,-i,-f,-r選項。io

command:[地址1,地址2] [函數] [參數(標記)]ast

經常使用選項:

-n 選項後被設定爲安靜模式,也就是不會輸出默認打印信息  。常常結合p使用

# echo -e 'hello\nworld' |sed 's/hello/H/'
H
world
# echo -e 'hello\nworld' |sed -n 's/hello/H/'   #加上-n 不打印
# echo -e 'hello\nworld' |sed -n 's/hello/H/p'     # -n加上p標記,只會把匹配並修改的內容打印出來
H

-e  用sed執行多條命令

# echo -e 'hello\nworld' |sed -e 's/hello/H/' -e 's/world/W/'
H
W
# echo -e 'hello\nworld' |sed 's/hello/H/;s/world/W/'    #等同於用分號鏈接多個子命令
H W

-i    修改源文件

# sed -i.bak '/while/d'   test.py     # 修改源文件 源文件備份爲.bak  

打印

p   打印

# nl /etc/passwd | sed -n '10,20p'     打印第10行-20行

....

# cat /etc/my.cnf |sed -n '/log-error/p'       打印包含log-error的行

# nl /etc/passwd | sed -n '/operator/,/abrt/p'       匹配operator到abrt之間的全部行

# nl /etc/passwd | sed -n '10!p'      不打印第10行

# nl /etc/passwd | sed -n '10,20!p'    不打印第10-20行

# nl /etc/passwd | sed -n '1~2p'    間隔行  打印1,3,5....行

 

操做命令

a 追加

# echo -e 'hello\nworld' |sed '/world/a ---'    #在world這行後面增長
hello
world
---

# echo -e 'hello\nworld' |sed '1,2a ---'   #1-5行後面 每一行都增長
hello
---
world
---  

# echo -e 'hello\nworld' |sed '$a ---'     #在文末增長  
hello
world
---

r  把一個文件的內容追加到指定行下面

# cat a.txt
aaaaa

# echo -e 'hello \nworld'  | sed '2r a.txt'
hello
world
aaaaa

i 插入

# echo -e 'hello\nworld' |sed '1i +++'    #第1行前面插入

# echo -e 'hello\nworld' |sed '1,2i +++'    #第1-2行每一行前面插入
+++
hello
+++
world

c 替代

# echo -e 'hello\nworld' |sed '/hello/c xxx'  #把匹配的行內容替換成xxx
xxx
world

# echo -e 'hello\nworld' |sed '1,$c xxx'   #把1-末行內容做爲一個總體替換成xxx
xxx

d 刪除

# cat /etc/passwd |sed '1,40d'    # 刪除1-40行

# cat /etc/my.cnf |sed '/^$/d'   #刪除空行

# cat /etc/passwd |sed '/rpc/,/salt/d'   #刪除匹配到rpc行和salt行之間的全部行

s 替換  

g全局

# echo -e 'falsefalse\ntrue' |sed 's/false/FALSE/g'
FALSEFALSE
true

# ifconfig | sed -n '/broadcast/p' |sed 's/inet//' |sed 's/netmask.*//'

&  表示正則表達式匹配的整個結果集

# echo 'hello world' |sed 's/hello/11&22/'   # &符號
11hello22 world

 -r  支持擴展正則表達式  

()分組

# echo 'hello 123 world' |sed -r 's/([a-z]+)( [0-9]+ )([a-z]+)/\1\3\2/'
helloworld 123

 

awk

一次處理一行內容,能夠對每行進行切片處理

格式: awk [option]  'pattern{awk 操做命令}'  file

pattern:正則表達式,邏輯判斷式
操做命令:內置函數:print()  printf()
控制指令:if(){...} else{...},while(){...}


awk 內置變量
$0: 整個當前行
$1: 每行第一個字段

分隔符
-F 若是不指定就會默認使用空格
# cat /etc/passwd |awk -F: '{print $3}' #打印第三個字段

# cat /etc/passwd |awk -F: '{print $1"\t"$7}' #打印第1,7字段,並以\t分割


NR:每行的記錄號  行號 Number of Record
NF: 字段數量變量  列號 Number of Fileds

# cat /etc/passwd |awk -F: '{print "line: "NR,"column: "NF,"user: "$1}'


條件判斷 if
打印用戶id大於100的行號和用戶名
# cat /etc/passwd |awk -F: '{if ($3>100) print $1}'

打印包含True的行
# cat server.py |awk '/True/{print $0}'
用sed
# cat server.py |sed -n '/True/p'


patter 邏輯判斷式
~,!~ 匹配正則表達式
== ,!= ,< , > 判斷邏輯表達式

# df -h  |  awk 'NR!=1{print  $1,$4}'

# cat /etc/passwd |awk -F: '$1=="root" {print $0}'     #"root"雙引號
# cat /etc/passwd |awk -F: '$3 > 100 && $3 < 1000 {print $1,$3}'   # 100<uid<1000
# cat /etc/passwd |awk -F ':' '$1~/^m.*/{print $1}'     #第一個字段以m開頭

# cat test.log     |  awk '{if($0~/^$/) print NR}'   #打印空行的行號


擴展格式 BEGIN END

awk [options] 'BEGIN{print "start"}  pattern{commands}  END{print "end"}' file

BEGIN{ 執行前的語句 }      {處理每一行時要執行的語句}       END { 處理完全部的行後要執行的語句 }

 

# cat /etc/passwd |awk -F: 'BEGIN{print "------start-----"}$3>1000{print $1":"$3}END{print "---end-------"}'
------start-----
git:1001
salt:1002
---end-------

# cat /etc/passwd |awk -F: 'BEGIN{print "Line\tCol\tUser"}{print NR"\t"NF"\t"$1}END{print "------"}'
Line Col User
1 7 root
2 7 bin
------

統計文件夾大小
# ls -l |awk 'BEGIN{s=0}{s+=$5}END{print "size is:"s/1024/1024"M"}'    
size is:382.935M

# ls -l |awk '{s+=$5}END{print "size is:"s/1024/1024"M"}'   #awk裏面變量初始值默認0,能夠不寫BEGIN初始化
size is:382.935M

統計passwd帳戶總人數
# cat /etc/passwd |awk -F: '$1!~/^$/{sum++}END{print "count: "sum}' #排除空行
count: 42

統計UID大於100的用戶名
# cat /etc/passwd |awk -F: '$3>100 {sum+=1}END{print sum}'
20

 

 # cat /etc/passwd |awk -F: '{if ($3>100) name[count++]=$1}END{for (i=0;i<count;i++) print i,name[i]}'
 # netstat -nap  |awk '$6~/CONNECT/{sum[$6]++}END{for (i in sum)print i,sum[i]}'
相關文章
相關標籤/搜索