天天看點

Linux Shell程式設計第四章sed指令和awk程式設計

Sed 簡介

sed 是一種新型的,非互動式的編輯器。它能執行與編輯器 vi 和 ex 相同的編輯任務。sed 編輯器沒有提供互動式使用方式,使用者隻能在指令行輸入編輯指令、指定檔案名,然後在螢幕上檢視輸出。 sed 編輯器沒有破壞性,它不會修改檔案,除非使用 shell 重定向來儲存輸出結果。預設情況下,所有的輸出行都被列印到螢幕上。

sed 工作過程

sed 編輯器逐行處理檔案(或輸入),并将輸出結果發送到螢幕。 sed 的指令就是在 vi和 ed/ex 編輯器中見到的那些。 sed 把目前正在處理的行儲存在一個臨時緩存區中,這個緩存區稱為模式空間或臨時緩沖。sed 處理完模式空間中的行後(即在該行上執行 sed 指令後),就把改行發送到螢幕上(除非之前有指令删除這一行或取消列印操作)。 sed 每處理完輸入檔案的最後一行後, sed 便結束運作。 sed 把每一行都存在臨時緩存區中,對這個副本進行編輯,是以不會修改或破壞源檔案。如圖 1: sed 處理過程。 

Linux Shell程式設計第四章sed指令和awk程式設計

從上圖可以看出 sed 不是破壞性的,它不會修改正在編輯的檔案。 

sed修改的檔案重定向輸出

sed     [選項]     'sed指令'     input-file > output-file

Sed 指令格式(三種)

1、在Shell 指令行輸入指令調用sed

sed     [選項]     'sed指令'     輸入檔案

Note:sed指令要用單引号引起來。

2、将sed指令插入到腳本檔案中;然後通過sed指令調用它。

sed     [選項]    -f    sed腳本檔案    輸出檔案

3、将sed指令插入到腳本檔案中;最常用的方法就是設定該腳本檔案為可執行;然後直接執行該腳本檔案。

./sed腳本檔案   輸入檔案

第二種的sed的腳本檔案和第三種不同; 

sed指令的常用選項以及意義

選項 說明
-n 加入-n 後隻列印被 sed 特殊處理的行;不列印所有的行到标準的輸出。
-e 如果隻是傳遞一個編輯指令給sed,-e選項可以省略。多重編輯的時候,需要使用 -e選項。
-f 表示一個sed腳本檔案正在執行。
-r Sed 使用擴充正則。
-i 直接修改文檔讀取的内容,不在螢幕上輸出。

 sed指令通常是由sed定位文本和sed編輯指令兩部分組成。sed編輯指令會對定位的文本行進行各種處理。 

Sed 定位文本(兩種方式)

  • 使用行号,指定一行;或者指定行号的範圍。
  • 使用正規表達式。
選項 說明
x x 為指定的行号
x,y 指定從x 到 y的行号範圍
/regexp/ 查詢包含比對正則的行;通過/ /之間的正則來比對
/regexp/regexp/ 查詢包含兩個模式的行
x,/regexp/ 從x 行号到與regexp比對的行之間的行
x,y! 查詢不包括 x和y行号的行
$ 這個$符表示比對最後一行

Sed 的正規表達式

元字元 功 能 示 例 示例的比對對象
^ 行首定位符 /^love/ 比對所有以 love 開頭的行
$ 行尾定位符 /love$/ 比對所有以 love 結尾的行
.

比對除換行外的單

個字元

/l..e/

比對包含字元 l、後跟兩個任意

字元、再跟字母 e 的行

*

比對零個或多個前

導字元

符号&代表查找串。字元串 love

将替換前後各加了兩個**的引

用,即 love 變成**love**

\< 詞首定位符 /\<love/

比對包含以 love 開頭的單詞的

\> 詞尾定位符 /love\>/

比對包含以 love 結尾的單詞的

x\{m\} 連續 m 個 x /o\{5\}/

分别比對出現連續 5 個字母 o、

至少 5 個連續的 o、或 5~10 個

連續的 o 的行

x\{m,\} 至少 m 個 x /o\{5,\}/
x\{m,n\}

至少 m 個 x,但不

超過 n 個 x

/o\{5,10\}/

Sed 編輯指令

sed 編輯指令告訴 sed 如何處理由位址指定的各輸入行。如果沒有指定位址, sed 就會處理輸入的所有的行。

命 令 說 明
p 列印比對行資訊
= 列印檔案的行号
a\ 在定位行号之後追加文本資訊
i\ 在定位行号之前插入文本資訊
d 删除定位行
c\ 用新文本修改(替換)定位文本
s 用一個字元串替換另一個
r 從另一個檔案中讀取文本
w 将文本寫入到一個檔案
y 變換字元;将字元轉換為另一字元(不能對正規表達式使用 y 指令)
q 第一個模式比對完成後 結束或退出 sed
l 顯示與八進制ASCII碼等價的控制字元
{} 在定位行執行的指令組
n 讀取下一個輸入行,用下一個指令處理新的行
h 把模式緩沖區的内容複制到暫存緩存區
H 把模式緩沖區的内容追加到暫存緩存區
x 交換模式緩沖區和暫存緩存區内的容
g 取出暫存緩沖區裡的内容,将其複制到模式緩沖區,覆寫該處原有内容
G 取出暫存緩沖區裡的内容,将其複制到模式緩沖區,追加在原有内容後面
對所選行以外的所有行應用指令

4.2 sed使用執行個體

下面給出測試檔案作為輸入檔案:ceshi.txt 

1

2

3

4

5

6

7

8

9

10

[[email protected] ysj]$ cat ceshi.txt 

northwest       NW      Charles Main    3.0     .98     3       34

western         WE      Sharon Gray     5.3     .97     5       23

southwest       SW      Lewis Dalsass   2.7     .8      2       18

southern        SO      Suan Chin       5.1     .95     4       15

southeast       SE      Patricia Hemenway       4.0     .7      4       17

eastern         EA      TB Savage       4.4     .84     5       20

northeast       NE      AM Main Jr.     5.1     .94     3       13

north           NO      Margot Weber    4.5     .89     5       9

central         CT      Ann Stephens    5.7     .94     5       13

4.2.1 sed指令選項的執行個體

這裡需要結合幾個編輯指令才能展現其作用;但是重點還是了解指令選項的情況。

1、sed指令的 -n選項

  • 不使用 -n 不但列印出被sed指令特殊處理的行資訊;還會列印文本的全部内容
  • 加入 -n 隻會列印被sed特殊處理的行資訊。
//不帶 -n 不僅列印出被sed處理後的行資訊,還會列印所有的行資訊
[[email protected] ysj]$ sed  '1p' ceshi.txt 
northwest       NW      Charles Main    3.0     .98     3       34
northwest       NW      Charles Main    3.0     .98     3       34
western         WE      Sharon Gray     5.3     .97     5       23
southwest       SW      Lewis Dalsass   2.7     .8      2       18
southern        SO      Suan Chin       5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
eastern         EA      TB Savage       4.4     .84     5       20
northeast       NE      AM Main Jr.     5.1     .94     3       13
north           NO      Margot Weber    4.5     .89     5       9
central         CT      Ann Stephens    5.7     .94     5       13

//帶 -n 隻列印第一行資訊
[[email protected] ysj]$ sed -n '1p' ceshi.txt 
northwest       NW      Charles Main    3.0     .98     3       34
           

sed指令列印範圍行 

//列印測試檔案的 2,3,4行資訊
[[email protected] ysj]$ sed -n '2,4p' ceshi.txt 
western         WE      Sharon Gray     5.3     .97     5       23
southwest       SW      Lewis Dalsass   2.7     .8      2       18
southern        SO      Suan Chin       5.1     .95     4       15
           

sed指令列印比對模式行(正規表達式)

// 列印行中包含north字元的行資訊
[[email protected] ysj]$ sed  -n '/north/p' ceshi.txt
northwest       NW      Charles Main    3.0     .98     3       34
northeast       NE      AM Main Jr.     5.1     .94     3       13
north           NO      Margot Weber    4.5     .89     5       9
           

2、sed指令的 -e 選項

如果隻傳遞一個編輯指令給sed, -e選項是可以省略的,隻有當向sed指令傳遞多個編輯指令的時候 -e 才能有用武之地。

// 簡單的列印行号指令 
[[email protected] ysj]$ sed -n '/north/=' ceshi.txt 
1
7
8
// 列印行資訊和行号 傳遞兩個編輯指令給sed 使用 -e 選項
[[email protected] ysj]$ sed -n -e  '/north/=' -e '/north/p' ceshi.txt 
1
northwest       NW      Charles Main    3.0     .98     3       34
7
northeast       NE      AM Main Jr.     5.1     .94     3       13
8
north           NO      Margot Weber    4.5     .89     5       9
           

Note:sed不支援同時帶多個編輯指令的用法。 sed -n   '/north/p='  ceshi.txt 

帶多個編輯指令sed的一般格式  sed  [選項]  -e 編輯指令1  -e 編輯指令2   ... -e 編輯指令n   輸入檔案

 3、sed指令的 -f 選項

-f 選項隻有調用sed腳本檔案的時候才起作用。

追加,插入,修改,删除和替換文本等功能往往需要多條sed指令來完成,所有需要将這些指令寫入sed腳本,然後調用sed腳本完成。

// sed '指定位置 a\追加内容'  ceshi.txt
// 使用 a\ 編輯指令 在與17字元相比對行的後面追加文本 This is our append content!
// 如果沒有指定位置 會在每行進行追加。
[[email protected] ysj]$ sed '/17/a\This is our append content!' ceshi.txt 
northwest       NW      Charles Main    3.0     .98     3       34
western         WE      Sharon Gray     5.3     .97     5       23
southwest       SW      Lewis Dalsass   2.7     .8      2       18
southern        SO      Suan Chin       5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
This is our append content!
eastern         EA      TB Savage       4.4     .84     5       20
northeast       NE      AM Main Jr.     5.1     .94     3       13
north           NO      Margot Weber    4.5     .89     5       9
central         CT      Ann Stephens    5.7     .94     5       13
           

将以上的實作的功能添加到sed腳本中去執行。 append.sed 

// sed 腳本的第一行和bash shell腳本一樣;都是以 #!符号開頭 後面是解釋器的路徑
// 一般在/bin 目錄下  如果不知道在哪個目錄下 使用 which sed指令查找。
// -f 在腳本檔案中不能少,否則報錯。如果追加的内容有多行,需要使用 \ 進行換行。
// 授予腳本檔案為可執行權限 (chmod u+x ceshi.txt);然後執行腳本(執行方法和bash腳本一樣) 
[[email protected] ysj]$ vi append.sed 
#! /bin/sed -f
/17/a\
This is our append content! \
This is our append content!
           
[[email protected] ysj]$ chmod u+x append.sed 
[[email protected] ysj]$ ./append.sed ceshi.txt 
northwest       NW      Charles Main    3.0     .98     3       34
western         WE      Sharon Gray     5.3     .97     5       23
southwest       SW      Lewis Dalsass   2.7     .8      2       18
southern        SO      Suan Chin       5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
This is our append content! 
This is our append content!
eastern         EA      TB Savage       4.4     .84     5       20
northeast       NE      AM Main Jr.     5.1     .94     3       13
north           NO      Margot Weber    4.5     .89     5       9
central         CT      Ann Stephens    5.7     .94     5       13
           

4.2.2  sed 文本定位的執行個體

1、比對元字元

需要使用轉義字元 "\" 屏蔽其特殊的含義。

// 比對 . 符号
[[email protected] ysj]$ sed  -n '/\./p' ceshi.txt 
northwest       NW      Charles Main    3.0     .98     3       34
western         WE      Sharon Gray     5.3     .97     5       23
southwest       SW      Lewis Dalsass   2.7     .8      2       18
southern        SO      Suan Chin       5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
eastern         EA      TB Savage       4.4     .84     5       20
northeast       NE      AM Main Jr.     5.1     .94     3       13
north           NO      Margot Weber    4.5     .89     5       9
central         CT      Ann Stephens    5.7     .94     5       13

// $在正規表達式中表示行尾,但是在sed指令中表示最後一行 
// sed的編輯指令可以放在單引号内,也可以放在單引号外面 如 :p
[[email protected] ysj]$ sed  -n '$p' ceshi.txt 
central         CT      Ann Stephens    5.7     .94     5       13
[[email protected] ysj]$ sed  -n '$'p ceshi.txt 
central         CT      Ann Stephens    5.7     .94     5       13

// 使用元字元進行任意字元的比對
[[email protected] ysj]$ sed  -n '/.*east/'p ceshi.txt 
southeast       SE      Patricia Hemenway       4.0     .7      4       17
eastern         EA      TB Savage       4.4     .84     5       20
northeast       NE      AM Main Jr.     5.1     .94     3       13

           

2、!符号

!符号表示取反。x,y! 表示比對不在x和y行号範圍内的行。

[[email protected] ysj]$ sed -n '3,5!p' ceshi.txt 
northwest       NW      Charles Main    3.0     .98     3       34
western         WE      Sharon Gray     5.3     .97     5       23
eastern         EA      TB Savage       4.4     .84     5       20
northeast       NE      AM Main Jr.     5.1     .94     3       13
north           NO      Margot Weber    4.5     .89     5       9
central         CT      Ann Stephens    5.7     .94     5       13
           

3、使用行号和關鍵字比對限定行的範圍

使用 x,/parttern/ 或者 /parttern/,y 兩種形式,去限制行号和關鍵字比對行之間的範圍。實際上這兩種形式與 x,y是一樣的,隻是x或y用 /parttern/代替而已。

// 列印與Margot Weber比對的行到最後一行
[[email protected] ysj]$ sed -n '/Margot Weber/,$p' ceshi.txt 
north           NO      Margot Weber    4.5     .89     5       9
central         CT      Ann Stephens    5.7     .94     5       13
// 列印出第7行到與Margot Weber比對的行
[[email protected] ysj]$ sed -n '7,/Margot Weber/p' ceshi.txt 
northeast       NE      AM Main Jr.     5.1     .94     3       13
north           NO      Margot Weber    4.5     .89     5       9
           

4.2.3  sed 編輯指令的執行個體

sed編輯指令是sed程式設計中最常見的指令,隻有深刻的了解了這些指令的用法,才能自如的利用sed進行Shell程式設計。

上面我們介紹了文本追加指令( \a),現在我們從插入文本指令( \i )開始。

1、插入文本 ( i )

插入文本和追加文本類似,隻是插入的順序不同,追加文本是在比對行的後面插入,而插入文本是在比對行的前面插入。

格式:sed  '指定位址 i\ text' 輸入檔案

建立 insert.sed 腳本

#! /bin/sed -f
/Margot Weber/i\   #表示此處換行插入文本
ysj                #插入文本的内容
           

授權執行: chmod u+x insert.sed    ./insert.sed ceshi.txt  

可以看到在Margot Weber行前面插入了新的文本。

[[email protected] ysj]$ ./insert.sed ceshi.txt 
northwest       NW      Charles Main    3.0     .98     3       34
western         WE      Sharon Gray     5.3     .97     5       23
southwest       SW      Lewis Dalsass   2.7     .8      2       18
southern        SO      Suan Chin       5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
eastern         EA      TB Savage       4.4     .84     5       20
northeast       NE      AM Main Jr.     5.1     .94     3       13
ysj
north           NO      Margot Weber    4.5     .89     5       9
central         CT      Ann Stephens    5.7     .94     5       13
           

2、修改文本 ( c )

将比對的文本行用新的文本替代。

指令格式: sed   '指定位址 c\text'   輸入檔案

建立 modify.sed 腳本

[[email protected] ysj]$ vi modify.sed
#! /bin/sed -f
/Margot Weber/c\                   # c\ 表示此處是換行修改文本
This is my modify line!            # 修改的文本内容
           

授權執行: chmod u+x modify.sed    ./modify.sed ceshi.txt  

[[email protected] ysj]$ ./modify.sed  ceshi.txt 
northwest       NW      Charles Main    3.0     .98     3       34
western         WE      Sharon Gray     5.3     .97     5       23
southwest       SW      Lewis Dalsass   2.7     .8      2       18
southern        SO      Suan Chin       5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
eastern         EA      TB Savage       4.4     .84     5       20
northeast       NE      AM Main Jr.     5.1     .94     3       13
This is my modify line!
central         CT      Ann Stephens    5.7     .94     5       13
           

3、删除文本 ( d ) 

sed删除指令可以将指定的行或者将指定的行的範圍進行删除 并列印剩餘的文本資訊;不使用 -n 選項。

指令格式: sed   '指定位址 d'    輸入檔案

删除文本檔案第一行的資料:    sed '1d' ceshi.txt 

删除文本檔案最後一行的資料: sed '$d' ceshi.txt 

删除文本檔案第1行到第10行的資料: sed '1,10d' ceshi.txt 

删除文本檔案第5行到最後一行的資料: sed '5,$d' ceshi.txt 

删除與關鍵字比對的行:Margot 

sed  '/[Mm][Aa][Rr][Gg][Oo][Tt]/d'  ceshi.txt 

[[email protected] ysj]$ sed '/[Mm][Aa][Rr][Gg][Oo][Tt]/d' ceshi.txt 
northwest       NW      Charles Main    3.0     .98     3       34
western         WE      Sharon Gray     5.3     .97     5       23
southwest       SW      Lewis Dalsass   2.7     .8      2       18
southern        SO      Suan Chin       5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
eastern         EA      TB Savage       4.4     .84     5       20
northeast       NE      AM Main Jr.     5.1     .94     3       13
central         CT      Ann Stephens    5.7     .94     5       13
           

4、替換文本 ( s ) 

替換文本和修改文本相似,但是替換文本可以替換一個字元串,而修改文本是對整行進行新修改。

另外替換文本的替換選項使得文本替換更加靈活。

指令格式: sed  's/被替換的字元串/新字元串/[替換選項]'   輸入檔案

替換選項:

選項 說明
g  g 表示在行内全局替換;也就是說如果每一行裡出現多個west,所有的 west 都會被替換為 north。如果沒有 g 指令,則隻将每一行的第一 west 替換為 north。
p 和 -n 結合使用 隻列印替換行
w 表示輸出定向到一個檔案

Note:

1)預設情況下 sed s指令将替換後的全部文本都輸出

2)如果要求隻輸出替換的行,需要結合使用 -n 和p 選項。

3)如果缺少p選項,将不會列印任何内容,使用 -n 無效。

// 預設情況下列印全部的輸入内容
[[email protected] ysj]$ sed  's/Charles/CHARLES/' ceshi.txt
northwest       NW      CHARLES Main    3.0     .98     3       34
western         WE      Sharon Gray     5.3     .97     5       23
southwest       SW      Lewis Dalsass   2.7     .8      2       18
southern        SO      Suan Chin       5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
eastern         EA      TB Savage       4.4     .84     5       20
northeast       NE      AM Main Jr.     5.1     .94     3       13
north           NO      Margot Weber    4.5     .89     5       9
central         CT      Ann Stephens    5.7     .94     5       13
// 用了 -n參數 但是沒有加 p選項 結果是不列印任何内容
[[email protected] ysj]$ sed  -n  's/Charles/CHARLES/' ceshi.txt
// -n p 結合使用隻列印替換行
[[email protected] ysj]$ sed  -n  's/Charles/CHARLES/p' ceshi.txt
northwest       NW      CHARLES Main    3.0     .98     3       34
           

w 選項(其後面加檔案名表示将輸出定向到這個檔案,如果輸出檔案沒有建立,sed指令自動建立輸出檔案。)

 sed -n 's/oldworld/newword/w 輸出檔案名'  輸入檔案名

[[email protected] ysj]$ sed -n 's/Charles/CHARLES/w output.txt' ceshi.txt 
[[email protected] ysj]$ cat output.txt
northwest       NW      CHARLES Main    3.0     .98     3       34
           

Note:sed替換文本的時候經常使用到 &符号 ;&符号可用來儲存被替換的字元串以供調用。

//使用sed -n 's/northwest/(&)/pg' ceshi.txt
//等同于 sed -n 's/northwest/(northwest)/pg' ceshi.txt
[[email protected] ysj]$ sed -n 's/northwest/(&)/pg' ceshi.txt 
(northwest)       NW      Charles Main    3.0     .98     3       34
           

5、寫入一個新文本 ( w ) 

格式:   sed    '指定位置 w 輸出檔案'    輸入檔案

sed  -n '1,5 w output.txt' ceshi.txt    :将前5行的資料輸出到 output.txt 檔案中

sed -n '/Charles/w output.txt' ceshi.txt    :将比對Charles行的資料輸出到 output.txt 檔案中

[[email protected] ysj]$ cat output.txt
northwest       NW      Charles Main    3.0     .98     3       34
western         WE      Sharon Gray     5.3     .97     5       23
southwest       SW      Lewis Dalsass   2.7     .8      2       18
southern        SO      Suan Chin       5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
[[email protected] ysj]$ sed -n '/Charles/w output.txt' ceshi.txt 
[[email protected] ysj]$ cat output.txt
northwest       NW      Charles Main    3.0     .98     3       34
           

6、從檔案中讀入文本 (r)

格式: sed -n  '指定位置  r  輸入檔案 '  文本檔案    

建立一個輸入檔案: otherfile.txt 

This is the first line of the  otherfile.

This is the second line of the  otherfile.

sed -n '/Charles/r otherfile.txt' ceshi.txt    :在比對到Charles行的後面追加otherfile.txt 文本檔案中的内容。

[[email protected] ysj]$ sed '/Charles/r otherfile.txt' ceshi.txt 
northwest       NW      Charles Main    3.0     .98     3       34
This is the first line of the  otherfile.

This is the second line of the  otherfile.
western         WE      Sharon Gray     5.3     .97     5       23
southwest       SW      Lewis Dalsass   2.7     .8      2       18
southern        SO      Suan Chin       5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
eastern         EA      TB Savage       4.4     .84     5       20
northeast       NE      AM Main Jr.     5.1     .94     3       13
north           NO      Margot Weber    4.5     .89     5       9
central         CT      Ann Stephens    5.7     .94     5       13
           

7、退出指令 (q)

sed '指定位置 q'  文本檔案  

隻要找到指定位置的内容就列印之前的資料資訊,并退出。

8、變換指令 (y)

格式: sed 'y/被變換的字元串序列/變換的字元序列/'  輸入檔案

Note:被變換的字元串序列和變換的字元序列等長度。否則會報錯。

sed y 将被變換的字元序列逐個用變換的字元序列中的字元替換。

9、顯示控制字元 (l)

sed  -n '1,$l'  輸入檔案   :顯示輸入檔案中第一行到最後一行 控制字元(空格、F1鍵、Shift鍵)。

10、在定位行執行指令組 ( { })

它的作用和 sed -e 類似 都是為了在定位行執行多個編輯指令:

sed -n -e '/Charles/p' -e '/Charles/=' ceshi.txt  :列印比對内容的行号和具體資訊

等價于:    sed -n  '/Charles/{p;=}' ceshi.txt

[[email protected] ysj]$ sed -n  '/Charles/{p;=}' ceshi.txt
northwest       NW      Charles Main    3.0     .98     3       34
1
           

4.2.4  sed 進階編輯指令例子

1、處理比對行的下一行

sed編輯指令 n的 意義就是讀取下一個輸入行,用n後面的指令處理該行。此時有多個編碼指令,所有編輯指令 n 需要和 {}結合使用。

sed '/Charles/ {n; s/5.3/9.9/}'  ceshi.txt   :找出關鍵字Charles的比對行,然後在比對行的下一行執行s/t/T/p 指令

将t字元用T替換

[[email protected] ysj]$ sed -n  '/Charles/{n;s/t/T/p}' ceshi.txt
wesTern         WE      Sharon Gray     5.3     .97     5       23
           

2、利用分号分割多個編輯指令 

等同于 -e 和 {}

4.3 awk程式設計

awk是一種程式設計語言,gawk是目前最新的版本,目前的Linux版本用的都是gawk,這裡我們對awk和gawk不在區分,統稱awk。

awk可以實作資料的查找,抽取檔案中的資料,建立管道流指令等功能。

4.3.1 awk調用方法

調用awk方法和sed類似。也有三種方式。一種是shell指令行,另外兩種是将awk程式寫入腳本檔案,然後執行此腳本。

  • 在shell指令行輸入指令調用awk格式:

    awk [-F 域分隔符]  'awk程式段' 輸入檔案

  • 将awk程式段插入腳本檔案,然後通過awk指令調用它。

    awk -f awk腳本檔案 輸入檔案

  • 将sed指令插入腳本檔案後,設定腳本檔案可執行,然後直接執行此腳本檔案。

    ./awk腳本檔案  輸入檔案

4.3.2 awk程式設計例子

1、awk模式比對

任何awk語句都由模式和動作組成。模式決定着動作何時觸發,動作執行對輸入行的處理。

[[email protected] ysj]$ awk '/^$/{print "This is a blank line."}' ceshi.txt 
This is a blank line.
           

使用shell指令行對輸入檔案ceshi.txt的空白行列印 This is a blank line. 

将上述指令寫到腳本檔案中。

[[email protected] ysj]$ vi scr.awk
/^$/{print "This is a blank line."}
           
[[email protected] ysj]$ awk -f scr.awk ceshi.txt 
This is a blank line.
           

直接執行awk腳本,在腳本檔案中開始處添加 #!

[[email protected] ysj]$ cat scr.awk 
#! /bin/awk -f
/^$/{print "This is a blank line."}
[[email protected] ysj]$ chmod u+x scr.awk 
[had[email protected] ysj]$ ./scr.awk ceshi.txt 
This is a blank line.
           

模式比對不隻是像這麼簡單,在進一步講解awk模式比對前,先了解awk的記錄和域。 

2、記錄和域

awk将每個輸入檔案行定義為記錄,行中的每個字元定義為域,分割域的符号叫分隔符(空格,Tab或其他字元)。

兩個或者多個連續的分隔符都當做一個分隔符處理。對文本檔案分域處理是Linux系統中很多指令都使用的方法。

Linux Shell程式設計第四章sed指令和awk程式設計

生産環境案例

在實際生産中,在修改配置檔案的時候,有一些空格、空行、帶“ #”開頭的注釋都要删除或替換,下面為大家介紹幾個實用的例子

案例 23:

1

2

3

4

5

[[email protected] scripts]

# cat sed.txt

today is 

nice

day

you can walk out on the street

it will be 

import

to you

##每行的前面都有空格

1

2

3

4

5

6

7

8

9

10

[[email protected] scripts]

# sed 's/^[ ]*//' sed.txt

today is 

nice

day

you can walk out on the street

it will be 

import

to you

## 注:[ ]裡面有個空格

或者:

[[email protected] scripts]

# sed 's/^[[:space:]]*//' sed.txt

today is 

nice

day

you can walk out on the street

it will be 

import

to you

案例24:删除文本中空行和空格組成的行及#号注釋的行 

1

2

3

4

5

6

7

8

[[email protected] scripts]

# grep -Eiv "^#|^$" ssh_config

Host *

GSSAPIAuthentication 

yes

ForwardX11Trusted 

yes

SendEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES

SendEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT

SendEnv LC_IDENTIFICATION LC_ALL LANGUAGE

SendEnv XMODIFIERS

案例 25:從 Google 上下載下傳下來的配置檔案往往都帶有數字,現在需要删除所有行的首數字。

1

2

3

4

5

6

7

8

[[email protected] scripts]

# cat sed.txt

1today is 

nice

day

2you can walk out on the street

3it will be 

import

to you

[[email protected] scripts]

# sed 's/^[0-9][0-9]*//g' sed.txt

today is 

nice

day

you can walk out on the street

it will be 

import

to you

總結

本章内容總結了 sed 指令的用法,前面部分是 sed 指令的文法,後面部分則主要以實際案例來說明 sed 的用法,最後面一點介紹了 sed 指令在生産實踐中的運用。所謂學為練,練為戰,希望大家能夠将 sed 指令勤加練習,必将會在工作中有所用途,尤其是頻繁的分析日志檔案, Awk+Sed 是比較好的組合。最後希望本文對大家有所幫助,真正達到熟練的程度這就靠大家在工作中歸納總結了。  

繼續閱讀