一句话总结:以行为单位的文本编辑工具,熟知常用命令及差别,不推荐直接编辑原文本。
1、条件是双引号,与awk的单引号区分开
2、^表示行首,$表示行尾;1表示第一行,$表示最后一行
3、命令字c替换的是行,s是替换指定字符串;d是删除一行,删除某个字符串直接用s即可
zjy@ubuntu:~$ echo "like love" | sed "s/like//"
love
4、命令字i是插入到行前,命令字a与其相反,是追加在行后
5、命令字p是打印,会把待处理的信息也输出,所以打印出的都是双份,一般加上-n仅显示script处理后的结果
zjy@ubuntu:~$ echo "like love" | sed p
like love
like love
zjy@ubuntu:~$ echo "like love" | sed p -n
like love
zjy@ubuntu:~$
基本工作方式: sed [-nef] '[动作]' [输入文本]
-n : 安静模式, 一般sed用法中, 来自stdin的数据一般会被列出到屏幕上, 如果使用-n参数后, 只有经过sed处理的那一行被列出来.
-e : 多重编辑, 比如你同时又想删除某行, 又想改变其他行, 那么可以用 sed -e '1,5d' -e 's/abc/xxx/g' filename
-f : 首先将 sed的动作写在一个档案内, 然后通过 sed -f scriptfile 就可以直接执行 scriptfile 内的sed动作
-i : 直接编辑, 这回就是真的改变文件中的内容了, 别的都只是改变显示. (改了文件回不去了,不推荐使用)
动作:
a 新增, a 后面可以接字符串, 而这个字符串会在新的一行出现. (下一行)
c 取代, c 后面的字符串, 这些字符串可以取代 n1,n2之间的行
d 删除, 后面不接任何东西
i 插入, 后面的字符串, 会在上一行出现
p 打印, 将选择的资料列出, 通常和 sed -n 一起运作 sed -n '3p' 只打印第3行
s 取代, 类似vi中的取代, 1,20s/old/new/g
[line-address]q 退出, 匹配到某行退出, 提高效率
[line-address]r 匹配到的行读取某文件 例如: sed '1r qqq' abc , 注意, 写入的文本是写在了第1行的后边, 也就是第2行
[line-address]w file, 匹配到的行写入某文件 例如: sed -n '/m/w qqq' abc , 从abc中读取带m的行写到qqq文件中, 注意, 这个写入带有覆盖性.
sed命令定义,摘自:http://www.runoob.com/linux/linux-comm-sed.html
Linux sed命令是利用script来处理文本文件。
sed可依照script的指令,来处理、编辑文本文件。
Sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。
语法
sed [-hnV][-e<script>][-f<script文件>][文本文件]
参数说明:
- -e<script>或--expression=<script> 以选项中指定的script来处理输入的文本文件。
- -f<script文件>或--file=<script文件> 以选项中指定的script文件来处理输入的文本文件。
- -h或--help 显示帮助。
- -n或--quiet或--silent 仅显示script处理后的结果。
- -V或--version 显示版本信息。
动作说明:
- a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
- c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
- d :删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
- i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
- p :列印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
- s :取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g 就是
下面文章不错,就不截图重写了,基本都是这个样子。转载自:http://blog.csdn.net/stpeace/article/details/46730991
sed是stream editor, 也就是流编辑器, 实际上就是linux中的一个命令, 作用很强大。 搞linux开发的人, 不能不熟悉该命令, 否则, 何以立足于江湖呢? 在本文中, 我们来一起玩玩sed, 简要介绍其用法。
因为sed很强大, 所以难以一次介绍所有的内容。 以后遇到新的东东, 再往本博文中添加。 现在, 能学一点就是一点。 还是老规矩, 在理解的基础上, 大量练习和实践。以实践操作为荣, 以只看不练为耻。
在本文中,我们的文件为taoge.txt, 其中的内容为(注意, 在本文中, 我们不改变这个文件中的内容):
- My favorite fruit is watermelon
- I like watermelon so much
- My favorite subject is math
- I like math so much
- My favorite pet is dog
- I like dog so much
-
- My English name is Eric
- But you can also call me taoge if you like
现在要输出taoge.txt的第2-3行, 该怎么做呢? 我们可能会这么搞:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ cat taoge.txt
- My favorite fruit is watermelon
- I like watermelon so much
- My favorite subject is math
- I like math so much
- My favorite pet is dog
- I like dog so much
-
- My English name is Eric
- But you can also call me taoge if you like
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ head -n 3 taoge.txt | tail -n 2
- I like watermelon so much
- My favorite subject is math
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
用head和tail的联合, 是个好思路, 但还是不如sed痛快, 让我们来看看sed, 如下:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed -n "2,3"p taoge.txt
- I like watermelon so much
- My favorite subject is math
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
说了那么多, 总算是见到了sed了。
sed的基本格式是:sed 【选项】【"命令"】 taoge.txt .我们来大致介绍一下sed的基本用法。sed的选项通常有-n, -i, -e等, 比较容易掌握, 本文我们重点介绍"命令"部分。
一. p命令, 意思是print, 用于打印。
1. 打印文件,类似于cat命令:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed p taoge.txt
- My favorite fruit is watermelon
- My favorite fruit is watermelon
- I like watermelon so much
- I like watermelon so much
- My favorite subject is math
- My favorite subject is math
- I like math so much
- I like math so much
- My favorite pet is dog
- My favorite pet is dog
- I like dog so much
- I like dog so much
-
-
- My English name is Eric
- My English name is Eric
- But you can also call me taoge if you like
- But you can also call me taoge if you like
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
我们看到, 重复了, 为什么呢? 因为sed在处理信息时, 会把待处理的信息也输出, 那怎样抑制输出待处理的信息呢? 且看-n选项:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed -n p taoge.txt
- My favorite fruit is watermelon
- I like watermelon so much
- My favorite subject is math
- I like math so much
- My favorite pet is dog
- I like dog so much
-
- My English name is Eric
- But you can also call me taoge if you like
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
当然, 你也可以用:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed -n "1,$"p taoge.txt
- My favorite fruit is watermelon
- I like watermelon so much
- My favorite subject is math
- I like math so much
- My favorite pet is dog
- I like dog so much
-
- My English name is Eric
- But you can also call me taoge if you like
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
其中, 1表示第一行, $表示最后一行, 也就是输出第一行到最后一行的内容, 而这就是整个文件。
2. 打印行(第3行到最后一行)
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed -n "3,$"p taoge.txt
- My favorite subject is math
- I like math so much
- My favorite pet is dog
- I like dog so much
-
- My English name is Eric
- But you can also call me taoge if you like
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
这个就不需要我多说了吧, 比所谓的head, tail高级多了。
3. 过滤行, 类似于grep命令, 如下:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed -n "/like/"p taoge.txt
- I like watermelon so much
- I like math so much
- I like dog so much
- But you can also call me taoge if you like
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
4. 从第1行打印, 直到匹配dog的行,这个貌似用得不多
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed -n "1,/dog/p" taoge.txt
- My favorite fruit is watermelon
- I like watermelon so much
- My favorite subject is math
- I like math so much
- My favorite pet is dog
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
5. 打印开始匹配dog的行, 到匹配taoge的行:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed -n "/dog/,/taoge/"p taoge.txt
- My favorite pet is dog
- I like dog so much
-
- My English name is Eric
- But you can also call me taoge if you like
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
二. s命令,用于替换。
1. 简单替换
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ echo "i love you, you love me" | sed "s/love/like/"
- i like you, you love me
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ echo "i love you, you love me" | sed "s/love/like/g"
- i like you, you like me
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
可见, g表示一行上的所有love都替换成like, 否则, 只替换第一个。 继续看:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed "s/taoge/haha/g" taoge.txt
- My favorite fruit is watermelon
- I like watermelon so much
- My favorite subject is math
- I like math so much
- My favorite pet is dog
- I like dog so much
-
- My English name is Eric
- But you can also call me haha if you like
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
其实, 上面这个替换,
并不会改变taoge.txt本身的内容
, 那要改变到文件中, 怎么办呢?
(1) 将结果重定向, 如sed "s/taoge/haha/g" taoge.txt > taoge.txt.bak
(2) 使用-i选项, 如sed -i "s/taoge/haha/g" taoge.txt
2. 仅仅针对某一行进行替换, 如:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed "2s/so/very/g" taoge.txt
- My favorite fruit is watermelon
- I like watermelon very much
- My favorite subject is math
- I like math so much
- My favorite pet is dog
- I like dog so much
-
- My English name is Eric
- But you can also call me taoge if you like
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
3. 针对多行进行替换, 如:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed "1,4s/water//g" taoge.txt
- My favorite fruit is melon
- I like melon so much
- My favorite subject is math
- I like math so much
- My favorite pet is dog
- I like dog so much
-
- My English name is Eric
- But you can also call me taoge if you like
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
4. 只替换第1个i, 如下:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed "s/i/I/1" taoge.txt
- My favorIte fruit is watermelon
- I lIke watermelon so much
- My favorIte subject is math
- I lIke math so much
- My favorIte pet is dog
- I lIke dog so much
-
- My EnglIsh name is Eric
- But you can also call me taoge If you like
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
只替换第2个i, 如下:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed "s/i/I/2" taoge.txt
- My favorite fruIt is watermelon
- I like watermelon so much
- My favorite subject Is math
- I like math so much
- My favorite pet Is dog
- I like dog so much
-
- My English name Is Eric
- But you can also call me taoge if you lIke
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
替换第二个到最后一个o, 如下:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed "s/o/O/2g" taoge.txt
- My favorite fruit is watermelOn
- I like watermelon sO much
- My favorite subject is math
- I like math so much
- My favorite pet is dOg
- I like dog sO much
-
- My English name is Eric
- But you can alsO call me taOge if yOu like
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
5. 多个模式匹配
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed "1,4s/I/i/g; 5,6s/I/you/g" taoge.txt
- My favorite fruit is watermelon
- i like watermelon so much
- My favorite subject is math
- i like math so much
- My favorite pet is dog
- you like dog so much
-
- My English name is Eric
- But you can also call me taoge if you like
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
当然, 也可以用扩展模式(是该介绍-e选项了):
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed -e "1,4s/I/i/g" -e "5,6s/I/you/g" taoge.txt
- My favorite fruit is watermelon
- i like watermelon so much
- My favorite subject is math
- i like math so much
- My favorite pet is dog
- you like dog so much
-
- My English name is Eric
- But you can also call me taoge if you like
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
6. 利用&表示匹配到的东东, 如下:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed "s/so/{&}/g" taoge.txt
- My favorite fruit is watermelon
- I like watermelon {so} much
- My favorite subject is math
- I like math {so} much
- My favorite pet is dog
- I like dog {so} much
-
- My English name is Eric
- But you can al{so} call me taoge if you like
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
7. 在每行前面加一些东西, 来个有意思点的, 颇有点C/C++注释的感觉:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed "s/^/\/\/ /" taoge.txt
- // My favorite fruit is watermelon
- // I like watermelon so much
- // My favorite subject is math
- // I like math so much
- // My favorite pet is dog
- // I like dog so much
- //
- // My English name is Eric
- // But you can also call me taoge if you like
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
实际上, 这个就实现了对代码进行注释了。 可能有的朋友不太清楚, 那我来解释一下:
a. ^表示一行的开口(当然, 如果你熟悉正则表达式, 那就更好了)
b. \/的\表示转义, /表示斜杠, 所以\/\/就是//了.
整体功能就是, 对每行的头加了//
8. 在每行的末尾加点东西, 如下:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed "s/$/ \/\/ comment /" taoge.txt
- My favorite fruit is watermelon // comment
- I like watermelon so much // comment
- My favorite subject is math // comment
- I like math so much // comment
- My favorite pet is dog // comment
- I like dog so much // comment
- // comment
- My English name is Eric // comment
- But you can also call me taoge if you like // comment
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
其实, 说白了, 还是正则表达式。
三. c命令, 用于行替换。
1. 替换第2行, 如下:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed "2 c hehe" taoge.txt
- My favorite fruit is watermelon
- hehe
- My favorite subject is math
- I like math so much
- My favorite pet is dog
- I like dog so much
-
- My English name is Eric
- But you can also call me taoge if you like
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
2. 仅仅替换有dog的行, 如下:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed "/dog/c cat" taoge.txt
- My favorite fruit is watermelon
- I like watermelon so much
- My favorite subject is math
- I like math so much
- cat
- cat
-
- My English name is Eric
- But you can also call me taoge if you like
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
四. i命令, 表示行插入。
1. 在第一行前面插入, 如下:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed "1 i oh, my god" taoge.txt
- oh, my god
- My favorite fruit is watermelon
- I like watermelon so much
- My favorite subject is math
- I like math so much
- My favorite pet is dog
- I like dog so much
-
- My English name is Eric
- But you can also call me taoge if you like
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
2. 在最后一行前面插入, 如下:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed "$ i oh, my god" taoge.txt
- My favorite fruit is watermelon
- I like watermelon so much
- My favorite subject is math
- I like math so much
- My favorite pet is dog
- I like dog so much
-
- My English name is Eric
- oh, my god
- But you can also call me taoge if you like
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
五. a命令, 用于append,也就是依附
1. 在第一行后依附, 如下:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed "1 a oh, my god" taoge.txt
- My favorite fruit is watermelon
- oh, my god
- I like watermelon so much
- My favorite subject is math
- I like math so much
- My favorite pet is dog
- I like dog so much
-
- My English name is Eric
- But you can also call me taoge if you like
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
2. 在最后一行后依附, 如下:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed "$ a oh, my god" taoge.txt
- My favorite fruit is watermelon
- I like watermelon so much
- My favorite subject is math
- I like math so much
- My favorite pet is dog
- I like dog so much
-
- My English name is Eric
- But you can also call me taoge if you like
- oh, my god
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
六. d命令, 表示删除所匹配的行。
1. 删除有is的行, 如下:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed "/is/d" taoge.txt
- I like watermelon so much
- I like math so much
- I like dog so much
-
- But you can also call me taoge if you like
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
2. 删除第1行:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed "1d" taoge.txt
- I like watermelon so much
- My favorite subject is math
- I like math so much
- My favorite pet is dog
- I like dog so much
-
- My English name is Eric
- But you can also call me taoge if you like
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
删除第2行:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed "2d" taoge.txt
- My favorite fruit is watermelon
- My favorite subject is math
- I like math so much
- My favorite pet is dog
- I like dog so much
-
- My English name is Eric
- But you can also call me taoge if you like
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
删除第2-4行:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed "2,4d" taoge.txt
- My favorite fruit is watermelon
- My favorite pet is dog
- I like dog so much
-
- My English name is Eric
- But you can also call me taoge if you like
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
删除第3到最后一行, 如下:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed "3,$"d taoge.txt
- My favorite fruit is watermelon
- I like watermelon so much
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
或者:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed '3,$d' taoge.txt
- My favorite fruit is watermelon
- I like watermelon so much
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
但如下有错误:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed "3,$d" taoge.txt
- sed: -e expression #1, char 2: unexpected `,'
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
七. N命令, 将下一行内容纳入缓冲区做匹配。
1. 先看:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed "N;s/me/XX/" taoge.txt
- My favorite fruit is waterXXlon
- I like watermelon so much
- My favorite subject is math
- I like math so much
- My favorite pet is dog
- I like dog so much
-
- My English naXX is Eric
- But you can also call me taoge if you like
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
2. 再看:
- Administrator@51B6904C3C8A485 ~/learn_sed
- $ sed "N;1,6s/\n/, /" taoge.txt
- My favorite fruit is watermelon, I like watermelon so much
- My favorite subject is math, I like math so much
- My favorite pet is dog, I like dog so much
-
- My English name is Eric
- But you can also call me taoge if you like
-
- Administrator@51B6904C3C8A485 ~/learn_sed
- $
OK, 本文先介绍到这里了, 后续遇到sed的其他用法, 会继续记录在此。