sed 命令详解

sed 即 stream editor,一个简单而强大的文本解析转换工具,1973-1974年期间由贝尔实验室的 Lee E. McMahon 开发,能够完美的配合正则表达式使用。
处理文本时,当前处理的行会先存储在临时缓冲区中,称为模式空间(pattern space),操作完成后再把缓冲区的内容送往屏幕。接着处理下一行,直到文件末尾。文件内容并没有改变,除非使用重定向存储输出。
sed 主要用来自动编辑一个或多个文件,简化对文件的反复操作,编写转换程序等。

1. 使用 s 命令替换

基本使用

sed 默认情况下通过 STDIN 输入流读取数据,可以利用管道符(|)进行重定向。如下:

$ echo "This is a test" | sed 's/test/big test/'
This is a big test

s 替换命令的格式为:s/pattern/replacement/flags ,如下:

$ cat test.txt
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.
$
$ sed 's/dog/cat/' test.txt
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.

使用 s 命令进行替换时,默认只替换每行中的第一个匹配。全局替换需增加 g 选项。

$ sed 's/o/O/g' test.txt
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.

sed 默认只将处理后的内容输出,而不对原文件的内容做任何改动。如需写入文件,可使用重定向:$ sed 's/dog/cat/' test.txt > test2.txt
或通过 -i 选项直接修改文件内容:$ sed -i 's/dog/cat/' test.txt

address

如果需要将命令应用到特定的一行或多行内容,可以使用 line addressing。格式为 [address[,address]][!]command
address 可以是数字模式,也可以通过逗号分隔两个 address 表示一个区间范围。
如只将第2行中的 dog 替换为 cat

$ sed '2s/dog/cat/' test.txt
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.

或者$ sed '1,3s/dog/cat/' test.txt,替换1-3行的内容。
$ sed '2,$s/dog/cat/' test.txt,替换第2行到最后一行的内容。

! 表示完成匹配后是否在该行执行替换命令。加上 ! 表示不执行。如下:

$ cat lines.txt
This is line one
This is line two
This is line three
This is line four
$
$ sed '/one/,2s/line/LINE/' lines.txt
This is LINE one
This is LINE two
This is line three
This is line four
$
$ sed '/one/,2!s/line/LINE/' lines.txt
This is line one
This is line two
This is LINE three
This is LINE four
替换选项

sed 的替换命令除了可以附加 g 选项(全局替换)外,还有更多选项适用于不同的情形。

  • 数字。表示只替换每行中的第 n 个匹配项,如:
$ sed 's/o/O/2' test.txt
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.

$ sed 's/o/O/2g' test.txt 替换每行中从第二个开始的所有匹配项(即替换每行中从第二个 o 开始直到该行行尾的所有 o 字符)

  • p,表示打印处理前的原始内容,结合上 -n 选项(不打印输出)则可以只输出处理过的内容。
$ sed 's/three/3/p' lines.txt
This is line one
This is line two
This is line 3
This is line 3
This is line four
$
$ sed -n 's/three/3/p' lines.txt
This is line 3
  • w,将处理过的内容写入文件
$ sed 's/three/3/w line3.txt' lines.txt
$
$ cat line3.txt
This is line 3
sed 命令选项
  • 删除:d
    $ sed '2,$d' lines.txt 删除 lines.txt 中第2行到最后一行的内容。
    $ sed '/two/d' lines.txt 删除 lines.txt 中包含 two 的行。
  • 追加(行下):a
$ sed '/two/a\
> This is a line behind line 2' lines.txt
This is line one
This is line two
This is a line behind line 2
This is line three
This is line four
  • 插入(行上):i
$ sed '2i\
> This is a line above line 2' lines.txt

在第2行以上插入内容。

  • 修改:c
$ sed '/two/c\
> Line 2' lines.txt

将包含 two 的行修改为 Line 2(整行内容替换为 Line 2)。

  • 字符转换:y
    格式为:[address]y/inchars/outchars/
    输入文件中所有包含在 inchars 中的字符都将替换为 outchars 中对应的字符。
$ cat line_number.txt
This is line 1.
This is line 2.
This is line 3.
This is another line 1.
$
$ sed 'y/123/456/' line_number.txt
This is line 4.
This is line 5.
This is line 6.
This is another line 4.
其他用法
  • 打印输出
    sed 的 p 命令可以达到类似 grep 的效果,结合上 address 功能还可以完成更复杂的筛选打印操作。
$ sed -n 'p' lines.txt
This is line one
This is line two
This is line three
This is line four
$
$ sed -n '2,3p' lines.txt
This is line two
This is line three
$
$sed -n '/two/,/three/p' lines.txt
This is line two
This is line three
  • 多个匹配
    可以通过 -e 选项实现多个匹配的替换
$ sed -e 's/fox/kangaroo/;s/dog/cat/' test.txt
The quick brown kangaroo jumps over the lazy cat.
The quick brown kangaroo jumps over the lazy cat.
The quick brown kangaroo jumps over the lazy cat.
The quick brown kangaroo jumps over the lazy cat.

也可以这样:

$ sed -e '
> s/brown/red/
> s/fox/kangaroo/
> s/dog/cat/' test.txt
The quick red kangaroo jumps over the lazy cat.
The quick red kangaroo jumps over the lazy cat.
The quick red kangaroo jumps over the lazy cat.
The quick red kangaroo jumps over the lazy cat.
  • 从脚本文件中读取命令
$ cat script.sed
s/brown/red/
s/fox/kangaroo/
s/dog/cat
$
$ sed -f script.sed test.txt
The quick red kangaroo jumps over the lazy cat.
The quick red kangaroo jumps over the lazy cat.
The quick red kangaroo jumps over the lazy cat.
The quick red kangaroo jumps over the lazy cat.
  • 可以使用 & 变量表示前面的匹配项,如:
$ sed 's/fox/&es/' test.txt
The quick brown foxes jumps over the lazy dog.
The quick brown foxes jumps over the lazy dog.
The quick brown foxes jumps over the lazy dog.
The quick brown foxes jumps over the lazy dog.

参考书籍和文章:

Linux Command Line and Shell Scripting Bible, 3rd Edition
SED 简明教程 by 陈皓
Linux 命令大全 / SED 命令

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

推荐阅读更多精彩内容

  • 原文链接:sed命令_Linux sed 命令用法详解:功能强大的流式文本编辑器 http://man.linu...
    e2ae5d4bd7c1阅读 771评论 0 1
  • linux sed命令详解 1. Sed简介 sed 是一种在线编辑器,它一次处理一行内容。处理时,把当前处理的行...
    很少更新了阅读 2,143评论 0 6
  • shell sed命令详解 sed是一种在线编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中...
    zplodge阅读 1,887评论 0 0
  • 1. 简介 sed命令是一个很强大的文本编辑器,可以对来自文件、以及标准输入的文本进行编辑。 执行时,sed会从文...
    tyrone_li阅读 21,300评论 2 12
  • 本文承接之前写的三十分钟学会AWK一文,在学习完AWK之后,趁热打铁又学习了一下SED,不得不说这两个工具真的堪称...
    mylxsw阅读 4,361评论 3 74