sed編輯工具
sed是一個做交互式 面向字符流的編輯器,它和許多unix程序同樣,被認爲是面向字符流的,這是由於輸入流經過
程序並將輸出直接輸送到標準輸出端,輸入能夠來自文件,也能夠來自鍵盤,輸出默認在屏幕上,也能夠輸出到
文件中,sed 經過解釋腳原本工做,這一腳本指定了將要執行的工做
sed能夠做爲編輯過濾器使用,換句話說,你能夠處理輸入文件並將輸出結果發送到另一個程序
sed 很容易被堪稱爲交互式編輯相反的程序,sed程序與如何手動的應用編輯是很接近的,sed限制了
你利用在文本編輯器中使用的方法,awk爲處理文本提供了更通常的模型。
awk程序的典型實例是將數據格式化吧的報表,這些數據
多是unix產生的日誌文件,並且報表以一種對系統管理員有用 的格式化數據彙總,全部這些的關健是他具備
某一種數據結構。
由於,當數據擁有某一種數據結構時,最能體現awk的好處,文本文件的結構或鬆或緊,咱們將看到一個腳本,用於提取章節標題並將它們編號,
由製表符分割的列項目所組成的表結構是高度結構化的,能夠利用awk腳本對數據從新排序,甚至能夠把它們的行變列,裂變行
awk能夠完成的功能
1.將文本文件看作由記錄和字段組成的文本數據庫
2.使用變量操做數據庫
3.使用算數和字符串操做符
4.使用普通的程序結構,列如循環和條件
5.生成格式化報告
6.定義函數
7.從腳本中執行unix命令
8.更加巧妙的處理命令行參數
9.更容易的處理多個輸入流
sed 和awk都基於行編輯器
基本操做
$ed 文件名
若機器不理解就顯示問號
$ ed <- 激活 ed 命令
a <- 告訴 ed 我要編輯新文件
My name is Titan. <- 輸入第一行內容
And I love Perl very much. <- 輸入第二行內容
. <- 返回 ed 的命令行狀態
i <- 告訴 ed 我要在最後一行以前插入內容
I am 24. <- 將「I am 24.」插入「My name is Titan.」和「And I love Perl very much.」之間
. <- 返回 ed 的命令行狀態
c <- 告訴 ed 我要替換最後一行輸入內容
I am 24 years old. <- 將「I am 24.」替換成「I am 24 years old.」(注意:這裏替換的是最後輸的內容)
. <- 返回 ed 的命令行狀態
w readme.text <- 將文件命名爲「readme.text」並保存(注意:若是是編輯已經存在的文件,只須要敲入 w 便可)
q <- 徹底退出 ed 編輯
輸入p當前的行內容
輸入行號移動到第幾行
刪除命令是d
1d刪除第幾行
刪除包含regular的行斜槓之間是正則表達式,刪除這一行,下行移到當前行
/regular/d刪除當前行
g/regular/d刪除全部行 這時的g是全局global
替代文件
[addrress]s /pattern/replacement/flag
s/regular/replacement/替換當前行第一次出現[regular]單詞
s/regular/replacement/g同一行的屢次出現,若沒找到會出現?這時的g指的是屢次
/regular/s/regular/replacement/g這個命令影響文件中與這個地址匹配的第一行,地址和模式沒必要相同
/T00000000200/s/T00000000200/22/g第一個是地址,第二個是模式
1命令
22
g/T00000000200/s/T00000000200/22/g
sed和ed不一樣,他不能交互式使用,sed與ed不一樣的是他是面向字符流的,默認狀況下,到sed的全部全部輸入命令都會相應處理,
並轉爲標準輸出
ed只做用於當前行,而sed會遍歷全部行,awk是做爲可編程的編輯器開發的,同sed同樣,他也是面向字符流的,而且解釋編輯命令的腳本
/regular/{print}
awk最獨特的特徵是分拆或者拆分每個輸入行,並生成每個可用於腳本處理的獨立單詞
命令行語法
command [option] scripts filename
sed和awk共同的選項
sed -f scriptsfile inputfile
$cat list
Yesterday was my birthday, so some of my classmates sent me presents.Mother prepared a tea
party for me. I invited all of them to come and take part in it.
The tea party began at half past six. There were cold drinks and refreshments.
We ate, talked and laughed. We felt that we were the happiest men in the worldI like the Chinese
new year better than any other festival. This is a time especially for rest and joy.I need not study.
I wear good clothes and eat good food. I have a good time from morning till night.I am as happy
as a king.No sooner had the witer vacation begun than I returned to my native town. Of course Imust make
good use of it;.In the morning I reviewed my lessons and read newspapers or magazines.
In the afternoon I played ball games with my friends or went fishing in the river.
At night I watched television with my family. hardly had the clock on the wall struck ten when I went to
bed.
I live very happily today! In the morning, it is very fine! Then I climb the mountain with family,
the air on the mountain is very fresh, the flowers plants and trees on the mountain all seem extremely
beautiful. Coming back home in the evening, family and I sat and watched TV together, we are returning and
eating the fruit while chatting, the whole family is happy and harmonious!
sed命令
sed 's/my/gdc/g' list 全部行的my 替換爲gdc
有三種方式指定命令行上的多重指令
1.用分號
sed 's/my/gdc/g;s/and/"##"/g' list
2.用-e選項
sed -e 's/my/gdc/g' -e 's/and/"##"/g' list
3。使用shell的分行指令功能
$ sed '
> s/my/gdc/g
> s/and/"##"/g list
若是出現了錯誤,就用gsed調試
4.腳本文件
$cat tt
s/my/gdc/g
s/and/"##"/g
$sed -f tt list
經常使用命令
阻止輸入行的自動提示
sed -n -f tt list 這時就看不了輸出的內容了
sed -f tt list操做替換後的輸出
使用awk
awk和sed結構相同,但過程不一樣awk不像編輯器而像一種語言,語句和函數取代了使用一個或者兩個字符組成的序列,列如使用
print 打印表達式的值或打印當前輸入行的內容,一般狀況下awk將每行輸入解釋爲輸入記錄,而將上一行輸入的單詞解釋爲一個字段
由空格和tab健分割
$awk '{print $1}' list ##輸出第一個單詞
$awk '/Yesterday/' list ##匹配輸出第一行的數據Yesterday was my birthday, so some of my classmates sent me presents.Mother prepared a tea
$awk '/Yesterday/ {print $1}' list #Yesterday
5.
awk -F, '/Yesterday/ {print $1}' list #將分隔符變爲,Yesterday was my birthday
將每一個字段單獨打印在這一行上,多重命令由;隔開
awk -F, '/Yesterday/ {print $1;print $2;print $3}' list
awk -F, '/Yesterday/ {print $1;print $2}' list
#Yesterday was my birthday
#so some of my classmates sent me presents.Mother prepared a tea
awk命令行選項
-f 文件跟隨腳本名
-F分隔符
-v var=value
同時使用sed和awk
sed -f tt list |awk -F, '{print $1}'
正則表達式