歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> Sed與AWK入門教程之Sed篇

Sed與AWK入門教程之Sed篇

日期:2017/3/1 16:14:18   编辑:關於Linux
Sed與AWK入門教程之Sed篇 Sed和AWK是*nix命令行裡面文本處理的神器,相當的強大.它們都是面向行的,或者說它們處理文本的方式都是一行接著一行的處理,從標准輸入或者文件中讀取內容,一行一行的執行腳本命令,然後打印輸出到標准輸出,直到文件結尾(EOF). Sed Sed是一個流編輯器(Stream editor),它的功能在於對於一個輸入流進行編輯和處理.相當於是對一個輸入流進行腳本性的編輯.其實它就是對一個輸入流進行ed(一個面向行的編輯器)的腳本編輯. Sed命令包括二部分,一部分是命令行參數或者說命令的執行方式,另一部分就是它的編輯命令,也常稱作腳本. 命令執行方式: sed [OPTIONS] -e 'scripts' | -f script-file [input-files] 如: [plain] [alex@alexon:~]$sed -n -e 'p' speech.txt With great power comes great responsibility. The freedom is nothing but a chance to be better. 看出來了吧,這相當於cat 命令. [html] [alex@alexon:~]$cat speech.txt With great power comes great responsibility. The freedom is nothing but a chance to be better. 命令行參數 可以參考man手冊.比較常用的有: -n --quiet --silent 不自動打印模式空間.簡單來講就是不自動打印當前要處理的行.sed會讀入一行,放入到一個叫Pattern space(模式空間)裡,以便於執行編輯命令來處理它.默認情況下,會自動把這一行(Pattern space裡的內容)打印出來.對比下不指定這個參數時的情況就明白了: [html] [alex@alexon:~]$sed -e 'p' speech.txt With great power comes great responsibility. With great power comes great responsibility. The freedom is nothing but a chance to be better. The freedom is nothing but a chance to be better. 你會看到重復,原因就是第一行是默認打印的Pattern space的內容(也就是要處理的行的內容). 然後執行編輯命令,因為編輯命令是簡單的p(打印內容),所以你就看到重復的輸出. 但如果加了-n(或者--quiet --silent)會變成這樣: [html] [alex@alexon:~]$sed -n -e 'p' speech.txt With great power comes great responsibility. The freedom is nothing but a chance to be better. -e 'scripts' 指定編輯命令,或者叫做腳本.就是一要執行的sed所支持的編輯命令.主要是模式匹配和文本替換,插入,刪除之類的編輯操作. 這個選項可以指定多個,sed會按從左到右的順序,一個一個的執行. [html] [alex@alexon:~]$sed -e '=' -e 'p' -e 's/great/poor/' speech.txt 1 With great power comes great responsibility. With poor power comes great responsibility. 2 The freedom is nothing but a chance to be better. The freedom is nothing but a chance to be better. 解析:第一個命令'='是打印行號;第二個是打印這一行;第三個是做替換. -f script-file 執行指定的文件中的腳本.也就是不把編輯命令放在命令行中,而是放在一個文件裡面.讓sed去執行文件裡面的命令. -i[Suffix] --in-place[=Suffix] 即時的編輯輸入的文件.如果指定Suffix,則會用其作後綴來備份輸入文件.默認的行為是從輸入文件中一行一行的讀入文本,然後執行命令,然後輸出結果到標准輸出,也就是說對原文本沒有影響,並不會改動原文件.但有些時候我們想改變原文件,也就是說要對原文件進行編輯.這時就需要用到此選項.為了不丟失數據,可以指定後綴來備份原文件. 例如: [plain] [alex@alexon:~]$cat speech.txt With great power comes great responsibility. The freedom is nothing but a chance to be better. [alex@alexon:~]$sed -i.bak -e 's/great/poor/g' speech.txt [alex@alexon:~]$cat speech.txt With poor power comes poor responsibility. The freedom is nothing but a chance to be better. [alex@alexon:~]$cat speech.txt.bak With great power comes great responsibility. The freedom is nothing but a chance to be better. 命令是把文件中的great替換成poor,並把原文件備份為.bak. 到這裡,是不是讓你想起了強大的perl命令,也有類似的功能: [plain] [alex@alexon:~]$perl -p -i.bak -e 's/poor/great/g' speech.txt [alex@alexon:~]$cat speech.txt With great power comes great responsibility. The freedom is nothing but a chance to be better. [alex@alexon:~]$cat speech.txt.bak With poor power comes poor responsibility. The freedom is nothing but a chance to be better. 命令行參數僅是sed的一部分,它的主要核心部分是它的編輯命令,或者稱作它的腳本,也就是通過-e選項指定的,或者通過-f指定的腳本文件. 編輯命令的格式: [命令作用范圍][!] cmd [cmd-args] 如, [plain] [alex@alexon:~]$sed -n -e '1 p' speech.txt With great power comes great responsibility. 命令的作用范圍 也可以稱作是尋址.通俗的講就是指定後面的編輯命令的作用范圍,通常有幾種方式來指定范圍: 不指定 --- 如果不指定具體的范圍,那麼將作用到所有的行. [plain] [alex@alexon:~]$sed -n -e 'p' speech.txt With great power comes great responsibility. The freedom is nothing but a chance to be better. 用行號來指定 --- n, m第n行到第m行,特別的$代表最後一行 1, 3 ---- 第1行到第3行 1,$ ---- 第1 行到最後一行,也就是所有的行 相對的行數,可以在逗號後面用+m,如n,+m來表示n行以後到n+m行為止,的一個相對量,如: [plain] [alex@alexon:~]$sed -n -e '2,+3 p' speech.txt 2. The freedom is nothing but a chance to be better. 3. The tree of liberty must be refreshed from time to time with blood of patroits 4. and tyrants. 5. Life is like a box of chocolates, you never know what you gonna get. 跳躍性選擇行.-------可以用波浪~來跳躍性選擇, n~m,是指從第n行開始,後面每隔m行,執行一次,如:1~2,從第1行開始,每隔2行執行一次,也就是是執行1,3,5,7.....: [plain] [alex@alexon:~]$sed -n -e '1~2 p' speech.txt 1. With great power comes great responsibility. 3. The tree of liberty must be refreshed from time to time with blood of patroits 5. Life is like a box of chocolates, you never know what you gonna get. 模式匹配 指定作用范圍的最強大的地方就是在於可以使用模式匹配來指定.模式匹配的格式是: [/pattern1/], [/pattern2/] 如果僅指定一個匹配,則會在所有匹配的行為執行編輯命令,如果指定二個,則是第一個匹配pattern1的行到,第一次匹配pattern2的行. [plain] [alex@alexon:~]$sed -n -e '/great/ p' speech.txt 1. With great power comes great responsibility. [alex@alexon:~]$sed -n -e '/great/, /chocolates/ p' speech.txt 1. With great power comes great responsibility. 2. The freedom is nothing but a chance to be better. 3. The tree of liberty must be refreshed from time to time with blood of patroits 4. and tyrants. 5. Life is like a box of chocolates, you never know what you gonna get. 正則表達式 涉及到模式匹配就會涉及到正則表達式,sed的正則表達式與標准的略有不同.在命令行還可以指定-r --regexp-extended來使用擴展正則表達式. 位置符: ^ --- 行首 $ ----行尾 . ----任意非換行符'\n'符 \b ---- 一個單詞結尾,單詞定義為一連串的字母或數字,可以單獨放在一端,也可放二端. 限量符 * --- 0或1個或多個 \+ --- 1個或多個 \? --- 0或1 {m} --- 出現m次 {m,n} --- 出現m次到n次,如{1,5}表示出現1次到5次不等(1,2,3,4,5次) 轉義符 \ --- 可以轉義特殊字符 字符集 [] ---其內的任意字符 操作符 \| ---- 或操作,abc\|123匹配123或者abc \(...\) ----組合,形成一個組,主要用於索引 \n ---- 前面第n個組合, /\(123\)\1/ 則匹配123123 編輯命令 文本編輯命令也是非常熟悉的添加,插入,替換和刪除和其他一些如打印,打印行號等. add1[,add2] i text 插入 --- 在指定的行的前面插入文字 [plain] [alex@alexon:~]$sed -e '3 i abcd' speech.txt 1. With great power comes great responsibility. 2. The freedom is nothing but a chance to be better. abcd 3. The tree of liberty must be refreshed from time to time with blood of patroits 4. and tyrants. 5. Life is like a box of chocolates, you never know what you gonna get. add1[,add2] a text 添加 --- 在指定的行的後面添加文字 [plain] [alex@alexon:~]$sed -e '3 a abcd' speech.txt 1. With great power comes great responsibility. 2. The freedom is nothing but a chance to be better. 3. The tree of liberty must be refreshed from time to time with blood of patroits abcd 4. and tyrants. 5. Life is like a box of chocolates, you never know what you gonna get. add1[,add2] d刪除 --- 刪除指定的行 [plain] [alex@alexon:~]$sed -e '3 d' speech.txt 1. With great power comes great responsibility. 2. The freedom is nothing but a chance to be better. 4. and tyrants. 5. Life is like a box of chocolates, you never know what you gonna get. add1[,add2] s/pattern/replace/[opts] 替換把指定的行內的pattern替換為replace [plain] [alex@alexon:~]$sed -e '1, 3 s/great/poor/' speech.txt 1. With poor power comes great responsibility. 2. The freedom is nothing but a chance to be better. 3. The tree of liberty must be refreshed from time to time with blood of patroits 4. and tyrants. 5. Life is like a box of chocolates, you never know what you gonna get. 默認情況,只會替換行內第1個pattern. opts,可以指定選項以控制替換的行為 n --- 替換行內第n個pattern為replace g --- 替換行內所有的pattern為replace p --- 打印這一行,如果替換成功的話. add1[,add2] c text 把指定的行完整的替換為text [plain] [alex@alexon:~]$sed -e '1, 3 c abcd' speech.txt abcd 4. and tyrants. 5. Life is like a box of chocolates, you never know what you gonna get. p 打印 = 打印行號 知道了這些就可以應付大部分的文本處理.sed也有一些高級編輯命令如操作Pattern Space,或者分支等,但比較復雜,通常也用不到. 可以看出sed是一個流編輯器,它的強大之處在於可以以行的方式來腳本化處理文本.它的主要功能就是刪,查,換和添加.但它畢竟不是編程語言,所以它不能有變量,和循環,分支等邏輯.所以,sed通常與AWK一起使用.AWK更具有編程語言的特性,,它們剛好互補,一起使用構成了文本處理的二個利器.
Copyright © Linux教程網 All Rights Reserved