linux shell 腳本攻略學習19--sed命令詳解


sed(意為流編輯器英語stream editor”的縮寫)是Unix/linux常見的命令行程序。sed用來把文檔字符串里面的文字經過一系列編輯命令轉換為另一種格式輸出,即文本替換。sed通常用來匹配一個或多個正則表達式的文本進行處理。

輸入sed --help查看幫助信息:

amosli@amosli-pc:~/learn/sed$ sed --help
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  -n, --quiet, --silent
                 suppress automatic printing of pattern space
  -e script, --expression=script
                 add the script to the commands to be executed
  -f script-file, --file=script-file
                 add the contents of script-file to the commands to be executed
  --follow-symlinks
                 follow symlinks when processing in place
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if extension supplied)
  -l N, --line-length=N
                 specify the desired line-wrap length for the `l' command
  --posix
                 disable all GNU extensions.
  -r, --regexp-extended
                 use extended regular expressions in the script.
  -s, --separate
                 consider files as separate rather than as a single continuous
                 long stream.
  -u, --unbuffered
                 load minimal amounts of data from the input files and flush
                 the output buffers more often
      --help     display this help and exit
      --version  output version information and exit

If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.

語法:

 sed [OPTION]... {script-only-if-no-other-script} [input-file]...

參數-實例:

測試文件:test.txt

amosli@amosli-pc:~/learn/sed$ cat test.txt 
hi,this is sed command test file
linux world is so amazing

you will like it!

 

1.簡單的文本替換

常用格式:

sed 's/pattern/replace_string'  file
#或者
cat file | sed 's/pattern/replace_string' file

其中pattern為模式,replace_string為替換詞.即,將符合pattern的字符串替換為replace_string .

例:將will變為大寫WILL

amosli@amosli-pc:~/learn/sed$ sed "s/will/WILL/" test.txt 
hi,this is sed command test file
linux world is so amazing

you WILL like it!

使用標准輸入:

amosli@amosli-pc:~/learn/sed$ echo "this is test" | sed "s/test/TEST/"
this is TEST

2.-i參數,將替換結果應用於原文件

默認情況下sed不會修改原文件,因為式作原理為:sed編輯器逐行處理文件(或輸入),並將結果發送到屏幕。具體過程如下:首先sed把當前正在處理的行保存在一個臨時緩存區中(也稱為模式空間),然后處理臨時緩沖區中的行,完成后把該行發送到屏幕上。sed每處理完一行就將其從臨時緩沖區刪除,然后將下一行讀入,進行處理和顯示。處理完輸入文件的最后一行后,sed便結束運行。sed把每一行都存在臨時緩沖區中,對這個副本進行編輯,所以不會修改原文件。

amosli@amosli-pc:~/learn/sed$ sed -i 's/like/LOVE/' test.txt 
amosli@amosli-pc:~/learn/sed$ cat test.txt 
hi,this is sed command test file
linux world is so amazing

you will LOVE it!

英文提示信息:

 -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if extension supplied)

3、關於后綴/g

/g默認表示替換所有符合條件的文本。

amosli@amosli-pc:~/learn/sed$ echo "test1 test2 test3 test4 " | sed "s/test/TEST/g"
TEST1 TEST2 TEST3 TEST4 

/Ng表示從第N個符合條件的開始替換.

amosli@amosli-pc:~/learn/sed$ echo "test1 test2 test3 test4 " | sed "s/test/TEST/3g"
test1 test2 TEST3 TEST4 

4、關於字符'/'

字符‘/’在sed命令中扮演着定界符的作用,'|'和':'的作用和'/'一樣,可以做為定界符使用

如:

amosli@amosli-pc:~/learn/sed$ echo "test1 test2 test3 test4 " | sed "s:test:TEST:g"
TEST1 TEST2 TEST3 TEST4 
amosli@amosli-pc:~/learn/sed$ echo "test1 test2 test3 test4 " | sed "s|test|TEST|g"
TEST1 TEST2 TEST3 TEST4 

當定界符出現在樣式內部中,可以使用前綴\對它進行轉義,如:

amosli@amosli-pc:~/learn/sed$ echo "te/st1 te/st2 te/st3 te/st4 " | sed "s/test/TEST/g"
te/st1 te/st2 te/st3 te/st4

這樣替換就失效了,使用前綴\進行轉義

amosli@amosli-pc:~/learn/sed$ echo "te/st1 te/st2 te/st3 te/st4 " | sed "s/te\/st/TEST/g"
TEST1 TEST2 TEST3 TEST4 

5.移除空白行

amosli@amosli-pc:~/learn/sed$ sed '/^$/d' test.txt 
hi,this is sed command test file
linux world is so amazing
you will LOVE it!

/pattern/d 會移除匹配樣式的行

^$行尾標記緊臨表示空白行。

6.&代表已經匹配的字符串

amosli@amosli-pc:~/learn/sed$ echo this is a test | sed 's/\w\+/[&]/g'
[this] [is] [a] [test]

\w\+ 正則表達式表示匹配每一個單詞,&對應於之前所匹配到的單詞,使用[&]進行替換。

7.子串匹配字符口中\1

amosli@amosli-pc:~/learn/sed$ echo this is digit 7 in a number | sed 's/digit \([0-9]\)/\1/'
this is 7 in a number

這條命令將digit 7 替換為7,\(pattern\) 用於匹配子串,模式被包含在使用生命線轉義過的()中。對於匹配到的第一個子串,其對應的標記是\1,匹配到的第二個子串為\2,以次類推。

8.組合多個表達式

amosli@amosli-pc:~/learn/sed$ cat test.txt 
hi,this is sed command test file
linux world is so amazing

you will LOVE it!
amosli@amosli-pc:~/learn/sed$ cat test.txt | sed 's/linux/LINUX/' | sed 's/sed/SED/'
hi,this is SED command test file
LINUX world is so amazing

you will LOVE it!

也可以使用:

amosli@amosli-pc:~/learn/sed$ cat test.txt | sed "s/linux/LINUX/; s/sed/SED/"
hi,this is SED command test file
LINUX world is so amazing

you will LOVE it!

格式為:

sed 'expression' | sed 'expression'
#or
sed 'expression;expression'

9.引用

定義變量:

amosli@amosli-pc:~/learn/sed$ txt=hello

引用變量:

amosli@amosli-pc:~/learn/sed$ echo "hello world" | sed "s/$txt/HELLO/g"
HELLO world

10.可選參數:

 命令  功能
 a\

 在當前行后添加一行或多行。多行時除最后一行外,每行末尾需用“\”續行

 c\  用此符號后的新文本替換當前行中的文本。多行時除最后一行外,每行末尾需用"\"續行
 i\  在當前行之前插入文本。多行時除最后一行外,每行末尾需用"\"續行
 d  刪除行
 h  把模式空間里的內容復制到暫存緩沖區
 H  把模式空間里的內容追加到暫存緩沖區
 g  把暫存緩沖區里的內容復制到模式空間,覆蓋原有的內容
 G  把暫存緩沖區的內容追加到模式空間里,追加在原有內容的后面
 l  列出非打印字符
 p  打印行
 n  讀入下一輸入行,並從下一條命令而不是第一條命令開始對其的處理
 q  結束或退出sed
 r  從文件中讀取輸入行
 !  對所選行以外的所有行應用命令
 s  用一個字符串替換另一個
 g  在行內進行全局替換
   
 w  將所選的行寫入文件
 x  交換暫存緩沖區與模式空間的內容
 y  將字符替換為另一字符(不能對正則表達式使用y命令)

其他選項:

 選項     功能
 -e     進行多項編輯,即對輸入行應用多條sed命令時使用
 -n     取消默認的輸出
 -f     指定sed腳本的文件名

元字符總結:

 元字符  功能  示例
 ^  行首定位符  /^my/  匹配所有以my開頭的行
 $  行尾定位符  /my$/  匹配所有以my結尾的行
 .  匹配除換行符以外的單個字符  /m..y/  匹配包含字母m,后跟兩個任意字符,再跟字母y的行
 *  匹配零個或多個前導字符  /my*/  匹配包含字母m,后跟零個或多個y字母的行
 []  匹配指定字符組內的任一字符  /[Mm]y/  匹配包含My或my的行
 [^]  匹配不在指定字符組內的任一字符  /[^Mm]y/  匹配包含y,但y之前的那個字符不是M或m的行
 \(..\)  保存已匹配的字符  1,20s/\(you\)self/\1r/  標記元字符之間的模式,並將其保存為標簽1,之后可以使用\1來引用它。最多可以定義9個標簽,從左邊開始編號,最左邊的是第一個。此例中,對第1到第20行進行處理,you被保存為標簽1,如果發現youself,則替換為your。
 &  保存查找串以便在替換串中引用  s/my/**&**/  符號&代表查找串。my將被替換為**my**
 \<  詞首定位符  /\<my/  匹配包含以my開頭的單詞的行
 \>  詞尾定位符  /my\>/  匹配包含以my結尾的單詞的行
 x\{m\}  連續m個x  /9\{5\}/ 匹配包含連續5個9的行
 x\{m,\}  至少m個x  /9\{5,\}/  匹配包含至少連續5個9的行
 x\{m,n\}  至少m個,但不超過n個x  /9\{5,7\}/  匹配包含連續5到7個9的行


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM