sed是一种流编辑器,它是文本处理中非常有用的工具,能够完美的配合正则表达式使用,功能不同凡响。处理时,把当前处理的行存储在临时缓冲区中,称为『模式空间』(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。sed主要用来自动编辑一个或多个文件,简化对文件的反复操作,编写转换程序等。
sed的选项、命令、替换标记
sed [options] 'command' file(s)sed [options] -f scriptfile file(s)
![e982004cfc2dc941d70d29d71f8f30ce.png](https://i-blog.csdnimg.cn/blog_migrate/762a9edf111f0fc2bde3e887be9ef513.jpeg)
文件:指定待处理的文本文件列表
sed命令
![1f1deb0b2f2fe4eea0f4e6a63b518f3e.png](https://i-blog.csdnimg.cn/blog_migrate/a55b75b95c60c1de61c027deb6a70638.jpeg)
![cb4d8782901e40c20a9044dfb4f0fffb.png](https://i-blog.csdnimg.cn/blog_migrate/8267fc830331d7e8045542a345ee6048.jpeg)
sed替换标记
![a6e80be98bbac0405c5d14524809016b.png](https://i-blog.csdnimg.cn/blog_migrate/f3c1f7c972b7e0a1de91ebec8aaa908b.jpeg)
sed元字符集
![29e2f6ebe3f77c12bfc5c4e5d17de344.png](https://i-blog.csdnimg.cn/blog_migrate/5d06a4817b01ab9e5e5c0ccce6b91c09.jpeg)
sed用法实例
我们先准备一个测试文件
MacBook-Pro:tmp maxincai$ cat test.txtmy cat's name is bettyThis is your dogmy dog's name is frankThis is your fishmy fish's name is georgeThis is your goatmy goat's name is adam
替换操作:s命令
替换文本中的字符串:
MacBook-Pro:tmp maxincai$ sed 's/This/aaa/' test.txtmy cat's name is bettyaaa is your dogmy dog's name is frankaaa is your fishmy fish's name is georgeaaa is your goatmy goat's name is adam
-n选项和p命令一起使用表示只打印那些发生替换的行:
MacBook-Pro:tmp maxincai$ sed -n 's/This/aaa/p' test.txtaaa is your dogaaa is your fishaaa is your goat
测试过程中发现mac os x和linux还是有点不一样,换回centos 6.5进行测试
直接编辑文件选项-i,会匹配test.txt文件中每一行的第一个This替换为this:
[root@vagrant-centos65 workspace]# sed -i 's/This/this/' test.txt[root@vagrant-centos65 workspace]# cat test.txtmy cat's name is bettythis is your dogmy dog's name is frankthis is your fishmy fish's name is georgethis is your goatmy goat's name is adam
全面替换标记g
使用后缀/g标记会替换每一行中的所有匹配:
[root@vagrant-centos65 workspace]# sed 's/this/This/g' test.txtmy cat's name is bettyThis is your This dogmy dog's name is This frankThis is your fishmy fish's name is This georgeThis is your goatmy goat's name is This adam
当需要从第N处匹配开始替换时,可以使用/Ng:
[root@vagrant-centos65 workspace]# echo sksksksksksk | sed 's/sk/SK/2g'skSKSKSKSKSK[root@vagrant-centos65 workspace]# echo sksksksksksk | sed 's/sk/SK/3g'skskSKSKSKSK[root@vagrant-centos65 workspace]# echo sksksksksksk | sed 's/sk/SK/4g'skskskSKSKSK
定界符
以上命令中字符 / 在sed中作为定界符使用,也可以使用任意的定界符:
[root@vagrant-centos65 workspace]# echo sksksksksksk | sed 's:sk:SK:4g'skskskSKSKSK[root@vagrant-centos65 workspace]# echo sksksksksksk | sed 's|sk|SK|4g'skskskSKSKSK
定界符出现在样式内部时,需要进行转义:
[root@vagrant-centos65 workspace]# echo '/usr/local/bin' | sed 's//usr/local/bin//USR/LOCAL/BIN/g'/USR/LOCAL/BIN
删除操作:d命令
删除空白行:
[root@vagrant-centos65 workspace]# cat test.txtmy cat's name is bettythis is your this dogmy dog's name is this frankthis is your fishmy fish's name is this georgethis is your goatmy goat's name is this adam[root@vagrant-centos65 workspace]# sed '/^$/d' test.txtmy cat's name is bettythis is your this dogmy dog's name is this frankthis is your fishmy fish's name is this georgethis is your goatmy goat's name is this adam
删除文件的第2行:
[root@vagrant-centos65 workspace]# sed '2d' test.txtmy cat's name is bettymy dog's name is this frankthis is your fishmy fish's name is this georgethis is your goatmy goat's name is this adam
删除文件的第2行到末尾所有行:
[root@vagrant-centos65 workspace]# sed '2,$d' test.txtmy cat's name is betty
删除文件最后一行:
[root@vagrant-centos65 workspace]# sed '$d' test.txtmy cat's name is bettythis is your this dogmy dog's name is this frankthis is your fishmy fish's name is this georgethis is your goat
删除文件中所有以my开头的行:
[root@vagrant-centos65 workspace]# sed '/^my/'d test.txtthis is your this dogthis is your fishthis is your goat
已匹配字符串标记&
正则表达式w+匹配每一个单词,使用[&]替换它,&对应之前所匹配到的单词:
[root@vagrant-centos65 workspace]# echo this is a test line | sed 's/w+/[&]/g'[this] [is] [a] [test] [line]
子串匹配标记1
匹配给定样式的其中一部份:
[root@vagrant-centos65 workspace]# 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@vagrant-centos65 workspace]# echo aaa BBB | sed 's/([a-z]+) ([A-Z]+)/2 1/'BBB aaa
组合多个表达式
sed '表达式' | sed '表达式'
等价于
sed '表达式; 表达式'
引用
sed表达式可以使用单引号来引用,但是如果表达式内部包含变量字符串,就需要使用双引号。
[root@vagrant-centos65 workspace]# test=hello[root@vagrant-centos65 workspace]# echo hello WORLD | sed "s/$test/HELLO/"HELLO WORLD
选定行的范围:,(逗号)
打印从第5行开始到第一个包含以this开始的行之间的所有行:
[root@vagrant-centos65 workspace]# sed -n '5,/^this/p' test.txtmy fish's name is this georgethis is your goat
多点编辑:e命令
-e选项允许在同一行里执行多条命令:
[root@vagrant-centos65 workspace]# sed -e '1,5d' -e 's/my/MY/' test.txtthis is your goatMY goat's name is this adam
上面sed表达式的第一条命令删除1至5行,第二条命令用check替换test。命令的执行顺序对结果有影响。如果两个命令都是替换命令,那么第一个命令将影响第二个命令的结果。
和 -e 等价的命令是 --expression
从文件读入:r命令
file里的内容被读进来,显示在与test匹配的行后面,如果匹配多行,则file的内容将显示在所有匹配行的下面:
[root@vagrant-centos65 workspace]# cat test1.txtaaaaaaaa[root@vagrant-centos65 workspace]# sed '/my/r test1.txt' test.txtmy cat's name is bettyaaaaaaaathis is your this dogmy dog's name is this frankaaaaaaaathis is your fishmy fish's name is this georgeaaaaaaaathis is your goatmy goat's name is this adamaaaaaaaa
写入文件:w命令
在test.txt中所有包含my的行都被写入test2.txt里:
[root@vagrant-centos65 workspace]# sed -n '/my/w test2.txt' test.txt[root@vagrant-centos65 workspace]# cat test2.txtmy cat's name is bettymy dog's name is this frankmy fish's name is this georgemy goat's name is this adam
追加(行下):a命令
将this is a test line 追加到以my开头的行后面:
[root@vagrant-centos65 workspace]# sed '/^my/ahis is a test line' test.txtmy cat's name is bettythis is a test linethis is your this dogmy dog's name is this frankthis is a test linethis is your fishmy fish's name is this georgethis is a test linethis is your goatmy goat's name is this adamthis is a test line
在text.txt文件第2行之后插入this is a test line:
[root@vagrant-centos65 workspace]# sed '2ahis is a test line' test.txtmy cat's name is bettythis is your this dogthis is a test linemy dog's name is this frankthis is your fishmy fish's name is this georgethis is your goatmy goat's name is this adam
插入(行上):i命令
将this is a test line 插入到以my开头的行前面:
[root@vagrant-centos65 workspace]# sed '/^my/ihis is a test line' test.txtthis is a test linemy cat's name is bettythis is your this dogthis is a test linemy dog's name is this frankthis is your fishthis is a test linemy fish's name is this georgethis is your goatthis is a test linemy goat's name is this adam
下一个:n命令
如果my被匹配,则移动到匹配行的下一行,替换这一行的this为This,并打印该行:
[root@vagrant-centos65 workspace]# sed '/my/{n; s/this/This/; }' test.txtmy cat's name is bettyThis is your this dogmy dog's name is this frankThis is your fishmy fish's name is this georgeThis is your goatmy goat's name is this adam
变形:y命令
把1-10行内所有的abcde转变为大写,注意,正则表达式元字符不能使用这个命令:
[root@vagrant-centos65 workspace]# sed '1,10y/abcde/ABCDE/' test.txtmy CAt's nAmE is BEttythis is your this Dogmy Dog's nAmE is this frAnkthis is your fishmy fish's nAmE is this gEorgEthis is your goAtmy goAt's nAmE is this ADAm
退出:q命令
打印完第3行,退出sed
[root@vagrant-centos65 workspace]# sed '3q' test.txtmy cat's name is bettythis is your this dogmy dog's name is this frank
打印奇数行或偶数行
方法1:
奇数行
[root@vagrant-centos65 workspace]# sed -n 'p;n' test.txtmy cat's name is bettymy dog's name is this frankmy fish's name is this georgemy goat's name is this adam
偶数行
[root@vagrant-centos65 workspace]# sed -n 'n;p' test.txtthis is your this dogthis is your fishthis is your goat
方法2:
sed -n '1~2p' test.txtsed -n '2~2p' test.txt
更多的需要在以后的工作中慢慢摸索,这里只是一个简单的记录,以后如果有更多经验了再完善一篇sed实战吧。
专注于技术热点大数据,人工智能,JAVA、Python、 C 、GO、Javascript等语言最新前言技术,及业务痛点问题分析,请关注【编程我最懂】共同交流学习。