我们首先准备了一个测试文件
[root@ localhost ~]# cat text.txt
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
1.s 替换
讲文中的This 替换成 cyy
[root@ localhost ~]# sed 's/This/cyy/' text.txt
my cat's name is betty
cyy is your dog
my dog's name is frank
cyy is your fish
my fish's name is george
cyy is your goat
my goat's name is adam
在替换的时候如果加入-i 选项就会真的替换 但是只是会替换每一行的第一个
例如
[root@ localhost ~]# sed -i 's/This/cyy/' text.txt
[root@ localhost ~]# cat text.txt
my cat's name is betty
cyy is your dog
my dog's name is frank
cyy is your fish
my fish's name is george
cyy is your goat
my goat's name is adam
-n 和 -p 一起使用表示试打印那些发生替换的行
加入-g的参数是指全部替换,全局的一个参数
[root@ localhost ~]# sed 's/i/I/g' text.txt
my cat's name Is betty
cyy Is your dog
my dog's name Is frank
cyy Is your fIsh
my fIsh's name Is george
cyy Is your goat
my goat's name Is adam
当需要从第N处替换的时候,可以加入Ng:这里的N是具体的数
[root@ localhost ~]# echo 01010101010101 | sed 's/01/99/2g'
01999999999999
注意:在以上的命令种的字符 / 在sed中作为定界符使用,当然也可以使用任意的定界符
[root@ localhost ~]# echo 01010101010101 | sed 's:01:99:2g'
01999999999999
2. 删除操作
可以先修改要测试的文件,在这个文件中添加空白行
[root@ localhost ~]# vim text.txt
[root@ localhost ~]# cat text.txt
my cat's name is betty
cyy is your dog
my dog's name is frank
cyy is your fish
my fish's name is george
cyy is your goat
my goat's name is adam
删除所有的空白行
[root@ localhost ~]# sed '/^$/d' text.txt
my cat's name is betty
cyy is your dog
my dog's name is frank
cyy is your fish
my fish's name is george
cyy is your goat
my goat's name is adam
删除文件中的第二行
[root@ localhost ~]# sed '2d' text.txt
my cat's name is betty
cyy is your dog
my dog's name is frank
cyy is your fish
my fish's name is george
cyy is your goat
my goat's name is adam
删除文件的第2行至末尾的所有行
sed '2,$d' text.txt
my cat's name is betty
删除文件中的最后一行
[root@ localhost ~]# sed '$d' text.txt
my cat's name is betty
cyy is your dog
my dog's name is frank
cyy is your fish
my fish's name is george
cyy is your goat
删除文件中以my开头的行
[root@ localhost ~]# sed '/^my/d' text.txt
cyy is your dog
cyy is your fish
cyy is your goat
3. 已匹配字符串标记&
正则表达式\w+匹配每一个单词,使用[&]替换他 ,&对应之前所匹配到的单词
[root@ localhost ~]# echo this is a test lie | sed 's/\w\+/[&]/g'
[this] [is] [a] [test] [lie]
字符串匹配标记\1
[root@ localhost ~]# echo this is digit 7 in a number | sed 's/digit \([0-9]\)/\1/'
this is 7 in a number
注意:命令中digit 7 被替换程7 样式匹配到的子串是7,(…)用于匹配字串,对于匹配到的第一个子串标记为\1 以此类推匹配到的第二个结果就是\2:例如
[root@ localhost ~]# echo aaa BBB | sed 's/\([a-z]\+\) \([A-Z]\+\)/\2 \1/'
BBB aaa
组合多个表达式:sed ‘表达式’ | sed ‘表达式’
注意:sed表达式可以使用单引号来引用,如果表达式内部包含变量字符串,就需要使用双引号
例子:首先定义一个变量 然后将这个变量替换程大写
[root@ localhost ~]# test=hello
[root@ localhost ~]# echo hello WORLD | sed "s/$test/HELLO/"
HELLO WORLD
选定行的范围
首先是逗号
答应从第5行开始到第一个包含以this开始的行之间的所有行
[root@ localhost ~]# sed -n '5,/^This/p' text.txt
my dog's name is frank
cyy is your fish
my fish's name is george
cyy is your goat
my goat's name is adam
多点编辑: e命令
-e选项允许在同一行里执行多条命令 和-e等价的命令是 -expression
[root@ localhost ~]# sed -e '1,5d' -e 's/my/MY/' text.txt
cyy is your fish
MY fish's name is george
cyy is your goat
MY goat's name is adam
注意事项:上面sed表达式的第一条命令删除1-5行,第二条命令使用check替换test。命令的执行顺序对结果又影响。如果两个命令都是替换命令,那么第一个命令将影响第二个命令的结果。
4. 文件读入
-r 命令
file里的内容被读进来。显示在与test匹配的行后面。如果匹配多行,这file的内容将显示在所有匹配行的下面
首先先编辑一个文件
[root@ localhost ~]# cat cyy.txt
aaaaaaaaaaaaaaa
[root@ localhost ~]# sed '/my/r cyy.txt' text.txt
my cat's name is betty
aaaaaaaaaaaaaaa
cyy is your dog
my dog's name is frank
aaaaaaaaaaaaaaa
cyy is your fish
my fish's name is george
aaaaaaaaaaaaaaa
cyy is your goat
my goat's name is adam
aaaaaaaaaaaaaaa
写入文件 -w
在text.txt 中所有 包含my的行都被写入cyy.txt中
[root@ localhost ~]# sed -n '/my/w text.txt' cyy.txt
[root@ localhost ~]# cat cyy.txt
my cat's name is betty
my dog's name is frank
my fish's name is george
my goat's name is adam
追加(指定行的下面) -a
将this is a test line 追加到以my开头的行的后面
[root@ localhost ~]# sed '/^my/a\this is a test line' text.txt
my cat's name is betty
this is a test line
my dog's name is frank
this is a test line
my fish's name is george
this is a test line
my goat's name is adam
this is a test line
在 text.txt 文件中的第二行后插入this is a test line
[root@ localhost ~]# sed '2a\this is a test lina' text.txt
my cat's name is betty
my dog's name is frank
this is a test lina
my fish's name is george
my goat's name is adam
插入(指定行的上面) -i
将this is a test line 插入到以my开头的行前面
[root@ localhost ~]# sed '/^my/i\this is a test lina' text.txt
this is a test lina
my cat's name is betty
this is a test lina
my dog's name is frank
this is a test lina
my fish's name is george
this is a test lina
my goat's name is adam
下一个 n命令
如果my被匹配,则移动到匹配行的下一行,替换这一行的this为This,并打印该行:
[root@ localhost ~]# sed '/my/{n; s/this/This/;}' text.txt
my cat's name is betty
my dog's name is frank
my fish's name is george
my goat's name is adam
变形 : y 命令
把1-4行内所有的abcde 转变为大写,注意,正则表达式元字符不能使用这个命令:
[root@ localhost ~]# sed '1,4y/abcde/ABCED/' text.txt
my CAt's nAmD is BDtty
my Eog's nAmD is frAnk
my fish's nAmD is gDorgD
my goAt's nAmD is AEAm
推出命令: q
打印完第3行,退出sed
[root@ localhost ~]# sed '3q' text.txt
my cat's name is betty
my dog's name is frank
my fish's name is george
打印奇数行或偶数行
奇数行
[root@ localhost ~]# sed -n 'p;n' text.txt
my cat's name is betty
my fish's name is george
偶数行
[root@ localhost ~]# sed -n 'n;p' text.txt
my dog's name is frank
my goat's name is adam