35 sed 命令

sed是一种流编辑器,它是文本处理中非常中的工具,能够完美的配合正则表达式使用,功能不同凡响。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输出。Sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。

使用这段文本做演示:

$ 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字符串替换成your,下面的语句应该很好理解(s表示替换命令,/my/表示匹配my,/your/表示把匹配替换成Hao Chen’s,/g 表示一行上的替换所有的匹配):

root@ubuntu:~# sed "s/my/your/g" pets.txt 
This is your cat
  your 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 your goat
  your goat's name is adam

再注意:上面的sed并没有对文件的内容改变,只是把处理过后的内容输出,如果你要写回文件,你可以使用重定向。
或使用 -i 参数直接修改文件内容:

root@ubuntu:~# 
root@ubuntu:~# 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
root@ubuntu:~# sed -i "s/my/your/g" pets.txt 
root@ubuntu:~# cat pets.txt 
This is your cat
  your 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 your goat
  your goat's name is adam

在每一行前面加一点东西:

sed "s/^/#/g" pets.txt
sed "s/^This/#This/g" pets.txt
sed "s/^This/#/g" pets.txt
root@ubuntu:~# sed "s/^/#/g" pets.txt
#This is your cat
#  your 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 your goat
#  your goat's name is adam
root@ubuntu:~# sed "s/^This/#This/g" pets.txt
#This is your cat
  your 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 your goat
  your goat's name is adam
root@ubuntu:~# sed "s/^This/#/g" pets.txt
# is your cat
  your cat's name is betty
# is your dog
  your dog's name is frank
# is your fish
  your fish's name is george
# is your goat
  your goat's name is adam

在每一行后面加一点东西

root@ubuntu:~# sed "s/$/\!\!\!/g" pets.txt
This is your cat!!!
  your 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 your goat!!!
  your goat's name is adam!!!

在sed中可用的正则表达式的锚定字符:

  • <abc 表示以abc开头的词
  • abc> 表示以abc结尾的词

比如我们想把your换成her:

root@ubuntu:~# sed "s/\<you/he/g" pets.txt
This is her cat
  her cat's name is betty
This is her dog
  her dog's name is frank
This is her fish
  her fish's name is george
This is her goat
  her goat's name is adam

又想把her换成his:

root@ubuntu:~# sed "s/\<you/he/g" pets.txt > herpets.txt
root@ubuntu:~# sed "s/er\>/is/g" herpets.txt 
This is his cat
  his cat's name is betty
This is his dog
  his dog's name is frank
This is his fish
  his fish's name is george
This is his goat
  his goat's name is adam

另外一个例子

root@ubuntu:~# cat html.txt 
<b>This</b> is what <span style="text-decoration: underline;">I</span> meant. Understand?

假设我们想去掉html标签,使用如下正则表达式:

root@ubuntu:~# sed "s/<.*>//g" html.txt 
 meant. Understand?

得到的结果是错误的,为什么呢?
因为正则表达式<.*> 表示<>中间的任意并且任意数量字符。其匹配结果为:

<b>This</b> is what <span style="text-decoration: underline;">I</span>

使用如下表达式可以正常匹配:

root@ubuntu:~#  sed 's/<[^>]*>//g' html.txt
This is what I meant. Understand?

其中方括号内的内容,指了除了>的字符重复0次或多次。

在sed中使用量词

只替第三行的pattern

root@ubuntu:~# sed '3s/your/my/' pets.txt 
This is your cat
  your cat's name is betty
This is my dog
  your dog's name is frank
This is your fish
  your fish's name is george
This is your goat
  your goat's name is adam

替换3,6行个pattern

root@ubuntu:~# sed '3,6s/your/my/' pets.txt 
This is your cat
  your 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 your goat
  your goat's name is adam

替换每一行的第一个s

root@ubuntu:~# sed 's/s/S/1' pets.txt 
ThiS is your cat
  your 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 your goat
  your goat'S name is adam

替换每一行第2个以后的s:

root@ubuntu:~# sed 's/s/S/2g' pets.txt 
This iS your cat
  your 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 your goat
  your goat's name iS adam

多个匹配

如果一次需要匹配多个模式,可以使用下面的方法:

root@ubuntu:~# sed '1,3s/your/my/g; 3,$s/This/That/g' pets.txt 
This is my cat
  my cat's name is betty
That is my dog
  your dog's name is frank
That is your fish
  your fish's name is george
That is your goat
  your goat's name is adam

可以使用&来当做被匹配的变量:

root@ubuntu:~# sed 's/your/[&]/g' pets.txt 
This is [your] cat
  [your] 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 [your] goat
  [your] goat's name is adam

分组

root@ubuntu:~# cat 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
root@ubuntu:~# sed 's/This is your \([^,]*\), .* is \(.*\)/\1:\2/g' my.txt
cat:betty
dog:frank
That is your fish, your fish's name is george
That is my goat, my goat's name is adam

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,293评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,604评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,958评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,729评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,719评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,630评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,000评论 3 397
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,665评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,909评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,646评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,726评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,400评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,986评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,959评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,197评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,996评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,481评论 2 342

推荐阅读更多精彩内容

  • 本文承接之前写的三十分钟学会AWK一文,在学习完AWK之后,趁热打铁又学习了一下SED,不得不说这两个工具真的堪称...
    mylxsw阅读 4,382评论 3 74
  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 12,131评论 2 34
  • sed篇总共分成6章:(简书版) Sed&awk笔记之sed篇:简单介绍 Sed&awk笔记之sed篇:模式空间与...
    magic5650阅读 815评论 0 3
  • sed与awk实例 文本间隔 在每一行后面增加一空行 将原来的所有空行删除并在每一行后面增加一空行。这样在输出的文...
    stuha阅读 1,881评论 0 21
  • 这篇是当初看完Chinaunix论坛的帖子“抛砖引玉----翻译加注sed1line”的笔记,最近无聊从Evern...
    magic5650阅读 1,147评论 0 4