1. 程式人生 > >linux 正則表示式 之 sed 與awk

linux 正則表示式 之 sed 與awk

sed 可以將資料進行替換,刪除,新增,選取等操作

sed [引數] [動作] 引數: -n :使用安靜模式 ,只有經過sed特殊處理的那一行才能被列出來 -i : 直接修改讀取檔案內容,而不是由螢幕輸出 -e :直接在命令列模式上進行sed動作編輯 -r :sed的動作支援是拓展正則表示式語法,預設是基礎正則表示式 動作說明 :[n1],[n2] function [n1],[n2] 代表的是 n1行到n2 行

function 說明: a :新增 a的後面可以接字串,而字串會在新的一行出現 c :替換 c的後面可以接字串。這些字串可以替換n1-n2之間的行 d : 刪除 i : 插入 i的後面可以接字串。而這些字串會在新的一行出現 p :打印出來,也就是將某個選擇的資料打印出來 s :替換 可以直接進行替換的工作 ,通常搭配正則表示式

1.刪除操作


[[email protected] tmp]# nl regular_express.txt | sed '2,5d'
     1  "Open Source" is a good mechanism to develop programs.
     6  GNU is free air not free beer.
     7  Her hair is very beauty.
     8  I can't finish the test.
     9  Oh! The soup taste good.
    10  motorcycle is cheap than car.
    11
This window is clear. 12 the symbol '*' is represented as start. 13 Oh! My god! 14 The gd software is a library for drafting programs. 15 You are the best is mean you are the no. 1. 16 The world <Happy> is the same with "glad". 17 I like dog. 18 google is
the best tools for search keyword. 19 goooooogle yes! 20 go! go! Let's go. 21 # I am VBird

2.在2行後新增 sxd???


[[email protected] tmp]# cat regular_express.txt  |  sed '2a sxd???'
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
sxd???
Football game is not use feet only.
this dress doesn't fit me.
However, this dress is about $ 3183 dollars.
GNU is free air not free beer.
Her hair is very beauty.
I can't finish the test.
Oh! The soup taste good.
motorcycle is cheap than car.
This window is clear.
the symbol '*' is represented as start.
Oh!     My god!
The gd software is a library for drafting programs.
You are the best is mean you are the no. 1.
The world <Happy> is the same with "glad".
I like dog.
google is the best tools for search keyword.
goooooogle yes!
go! go! Let's go.
# I am VBird

在第二行之前插入 將a改為i即可

3.將2-5行的內容替換成為 qqq


[[email protected] tmp]# nl regular_express.txt  |  sed '2,5c qqq'
     1  "Open Source" is a good mechanism to develop programs.
qqq
     6  GNU is free air not free beer.
     7  Her hair is very beauty.
     8  I can't finish the test.
     9  Oh! The soup taste good.
    10  motorcycle is cheap than car.
    11  This window is clear.
    12  the symbol '*' is represented as start.
    13  Oh!     My god!
    14  The gd software is a library for drafting programs.
    15  You are the best is mean you are the no. 1.
    16  The world <Happy> is the same with "glad".
    17  I like dog.
    18  google is the best tools for search keyword.
    19  goooooogle yes!
    20  go! go! Let's go.
    21  # I am VBird

4.列印第5-9行

[root@localhost tmp]# nl regular_express.txt  | sed -n '5,9p'
     5  However, this dress is about $ 3183 dollars.
     6  GNU is free air not free beer.
     7  Her hair is very beauty.
     8  I can't finish the test.
     9  Oh! The soup taste good.

部分資料的查詢並替換的功能 格式為 sed ‘s/要替換的字元/新的字串/g’

5.刪除註釋的資料(以#開頭的行)

[[email protected] tmp]# cat regular_express.txt | sed 's/#.*//g'
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn't fit me.

GNU is free air not free beer.
Her hair is very beauty.
I can't finish the test.
Oh! The soup taste good.
motorcycle is cheap than car.
This window is clear.
the symbol '*' is represented as start.
Oh!     My god!
The gd software is a library for drafting programs.
You are the best is mean you are the no. 1.
The world <Happy> is the same with "glad".
I like dog.
google is the best tools for search keyword.
goooooogle yes!
go! go! Let's go.

awk 適合處理小型的資料處理 awk 模式基本是 awk ‘條件1{動作1}條件2{動作2}’ 檔名 輸出格式特殊樣式 \n 換行 \t 水平的[Tab] 按鍵 \v 垂直的[tab]按鍵

BEGIN 第一步執行動作 END 最後結束執行動作 FS 指定分隔符

awk ‘{print 1}’   檔名                  列印檔案的第一列  awk  ’ FS=”:”{print1 "\t" 3}’        以: 為分隔符 列印1,2 列  awk  ’  {sum+=1 } END{print sum}‘  將第一列資料相加

注意:awk處理資料是一行一行往下走