sed 簡明教程
這篇文章同樣出自陳皓的手筆,與之前的AWK那篇文章一樣,都是神級作品,原文地址 sed簡明教程
用s命令替換
我使用下面的這段文字做演示:
$ cat pets.txt
This is my cat
my cat's name is betty
This is my dog
my dog's name is frank
This is my fish
my fish's name is george
This is my goat
my goat's name is adam
把其中的my字串替換成Hao Chen’s,下面的語句應該很好理解(s表示替換命令,/my/表示匹配my,/Hao Chen’s/表示把匹配替換成Hao Chen’s,/g 表示一行上的替換所有的匹配):
$ sed "s/my/Hao Chen's/g" pets.txt
This is Hao Chen's cat
Hao Chen's cat's name is betty
This is Hao Chen's dog
Hao Chen's dog's name is frank
This is Hao Chen's fish
Hao Chen's fish's name is george
This is Hao Chen's goat
Hao Chen's goat's name is adam
注意:如果你要使用單引號,那麼你沒辦法通過\’這樣來轉義,就有雙引號就可以了,在雙引號內可以用\”來轉義。
再注意:上面的sed並沒有對檔案的內容改變,只是把處理過後的內容輸出,如果你要寫回檔案,你可以使用重定向,如:
$ sed "s/my/Hao Chen's/g" pets.txt > hao_pets.txt
或使用 -i 引數直接修改檔案內容:
$ sed -i "s/my/Hao Chen's/g" pets.txt
在每一行最前面加點東西:
$ sed 's/^/#/g' pets.txt
#This is my cat
# my cat's name is betty
#This is my dog
# my dog's name is frank
#This is my fish
# my fish's name is george
#This is my goat
# my goat's name is adam
在每一行最後面加點東西:
$ sed 's/$/ --- /g' pets.txt
This is my cat ---
my cat's name is betty ---
This is my dog ---
my dog's name is frank ---
This is my fish ---
my fish's name is george ---
This is my goat ---
my goat's name is adam ---
順手介紹一下正規表示式的一些最基本的東西:
^ 表示一行的開頭。如:/^#/ 以#開頭的匹配。
$ 表示一行的結尾。如:/} $/ 以}結尾的匹配。
\< 表示詞首。 如 \< abc 表示以 abc 為首的詞。
> 表示詞尾。 如 abc> 表示以 abc 結尾的詞。
- . 表示任何單個字元。
- * 表示某個字元出現了0次或多次。
- [ ] 字符集合。 如:[abc]表示匹配a或b或c,還有[a-zA-Z]表示匹配所有的26個字元。如果其中有^表示反,如[^a]表示非a的字元
- 正規則表示式是一些很牛的事,比如我們要去掉某html中的tags:
html.txt
<b>This</b> is what <span style="text-decoration: underline;">I</span> meant. Understand?
看看我們的sed命令
# 如果你這樣搞的話,就會有問題
$ sed 's/<.*>//g' html.txt
Understand?
# 要解決上面的那個問題,就得像下面這樣。
# 其中的'[^>]' 指定了除了>的字元重複0次或多次。
$ sed 's/<[^>]*>//g' html.txt
This is what I meant. Understand?
我們再來看看指定需要替換的內容:
$ sed "3s/my/your/g" pets.txt
This is my cat
my cat's name is betty
This is your dog
my dog's name is frank
This is my fish
my fish's name is george
This is my goat
my goat's name is adam
下面的命令只替換第3到第6行的文字。
$ sed "3,6s/my/your/g" pets.txt
This is my cat
my cat's name is betty
This is your dog
your dog's name is frank
This is your fish
your fish's name is george
This is my goat
my goat's name is adam
$ cat my.txt
This is my cat, my cat's name is betty
This is my dog, my dog's name is frank
This is my fish, my fish's name is george
This is my goat, my goat's name is adam
只替換每一行的第一個s:
$ sed 's/s/S/1' my.txt
ThiS is my cat, my cat's name is betty
ThiS is my dog, my dog's name is frank
ThiS is my fish, my fish's name is george
ThiS is my goat, my goat's name is adam
只替換每一行的第二個s:
$ sed 's/s/S/2' my.txt
This iS my cat, my cat's name is betty
This iS my dog, my dog's name is frank
This iS my fish, my fish's name is george
This iS my goat, my goat's name is adam
只替換第一行的第3個以後的s:
$ sed 's/s/S/3g' my.txt
This is my cat, my cat'S name iS betty
This is my dog, my dog'S name iS frank
This is my fiSh, my fiSh'S name iS george
This is my goat, my goat'S name iS adam
多個匹配
如果我們需要一次替換多個模式,可參看下面的示例:(第一個模式把第一行到第三行的my替換成your,第二個則把第3行以後的This替換成了That)
$ sed '1,3s/my/your/g; 3,$s/This/That/g' my.txt
This is your cat, your cat's name is betty
This is your dog, your dog's name is frank
That is your fish, your fish's name is george
That is my goat, my goat's name is adam
上面的命令等價於:(注:下面使用的是sed的-e命令列引數)
sed -e '1,3s/my/your/g' -e '3,$s/This/That/g' my.txt
我們可以使用&來當做被匹配的變數,然後可以在基本左右加點東西。如下所示:
$ sed 's/my/[&]/g' my.txt
This is [my] cat, [my] cat's name is betty
This is [my] dog, [my] dog's name is frank
This is [my] fish, [my] fish's name is george
This is [my] goat, [my] goat's name is adam
圓括號匹配
使用圓括號匹配的示例:(圓括號括起來的正規表示式所匹配的字串會可以當成變數來使用,sed中使用的是\1,\2…)
$ sed 's/This is my \([^,]*\),.*is \(.*\)/\1:\2/g' my.txt
cat:betty
dog:frank
fish:george
goat:adam
上面這個例子中的正規表示式有點複雜,解開如下(去掉轉義字元):
正則為:This is my ([^,]),.*is (.)
匹配為:This is my (cat),……….is (betty)
然後:\1就是cat,\2就是betty
sed的命令
讓我們回到最一開始的例子pets.txt,讓我們來看幾個命令:
N命令
先來看N命令 —— 把下一行的內容納入當成緩衝區做匹配。
下面的的示例會把原文字中的偶數行納入奇數行匹配,而s只匹配並替換一次,所以,就成了下面的結果:
$ sed 'N;s/my/your/' pets.txt
This is your cat
my cat's name is betty
This is your dog
my dog's name is frank
This is your fish
my fish's name is george
This is your goat
my goat's name is adam
也就是說,原來的檔案成了:
This is my cat\n my cat's name is betty
This is my dog\n my dog's name is frank
This is my fish\n my fish's name is george
This is my goat\n my goat's name is adam
這樣一來,下面的例子你就明白了,
$ sed 'N;s/\n/,/' pets.txt
This is my cat, my cat's name is betty
This is my dog, my dog's name is frank
This is my fish, my fish's name is george
This is my goat, my goat's name is adam
a命令和i命令
a命令就是append, i命令就是insert,它們是用來新增行的。如:
# 其中的1i表明,其要在第1行前插入一行(insert)
$ sed "1 i This is my monkey, my monkey's name is wukong" my.txt
This is my monkey, my monkey's name is wukong
This is my cat, my cat's name is betty
This is my dog, my dog's name is frank
This is my fish, my fish's name is george
This is my goat, my goat's name is adam
# 其中的1a表明,其要在最後一行後追加一行(append)
$ sed "$ a This is my monkey, my monkey's name is wukong" my.txt
This is my cat, my cat's name is betty
This is my monkey, my monkey's name is wukong
This is my dog, my dog's name is frank
This is my fish, my fish's name is george
This is my goat, my goat's name is adam
我們可以運用匹配來新增文字:
# 注意其中的/fish/a,這意思是匹配到/fish/後就追加一行
$ sed "/fish/a This is my monkey, my monkey's name is wukong" my.txt
This is my cat, my cat's name is betty
This is my dog, my dog's name is frank
This is my fish, my fish's name is george
This is my monkey, my monkey's name is wukong
This is my goat, my goat's name is adam
下面這個例子是對每一行都會插入:
$ sed "/my/a ----" my.txt
This is my cat, my cat's name is betty
----
This is my dog, my dog's name is frank
----
This is my fish, my fish's name is george
----
This is my goat, my goat's name is adam
----
c命令
c 命令是替換匹配行
$ sed "2 c This is my monkey, my monkey's name is wukong" my.txt
This is my cat, my cat's name is betty
This is my monkey, my monkey's name is wukong
This is my fish, my fish's name is george
This is my goat, my goat's name is adam
$ sed "/fish/c This is my monkey, my monkey's name is wukong" my.txt
This is my cat, my cat's name is betty
This is my dog, my dog's name is frank
This is my monkey, my monkey's name is wukong
This is my goat, my goat's name is adam
d命令
刪除匹配行
$ sed '/fish/d' my.txt
This is my cat, my cat's name is betty
This is my dog, my dog's name is frank
This is my goat, my goat's name is adam
$ sed '2d' my.txt
This is my cat, my cat's name is betty
This is my fish, my fish's name is george
This is my goat, my goat's name is adam
$ sed '2,$d' my.txt
This is my cat, my cat's name is betty
p命令
列印命令
你可以把這個命令當成grep式的命令
# 匹配fish並輸出,可以看到fish的那一行被打了兩遍,
# 這是因為sed處理時會把處理的資訊輸出
$ sed '/fish/p' my.txt
This is my cat, my cat's name is betty
This is my dog, my dog's name is frank
This is my fish, my fish's name is george
This is my fish, my fish's name is george
This is my goat, my goat's name is adam
# 使用n引數就好了
$ sed -n '/fish/p' my.txt
This is my fish, my fish's name is george
# 從一個模式到另一個模式
$ sed -n '/dog/,/fish/p' my.txt
This is my dog, my dog's name is frank
This is my fish, my fish's name is george
#從第一行列印到匹配fish成功的那一行
$ sed -n '1,/fish/p' my.txt
This is my cat, my cat's name is betty
This is my dog, my dog's name is frank
This is my fish, my fish's name is george
相關文章
- 簡明 docker 教程Docker
- 最簡明的Shiro教程
- GitBook簡明安裝教程Git
- Linux sed命令詳細說明Linux
- Linux命令-Sed用法教程Linux
- 《簡明 PHP 教程》00 開篇PHP
- 《簡明 PHP 教程》04 基礎PHP
- 《簡明 PHP 教程》02 安裝PHP
- Raspberry Pi 3簡明配置教程
- 《簡明 PHP 教程》01 關於 PHPPHP
- sed命令簡單使用示例分享
- 《簡明 PHP 教程》03 第一步PHP
- 【Linux學習教程】Linux中Sed命令如何使用?Linux
- Linux入門教程之sed 命令常用操作介紹Linux
- 一條資料HBase之旅,簡明HBase入門教程開篇
- sed用法
- CentOS 7升級核心簡明說明CentOS
- Spark簡明筆記Spark筆記
- Gson簡明處理
- HTTP簡明學習HTTP
- Vim簡明學習
- fetch簡明學習
- React簡明學習React
- git簡單明瞭Git
- 乾貨!上古神器 sed 教程詳解,小白也能看的懂
- sed 應用
- linux之sedLinux
- LINUX命令-sedLinux
- Markdown 常用簡明語法
- jarsigner 簡單使用說明JAR
- OpenGrok簡單使用說明
- 【LVS】簡介與說明
- Linux sed命令用法Linux
- linux之 sed命令Linux
- sed用法點滴
- linux sed學習Linux
- sed 修改檔案
- sed命令小記
- eachdemo/rbac 的簡單說明