语法
sed [options] '{command}' [filename]
替换命令
's/{old value}/{new value}/'
例子
$echo The tiger cubs will meet on Tuesday afer school | sed 's/tiger/wolf/'
The wolf cubs will meet on Tuesday afer school
$echo The tiger cubs will meet on Tuesday after school | sed -e 's/tiger/wolf/' -e 's/after/befor/'
The wolf cubs will meet on Tuesday befor school
$echo The tiger cubs will meet on Tuesday after scholl | sed 's/tiger/wolf/;s/after/before/'
The wolf cubs will meet on Tuesday before scholl
$echo The tiger cubs will meet on Tuesday after school | sed '
> s/tiger/wolf/
> s/after/before/
> '
The wolf cubs will meet on Tuesday before school
$echo The tiger cubs will meet this Tuesday at the same time as the meeting last Tuesday |sed 's/Tuesday/Thursday/'
The tiger cubs will meet this Thursday at the same time as the meeting last Tuesday
$echo The tiger cubs will meet this Tuesday at the same time as meeting last Tuesday | sed 's/Tuesday/Thursday/g'
The tiger cubs will meet this Thursday at the same time as meeting last Thursday
sed 可以用来将任意的可打印字符修改为任意其它
的可打印字符。如果您想将不可打印字符修改为可打印字符—例如,铃铛修改
为单词"bell"—sed 不是适于完成这项工作的工具(但tr 是)。
$cat sample_one
one 1
two 1
three 1
one 1
two 1
two 1
three 1
模式匹配
$sed '/two/ s/1/2/' sample_one
one 1
two 2
three 1
one 1
two 2
two 2
three 1
$sed '
> /two/ s/1/2/
> /three/ s/1/3/' sample_one
one 1
two 2
three 3
one 1
two 2
two 2
three 3
$sed '
> /two/ s/1/2/
> /three/ s/1/3/' sample_one > sample_two
$cat sample_two
one 1
two 2
three 3
one 1
two 2
two 2
three 3
$
使用sed脚本文件
$cat sedlist
/two/ s/1/2/
/three/ s/1/3/
$sed -f sedlist sample_one
one 1
two 2
three 3
one 1
two 2
two 2
three 3
行匹配
$sed '5,6 s/1/2/' sample_one
one 1
two 1
three 1
one 1
two 2
two 2
three 1
禁止显示 -n
$sed -n -f sedlist sample_one
$
显示选项 -p
$cat sedlist
/two/ s/1/2/p
/three/ s/1/3/p
$sed -n -f sedlist sample_one
two 2
three 3
two 2
two 2
three 3
$
$sed -n -f sedlist sample_one > sample_two
$cat sample_two
two 2
three 3
two 2
two 2
three 3
只显示第三行到第五行
$sed -n '3,5p' sample_one
three 1
one 1
two 1
$
删除行
删除匹配行
$sed '/two/ d' sample_one
one 1
three 1
one 1
three 1
删除指定行(第一到第三行)
$sed '1,3 d' sample_one
one 1
two 1
two 1
three 1
对于流编辑器,一般当它们涉及
到全局表达式时,特别是应用于删除操作时,有几点要记住:
上三角号(^) 表示一行的开始,因此,如果"two" 是该行的头三个字符,则
sed '/^two/ d' sample_one
将只删除该行。
美元符号($) 代表文件的结尾,或一行的结尾,因此,如果"two" 是该行的最
后三个字符,则
sed '/two$/ d' sample_one
将只删除该行。
sed '/^$/ d' {filename}
删除文件中的所有空白行。
以下命令将"1" 替换为"2",以及将"1" 替换为"3",并删除文件中所有尾随的空行:
$ sed '/two/ s/1/2/; /three/ s/1/3/; /^$/ d' sample_one
以下命令将删除文件中所有的行,从第一行直到第一个空行:
sed '1,/^$/ d' {filename}
添加和插入文本
可以结合使用sed 和"a" 选项将文本添加到一个文件的末尾。实现方法如
下:
$ sed '$a\
> This is where we stop\
> the test' sample_one
one 1
two 1
three 1
one 1
two 1
two 1
three 1
This is where we stop
the test
$
在该命令中,美元符号($) 表示文本将被添加到文件的末尾。反斜线(\) 是必
需的,它表示将插入一个回车符。如果它们被遗漏了,则将导致一个错误,显
示该命令是错乱的;在任何要输入回车的地方您必须使用反斜线。
要将这些行添加到第4 和第5 个位置而不是末尾,则命令变为:
$sed '3a\
> This is where we stop\
> the rest' sample_one
one 1
two 1
three 1
This is where we stop
the rest
one 1
two 1
two 1
three 1
和几乎所有的编辑器一样,您可以选择插入而不
是添加(如果您希望这样的话)。这两者的区别是添加跟在指定的行之后,而
插入从指定的行开始。当用插入来代替添加时,只需用"i" 来代替"a",如下
所示:
$sed '3i\
> this is where we stop\
> the rest' sample_one
one 1
two 1
this is where we stop
the rest
three 1
one 1
two 1
two 1
three 1
读写文件
执行替换, 并将1-3 行写到名称为sample_three 的文件中:
$sed '
> /two/ s/1/2/
> /three/ s/1/3/
> 1,3 w sample_three' sample_one
one 1
two 2
three 3
one 1
two 2
two 2
three 3
$cat sample_three
one 1
two 2
three 3
修改命令
除了替换项目之外,还可以将行从一个值修改为另一个值。要记住的是,替换
是对字符逐个进行,而修改功能与删除类似,它影响整行:
$sed '/two/ c\
> we are no longer using two' sample_one
one 1
we are no longer using two
three 1
one 1
we are no longer using two
we are no longer using two
three 1
修改命令与替换的工作方式很相似,但在范围上要更大些—将一个项目完全替
换为另一个项目,而无论字符内容或上下文。夸张一点讲,当使用替换时,只
有字符"1" 被字符"2" 替换,而当使用修改时,原来的整行将被修改。在两种
情况下,要寻找的匹配条件都仅为"two"。
修改全部但……
对于大多数sed 命令,详细说明各种功能要进行何种修改。利用感叹号,可
以在除指定位置之外的任何地方执行修改—与默认的操作完全相反。
例如,要删除包含单词"two" 的所有行,操作为:
$sed '/two/ d' sample_one
one 1
three 1
one 1
three 1
$sed '/two/ !d' sample_one
two 1
two 1
two 1
如果您有一个文件包含一系列项目,并且想对文件中的每个项目执行一个操
作,那么首先对那些项目进行一次智能扫描并考虑将要做什么是很重要的。为
了使事情变得更简单,您可以将sed 与任意迭代例程(for、while、until)结
合来实现这一目的。
比如说,假定您有一个名为"animals" 的文件,其中包含以下项目:
pig
horse
elephant
cow
dog
cat
您希望运行以下例程:
#mcd.ksh
for I in $*
do
echo Old McDonald had a $I
echo E-I, E-I-O
done
结果将为,每一行都显示在"Old McDonald has a" 的末尾。虽然对于这些项
目的大部分这是正确的,但对于"elephant" 项目,它有语法错误,因为结果应
当为"an elephant" 而不是"a elephant"。利用sed,您可以在来自shell 文
件的输出中检查这种语法错误,并通过首先创建一个命令文件来即时地更正它
们:
#sublist
/ a a/ s/ a / an /
/ a e/ s/ a / an /
/a i/ s / a / an /
/a o/ s/ a / an /
/a u/ s/ a / an /
然后执行以下过程:
$ sh mcd.ksh 'cat animals' | sed -f sublist
现在,在运行了mcd 脚本之后,sed 将在输出中搜索单个字母a (空格,
"a",空格)之后紧跟了一个元音的任意位置。如果这种位置存在,它将把该序
列修改为空格,"an",空格。这样就使问题更正后才显示在屏幕上,并确保各
处的编辑人员在晚上可以更容易地入睡。结果是:
Old McDonald had a pig
E-I, E-I-O
Old McDonald had a horse
E-I, E-I-O
Old McDonald had an elephant
E-I, E-I-O
Old McDonald had a cow
E-I, E-I-O
Old McDonald had a dog
E-I, E-I-O
Old McDonald had a cat
E-I, E-I-O
提前退出
sed 默认读取整个文件,并只在到达末尾时才停止。不过,您可以使用退出命
令提前停止处理。只能指定一条退出命令,而处理将一直持续直到满足调用退
出命令的条件。
例如,仅在文件的前五行上执行替换,然后退出:
$ sed '
> /two/ s/1/2/
> /three/ s/1/3/
> 5q' sample_one
one 1
two 2
three 3
one 1
two 2
$
在退出命令之前的项目可以是一个行号(如上所示),或者一条查找/匹配命
令:
$ sed '
> /two/ s/1/2/
> /three/ s/1/3/
> /three/q' sample_one
one 1
two 2
three 3
$
您还可以使用退出命令来查看超过一定标准数目的行,并增加比head 中的功
能更强的功能。例如,head 命令允许您指定您想要查看一个文件的前多少行
—默认数为10,但可以使用从1 到99 的任意一个数字。如果您想查看一个
文件的前110 行,您用head 不能实现这一目的,但用sed 可以:
sed 110q filename //和head进行比较
当使用sed 时,要记住的重要事项是它的工作方式。它的工作方式是:读入
一行,在该行上执行它已知要执行的所有任务,然后继续处理下一行。每一行
都受给定的每一个编辑命令的影响
如果您的操作顺序没有十分彻底地考虑清楚,那么这可能会很麻烦。例如,假
定您需要将所有的"two" 项目修改为"three",然后将所有的"three" 修改为
"four":
$ sed '
> /two/ s/two/three/
> /three/ s/three/four/' sample_one
one 1
four 1
four 1
one 1
four 1
four 1
four 1
$
最初读取的"two" 被修改为"three"。然后它满足为下一次编辑建立的准则,
从而变为"four"。最终的结果不是想要的结果—现在除了"four" 没有别的项目
了,而本来应该有"three" 和"four"。
当执行这种操作时,您必须非常用心地注意指定操作的方式,并按某种顺序来
安排它们,使得操作之间不会互相影响。例如:
$ sed '
> /three/ s/three/four/
> /two/ s/two/three/' sample_one
one 1
three 1
four 1
one 1
three 1
three 1
four 1
$
这非常有效,因为"three" 值在"two" 变成"three" 之前得到修改。
sed [options] '{command}' [filename]
替换命令
's/{old value}/{new value}/'
例子
$echo The tiger cubs will meet on Tuesday afer school | sed 's/tiger/wolf/'
The wolf cubs will meet on Tuesday afer school
$echo The tiger cubs will meet on Tuesday after school | sed -e 's/tiger/wolf/' -e 's/after/befor/'
The wolf cubs will meet on Tuesday befor school
$echo The tiger cubs will meet on Tuesday after scholl | sed 's/tiger/wolf/;s/after/before/'
The wolf cubs will meet on Tuesday before scholl
$echo The tiger cubs will meet on Tuesday after school | sed '
> s/tiger/wolf/
> s/after/before/
> '
The wolf cubs will meet on Tuesday before school
$echo The tiger cubs will meet this Tuesday at the same time as the meeting last Tuesday |sed 's/Tuesday/Thursday/'
The tiger cubs will meet this Thursday at the same time as the meeting last Tuesday
$echo The tiger cubs will meet this Tuesday at the same time as meeting last Tuesday | sed 's/Tuesday/Thursday/g'
The tiger cubs will meet this Thursday at the same time as meeting last Thursday
sed 可以用来将任意的可打印字符修改为任意其它
的可打印字符。如果您想将不可打印字符修改为可打印字符—例如,铃铛修改
为单词"bell"—sed 不是适于完成这项工作的工具(但tr 是)。
$cat sample_one
one 1
two 1
three 1
one 1
two 1
two 1
three 1
模式匹配
$sed '/two/ s/1/2/' sample_one
one 1
two 2
three 1
one 1
two 2
two 2
three 1
$sed '
> /two/ s/1/2/
> /three/ s/1/3/' sample_one
one 1
two 2
three 3
one 1
two 2
two 2
three 3
$sed '
> /two/ s/1/2/
> /three/ s/1/3/' sample_one > sample_two
$cat sample_two
one 1
two 2
three 3
one 1
two 2
two 2
three 3
$
使用sed脚本文件
$cat sedlist
/two/ s/1/2/
/three/ s/1/3/
$sed -f sedlist sample_one
one 1
two 2
three 3
one 1
two 2
two 2
three 3
行匹配
$sed '5,6 s/1/2/' sample_one
one 1
two 1
three 1
one 1
two 2
two 2
three 1
禁止显示 -n
$sed -n -f sedlist sample_one
$
显示选项 -p
$cat sedlist
/two/ s/1/2/p
/three/ s/1/3/p
$sed -n -f sedlist sample_one
two 2
three 3
two 2
two 2
three 3
$
$sed -n -f sedlist sample_one > sample_two
$cat sample_two
two 2
three 3
two 2
two 2
three 3
只显示第三行到第五行
$sed -n '3,5p' sample_one
three 1
one 1
two 1
$
删除行
删除匹配行
$sed '/two/ d' sample_one
one 1
three 1
one 1
three 1
删除指定行(第一到第三行)
$sed '1,3 d' sample_one
one 1
two 1
two 1
three 1
对于流编辑器,一般当它们涉及
到全局表达式时,特别是应用于删除操作时,有几点要记住:
上三角号(^) 表示一行的开始,因此,如果"two" 是该行的头三个字符,则
sed '/^two/ d' sample_one
将只删除该行。
美元符号($) 代表文件的结尾,或一行的结尾,因此,如果"two" 是该行的最
后三个字符,则
sed '/two$/ d' sample_one
将只删除该行。
sed '/^$/ d' {filename}
删除文件中的所有空白行。
以下命令将"1" 替换为"2",以及将"1" 替换为"3",并删除文件中所有尾随的空行:
$ sed '/two/ s/1/2/; /three/ s/1/3/; /^$/ d' sample_one
以下命令将删除文件中所有的行,从第一行直到第一个空行:
sed '1,/^$/ d' {filename}
添加和插入文本
可以结合使用sed 和"a" 选项将文本添加到一个文件的末尾。实现方法如
下:
$ sed '$a\
> This is where we stop\
> the test' sample_one
one 1
two 1
three 1
one 1
two 1
two 1
three 1
This is where we stop
the test
$
在该命令中,美元符号($) 表示文本将被添加到文件的末尾。反斜线(\) 是必
需的,它表示将插入一个回车符。如果它们被遗漏了,则将导致一个错误,显
示该命令是错乱的;在任何要输入回车的地方您必须使用反斜线。
要将这些行添加到第4 和第5 个位置而不是末尾,则命令变为:
$sed '3a\
> This is where we stop\
> the rest' sample_one
one 1
two 1
three 1
This is where we stop
the rest
one 1
two 1
two 1
three 1
和几乎所有的编辑器一样,您可以选择插入而不
是添加(如果您希望这样的话)。这两者的区别是添加跟在指定的行之后,而
插入从指定的行开始。当用插入来代替添加时,只需用"i" 来代替"a",如下
所示:
$sed '3i\
> this is where we stop\
> the rest' sample_one
one 1
two 1
this is where we stop
the rest
three 1
one 1
two 1
two 1
three 1
读写文件
执行替换, 并将1-3 行写到名称为sample_three 的文件中:
$sed '
> /two/ s/1/2/
> /three/ s/1/3/
> 1,3 w sample_three' sample_one
one 1
two 2
three 3
one 1
two 2
two 2
three 3
$cat sample_three
one 1
two 2
three 3
修改命令
除了替换项目之外,还可以将行从一个值修改为另一个值。要记住的是,替换
是对字符逐个进行,而修改功能与删除类似,它影响整行:
$sed '/two/ c\
> we are no longer using two' sample_one
one 1
we are no longer using two
three 1
one 1
we are no longer using two
we are no longer using two
three 1
修改命令与替换的工作方式很相似,但在范围上要更大些—将一个项目完全替
换为另一个项目,而无论字符内容或上下文。夸张一点讲,当使用替换时,只
有字符"1" 被字符"2" 替换,而当使用修改时,原来的整行将被修改。在两种
情况下,要寻找的匹配条件都仅为"two"。
修改全部但……
对于大多数sed 命令,详细说明各种功能要进行何种修改。利用感叹号,可
以在除指定位置之外的任何地方执行修改—与默认的操作完全相反。
例如,要删除包含单词"two" 的所有行,操作为:
$sed '/two/ d' sample_one
one 1
three 1
one 1
three 1
$sed '/two/ !d' sample_one
two 1
two 1
two 1
如果您有一个文件包含一系列项目,并且想对文件中的每个项目执行一个操
作,那么首先对那些项目进行一次智能扫描并考虑将要做什么是很重要的。为
了使事情变得更简单,您可以将sed 与任意迭代例程(for、while、until)结
合来实现这一目的。
比如说,假定您有一个名为"animals" 的文件,其中包含以下项目:
pig
horse
elephant
cow
dog
cat
您希望运行以下例程:
#mcd.ksh
for I in $*
do
echo Old McDonald had a $I
echo E-I, E-I-O
done
结果将为,每一行都显示在"Old McDonald has a" 的末尾。虽然对于这些项
目的大部分这是正确的,但对于"elephant" 项目,它有语法错误,因为结果应
当为"an elephant" 而不是"a elephant"。利用sed,您可以在来自shell 文
件的输出中检查这种语法错误,并通过首先创建一个命令文件来即时地更正它
们:
#sublist
/ a a/ s/ a / an /
/ a e/ s/ a / an /
/a i/ s / a / an /
/a o/ s/ a / an /
/a u/ s/ a / an /
然后执行以下过程:
$ sh mcd.ksh 'cat animals' | sed -f sublist
现在,在运行了mcd 脚本之后,sed 将在输出中搜索单个字母a (空格,
"a",空格)之后紧跟了一个元音的任意位置。如果这种位置存在,它将把该序
列修改为空格,"an",空格。这样就使问题更正后才显示在屏幕上,并确保各
处的编辑人员在晚上可以更容易地入睡。结果是:
Old McDonald had a pig
E-I, E-I-O
Old McDonald had a horse
E-I, E-I-O
Old McDonald had an elephant
E-I, E-I-O
Old McDonald had a cow
E-I, E-I-O
Old McDonald had a dog
E-I, E-I-O
Old McDonald had a cat
E-I, E-I-O
提前退出
sed 默认读取整个文件,并只在到达末尾时才停止。不过,您可以使用退出命
令提前停止处理。只能指定一条退出命令,而处理将一直持续直到满足调用退
出命令的条件。
例如,仅在文件的前五行上执行替换,然后退出:
$ sed '
> /two/ s/1/2/
> /three/ s/1/3/
> 5q' sample_one
one 1
two 2
three 3
one 1
two 2
$
在退出命令之前的项目可以是一个行号(如上所示),或者一条查找/匹配命
令:
$ sed '
> /two/ s/1/2/
> /three/ s/1/3/
> /three/q' sample_one
one 1
two 2
three 3
$
您还可以使用退出命令来查看超过一定标准数目的行,并增加比head 中的功
能更强的功能。例如,head 命令允许您指定您想要查看一个文件的前多少行
—默认数为10,但可以使用从1 到99 的任意一个数字。如果您想查看一个
文件的前110 行,您用head 不能实现这一目的,但用sed 可以:
sed 110q filename //和head进行比较
当使用sed 时,要记住的重要事项是它的工作方式。它的工作方式是:读入
一行,在该行上执行它已知要执行的所有任务,然后继续处理下一行。每一行
都受给定的每一个编辑命令的影响
如果您的操作顺序没有十分彻底地考虑清楚,那么这可能会很麻烦。例如,假
定您需要将所有的"two" 项目修改为"three",然后将所有的"three" 修改为
"four":
$ sed '
> /two/ s/two/three/
> /three/ s/three/four/' sample_one
one 1
four 1
four 1
one 1
four 1
four 1
four 1
$
最初读取的"two" 被修改为"three"。然后它满足为下一次编辑建立的准则,
从而变为"four"。最终的结果不是想要的结果—现在除了"four" 没有别的项目
了,而本来应该有"three" 和"four"。
当执行这种操作时,您必须非常用心地注意指定操作的方式,并按某种顺序来
安排它们,使得操作之间不会互相影响。例如:
$ sed '
> /three/ s/three/four/
> /two/ s/two/three/' sample_one
one 1
three 1
four 1
one 1
three 1
three 1
four 1
$
这非常有效,因为"three" 值在"two" 变成"three" 之前得到修改。