s命令說白了就是替換命令,在給定的字串中用反斜槓後面的字串代替反斜槓前面的字串。直接將字串用管道交給sed即可,直接看例子:
kevin@kevin-hp
-compaq
-pro
-4300
-aio-20
-pc-apj
-cto:~$ echo "this is a test"
| sed 's/test/big test/'
this is a big test
假如我們要處理文字,那麼應該這麼搞:
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.
kevin@kevin-hp-compaq-pro-4300-aio-20-pc-apj-cto:~$ 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.
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.
kevin@kevin-hp-compaq-pro-4300-aio-20-pc-apj-cto:~$ 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.
命令很簡單,把檔名放在命令後面就行了,輸出了替換後的字串。注意,這個時候替換的只是輸出的字串,檔案中的dog沒有被替換成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.
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.
很明白了吧,就是一次性替換多個,但要注意:*命令之間必須用「;」分隔。
還有你也可以這麼搞,不想一次性把要替換的word都寫出來,可以用bash shell的提示符來寫:
> 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.
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.
bash shell是這麼執行的,就是一旦發現了封尾的單引號,就開始執行命令。
以上無論這麼折騰,都是現場敲命令,那我們能不能將指令碼寫好,然後再處理呢,答案當然是可以。
首先編輯好指令碼script
s/brown/green/
s/fox/elephant/
s/dog/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.
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.
總而言之,言而總之,替換來替換去只是替換stdout上的字元,文字中的字元不動。
sed編輯器基礎
1.替換標記 sed的 s選項用來替換檔案中的內容,但是僅僅作用於每行的第一處,若需要替換其他地方則需要替換標記 s pattern replacement flags 4種可用替換標記 1.數字 表示文字每行記錄的第幾處進行替換 2.g 表示替換所有 3.p 表示原先行的內容要列印出來 4.w f...
sed編輯器基礎
一 更多的替換選項 替換標記 root localhost sed cat data4.txt this is a test of the test script.this is the second test of the test script.root localhost sed sed s ...
sed編輯器基礎操作
sed e 操作 檔案1檔案2 sed n e 操作 檔案1檔案2 sed f 指令碼檔案 檔案1 檔案2 sed e 操作 檔案1 檔案2 sed e n 檔案1 檔案2 e或 expression 表示用指定命令來處理輸入的文字檔案,只有乙個操作命令時可省略,一般在執行 多個操作命令使用 f 或...