Shell腳本編程---sed和gawk介紹(九)

一、文本處理

(1)sed編輯器

       sed編輯器稱爲流編輯器,與普通的交互式文本編輯器相對應。

sed命令的格式:

sed options script file

1》在命令行中定義編輯器命令

[root@ceph01 home]# echo "This is a test" | sed 's/test/big test/'
This is a big test

[root@ceph01 sed-gawk]# cat data 
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog

[root@ceph01 sed-gawk]# sed 's/dog/cat/' data
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat

2》在命令行中使用多個編輯器命令

[root@ceph01 sed-gawk]# sed -e 's/brown/green/; s/dog/cat/' data
The quick green fox jumps over the lazy cat
The quick green fox jumps over the lazy cat
The quick green fox jumps over the lazy cat
The quick green fox jumps over the lazy cat

[root@ceph01 sed-gawk]# sed -e '
> s/brown/green/
> s/fox/elephant/
> s/dog/cat/' data
The quick green elephant jumps over the lazy cat
The quick green elephant jumps over the lazy cat
The quick green elephant jumps over the lazy cat
The quick green elephant jumps over the lazy cat

3》從文件讀取編輯器命令

[root@ceph01 sed-gawk]# cat script1 
s/brown/green/
s/fox/elephant/
s/dog/cat/
[root@ceph01 sed-gawk]# sed -f script1 data
The quick green elephant jumps over the lazy cat
The quick green elephant jumps over the lazy cat
The quick green elephant jumps over the lazy cat
The quick green elephant jumps over the lazy cat

(2)gawk程序

gawk程序是Unix中原awk程序的GNU版本。在編輯語言內部,可以:

         定義要保存數據的變量

         使用算術和字符串操作符對數據進行計算

          使用結構化編程概念,例如if-then語句和循環,將邏輯添加到數據處理過程

          通過從數據文件內抽取數據元素以及按照其他順序或格式對它們重定位,生成帶格式的報告。

1》gawk命令格式

gawk options program file

2》自命令行讀取程序腳本

[root@ceph01 sed-gawk]# gawk '{print "Hello John!"}'
This is test             
Hello John!
hello 
Hello John!

3》使用數據字段變量

gawk的主要功能之一是處理文本文件中數據的能力。

$0 表示整行文本;

$1表示文本行中的第一個數據字段;

$2表示文本行中的第一個數據字段;

$n表示文本行中的第n個數據字段;

[root@ceph01 sed-gawk]# cat data 
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
[root@ceph01 sed-gawk]# gawk '{print $1}' data
The
The
The
The
The
The
The

4》在程序腳本中使用多個命令

[root@ceph01 sed-gawk]# echo "My name is Rich" | gawk '{$4="Dave"; print $0}'
My name is Dave
[root@ceph01 sed-gawk]# gawk '{
> $4="testing"
> print $0 }'

   testing

   testing

5》從文件讀取程序

[root@ceph01 sed-gawk]# cat script2 
{ print $5 "'s userid is " $1 }

[root@ceph01 sed-gawk]# gawk -F: -f script2 data
's userid is The quick brown fox jumps over the lazy dog
's userid is The quick brown fox jumps over the lazy dog
's userid is The quick brown fox jumps over the lazy dog
's userid is The quick brown fox jumps over the lazy dog
's userid is The quick brown fox jumps over the lazy dog
's userid is The quick brown fox jumps over the lazy dog
's userid is The quick brown fox jumps over the lazy dog

6》在處理數據之前運行腳本

[root@ceph01 sed-gawk]# gawk 'BEGIN {print "Hello World!"} {print $0}'
Hello World!
This is test       
This is test
this is another test
this is another test

7》在處理數據之後運行腳本

[root@ceph01 sed-gawk]# gawk 'BEGIN {print "Hello World!"} {print $0} END {print "byebye"}'
Hello World!
this is a test
this is a test
byebye

二、sed編輯器基礎知識

(1)更多替換選項

替換標記:

s/pattern/replacement/flags

可用的替換標記有4種:

       數字 :表示新文本替換的模式

       g      :表示用新文本替換現有文本的全部實例

       p      :表示打印原始行的內容

       w file:將替換的結果寫入文件

[root@ceph01 sed-gawk]# sed 's/fox/cat/g' data
The quick brown cat jumps over the lazy dog
The quick brown cat jumps over the lazy dog
The quick brown cat jumps over the lazy dog

替換字符

$:sed 's!/bin/bash!/bin/csh!' /etc/passwd

(2)使用地址

在sed編輯器中,有兩種尋址形式:

        行的數值範圍

        篩選行的文本模式

格式:

[address] command

--------------------------------------------------------------------------
address {
    command1
    command2
    command3
}

1》數字式行尋址

[root@ceph01 sed-gawk]# sed '2s/dog/cat/' data
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy dog

2》使用文本模式篩選器

命令格式:

/pattern/command

3》組合命令

[root@ceph01 sed-gawk]# sed '2{
> s/fox/elephant/
> s/dog/cat/
> }' data
The quick brown fox jumps over the lazy dog
The quick brown elephant jumps over the lazy cat
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog

(3)刪除行

刪除指定的行

$:sed '3d' data

刪除特定的文本行範圍

$:sed '2,3d' data

(4)插入和附加文本

     插入命令(i)在指定行之前添加新的一行;

     附加命令(a)在指定行之後添加新的一行

命令格式:

sed '[address]command new line'
[root@ceph01 sed-gawk]# echo "testing" | sed 'i\
> this is a test'
this is a test
testing
[root@ceph01 sed-gawk]# echo "testing" | sed 'a\
this is a test'
testing
this is a test

(5)更改行

[root@ceph01 sed-gawk]# sed '3c\
> this is a changed line of text.' data
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
this is a changed line of text.
The quick brown fox jumps over the lazy dog

(6)變換命令

變換命令(y)是唯一對單個字符進行操作的sed編輯器命令。命令格式:

[address]y/inchars/outchars/
[root@ceph01 sed-gawk]# sed 'y/123/789/' data
This is line number 7
This is line number 8
This is line number 9
This is line number 4
This is line number 5

(7)打印命令溫習

         打印文本行的小寫p命令

         打印行號的等號(=)命令

         列出行的1(小寫L)命令

1》打印行

[root@ceph01 sed-gawk]# echo "this is test" | sed 'p'
this is test
this is test

2》打印行號

[root@ceph01 sed-gawk]# sed '=' data 
1
The quick brown fox jumps over the lazy dog
2
The quick brown fox jumps over the lazy dog
3
The quick brown fox jumps over the lazy dog
4
The quick brown fox jumps over the lazy dog

3》列出行

[root@ceph01 sed-gawk]# sed -n 'l' data
The quick brown fox jumps over the lazy dog$
The quick brown fox jumps over the lazy dog$
The quick brown fox jumps over the lazy dog$

(8)將文件用於sed

1》寫文件

w命令用於將文本行寫入文件:

[address]w filename
[root@ceph01 sed-gawk]# sed '1,2w test' data
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog

[root@ceph01 sed-gawk]# cat test 
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog

2》從文件讀取數據

[address]r filename
[root@ceph01 sed-gawk]# cat test 
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
[root@ceph01 sed-gawk]# sed '2r test' data
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章