介绍
sed 是一种流式文本编辑器,常用于在 Unix 和类 Unix 系统中对文本进行处理。它可以将文本从标准输入或文件中读取,对其进行修改,然后将修改后的文本输出到标准输出或文件中。sed 是 “stream editor” 的缩写。
语法
sed 的基本语法为:
sed [options] 'command' [file...]
command 是要执行的编辑命令,file 是要处理的文件名(可以是多个文件,也可以使用通配符指定一组文件),而 options 则控制 sed 的行为。
选项
sed 常用的一些选项
-n
:只输出修改后的文本,不输出原始文本。-e
:指定要执行的多个编辑命令。-f
:指定包含要执行的编辑命令的文件名。-i
:直接在文件中进行修改,而不是将修改后的文本输出到标准输出中。-r
:启用正则表达式扩展语法。
命令
sed 的编辑命令可以使用单引号或双引号括起来,ed 常用的一些编辑命令:
s/pattern/replacement/
:将匹配 pattern 的文本替换为 replacement。d
:删除匹配的行。p
:打印匹配的行。a text
:在匹配行的后面添加一行文本。i text
:在匹配行的前面添加一行文本。c text
:用指定的文本替换匹配的行。y/chars1/chars2/
:将 chars1 中的每个字符替换为 chars2 中相应位置的字符。
示例
我们先准备一个文件,名为test
做测试,内容如下:
➜ cat test
HELLO LINUX!
Linux is a free unix-type opterating system.
This is a linux testfile!
Linux test
增加内容
使用命令sed -e 3a\newLine testfile
这个命令的意思就是,在第三行后面追加newLine
这么一行字符,字符前面要用反斜线作区分。执行完毕之后可以看到结果:
➜ sed -e 3a\newline test
HELLO LINUX!
Linux is a free unix-type opterating system.
This is a linux testfile!
newline
Linux test
但是注意,这个只是将文字处理了,没有写入到文件里,文件里还是之前的内容。
其实 a 前面是可以匹配字符串,比如我们只想在出现 Linux 的行后面追加,就可以:sed -e /Linux/a\newline test
两个斜线之间的内容是需要匹配的内容。可以看出,只有第二、第四行有Linux
,所以结果如下:
➜ sed -e /Linux/a\newline test
HELLO LINUX!
Linux is a free unix-type opterating system.
newline
This is a linux testfile!
Linux test
newline
这里用双引号把整个表达式括起来也可以,还方便处理带空格的字符。
sed -e /Linux/a\newline test
等效于sed "/Linux/a newline" test
插入内容
跟 a
类似,sed 3i\newline test
是在第三行前面插入newline
:
➜ sed 3i\newline test
HELLO LINUX!
Linux is a free unix-type opterating system.
newline
This is a linux testfile!
Linux test
sed /Linux/i\newline test
是在所有匹配到Linux
的行前面插入:
➜ sed /Linux/i\newline test
HELLO LINUX!
newline
Linux is a free unix-type opterating system.
This is a linux testfile!
newline
Linux test
可以看出插入的用法和增加很相似。
删除
删除的字符是d
,用法跟前面也很相似,就不赘述,例子如下:
➜ sed '/Linux/d' test
HELLO LINUX!
This is a linux testfile!
可以看到删除了匹配到的两行。
替换
替换也是一样,字符是c
。举个栗子:
➜ sed '/Linux/c\Windows' test
HELLO LINUX!
Windows
This is a linux testfile!
Windows
替换还有个字符是 s
,但是用法由不太一样了,最常见的用法:sed 's/old/new/g'
其中old
代表想要匹配的字符,new
是想要替换的字符,比如:
➜ sed 's/Linux/Windows/g' test
HELLO LINUX!
Windows is a free unix-type opterating system.
This is a linux testfile!
Windows test
这里的/g
的意思是一行中的每一次匹配,因为一行中可能匹配到很多次。我们拿一个新的文本文件做例子:
➜ cat test2
aaaaaaaaaaa
bbbbbabbbbb
cccccaacccc
假设我们想把一行中的第三次及以后出现的a
变成大写A
,那应该这么写:
➜ sed 's/a/A/3g' test2
aaAAAAAAAAA
bbbbbabbbbb
cccccaacccc
可以看出只有第一行的有的改了,因为第二第三行没有这么多a
出现。
关于s
还有很多用法,还是回到第一个文件,比如可以用/^/
和/$/
分别代表行首和行尾:
➜ sed 's/^/###/g' test
###HELLO LINUX!
###Linux is a free unix-type opterating system.
###This is a linux testfile!
###Linux test
➜ sed 's/$/---/g' test
HELLO LINUX! ---
Linux is a free unix-type opterating system. ---
This is a linux testfile! ---
Linux test ---
这个其实就是正则表达式的语法,其他类似语法还有:
^
表示一行的开头。如:/^#/
以#开头的匹配。$
表示一行的结尾。如:/}$/
以}结尾的匹配。\<
表示词首。 如:`\ 表示以 abc 为首的詞。\>
表示词尾。 如:abc\>
表示以 abc 結尾的詞。.
表示任何单个字符。*
表示某个字符出现了0次或多次。[ ]
字符集合。 如:[abc]
表示匹配a或b或c,还有[a-zA-Z]
表示匹配所有的26个字符。如果其中有^表示反,如[^a]
表示非a的字符
以上的所有用法,还可以在字符前面增加行号或者匹配。什么意思呐?比如你想在第一和第二行后面增加一行内容newline
,就是:
➜ sed '1,2a\newline' test
HELLO LINUX!
newline
Linux is a free unix-type opterating system.
newline
This is a linux testfile!
Linux test
其他操作同理。不止可以用数字来限定范围,还可以用匹配来限定,只需要用//
括起来:
➜ sed '/LINUX/,/linux/i\test' test
test
HELLO LINUX!
test
Linux is a free unix-type opterating system.
test
This is a linux testfile!
Linux test
这里的意思是,从匹配到LINUX
的那一行,到匹配到linux
的那一行,也就是 123 这三行
,都做插入操作。
多个匹配
用-e
命令可以执行多次匹配,相当于顺序依次执行两个sed
命令:
➜ sed -e 's/Linux/Windows/g' -e 's/Windows/Mac OS/g' test
HELLO LINUX!
Mac OS is a free unix-type opterating system.
This is a linux testfile!
Mac OS test
这个命令其实就是先把Linux
替换成Windows
,再把Windows
替换成Mac OS
。
写入文件
上面介绍的所有文件操作都支持在缓存中处理然后打印到控制台,实际上没有对文件修改。想要保存到原文件的话可以用> file
或者-i
来保存到文件