sed

流编辑器 对文本中的行,逐行处理 非交互式的编辑器
是一个编辑器

1、工作流程

1)将文件的第一行读入到自己的缓存空间(模式空间--pattern space),删除掉换行符
2)匹配,看一下改行是不是要编辑的行,如果是-->3;不是-->再读入下一行到模式空间,删除换行符
3)执行编辑命令
4)加上换行符输出到屏幕
5)判断是否为文件最后一行,是-->sed退出;不是,再重复1~4步骤

注意:
1)默认情况下,sed缓存空间内的行都会输出到屏幕,除非使用-n拟制未编辑过得行的输出。
2)默认,sed将修改的行输出到屏幕,并没有修改源文件。-i可以修改源文件(强制备份一份源文件)

2、命令用法
2.1 语法
sed [option] [address1[,address2]] [command][flag] [filename]
option:
  -i :  修改源文件,需要强制备份一份源文件(-i.bak 建议这样写-i选项,会将源文件生成一个.bak备份文件)
                例:sed -i.bak '/baidu$/a\souhu' 1.txt
  -r :  让sed支持扩展的正则表达式(不加选项即支持标准的正则表达式) 匹配你要修改的行
  -f :  指定文件。将sed的命令写到文件中,然后执行sed -f 文件名 要操作的文件
  -e :    允许一条命令执行多个sed子命令

  -n :    拟制输出,不输出未修改的行。强制输出用命令“p”

[address1[,address2] 匹配连续的范围 3,5 表示文本的第3~5行

[command]:增删改查
  a:   append(追加)会在定位的行的下面追加一行;默认对所有的行下面都追加
  i :   insert(插入) ....上面插入一行;...所有行上面插入一行
    d:   delete(删除)
例:删除第6行

sed -i.bak '6d' 1.txt //
例:删除匹配行
sed -i '/^a.*./d' tmp.txt

  s :    substitute 替换字符串(改)

[flag]
  g   全局(针对sed处理的每一行)
  n   数字,处理替换掉该行中第几个出现的关键字
  p   print输出,打印到屏幕
  w   保存到文件
  c    change 更改某行

[filename]:指定要处理的文件,如果没有,结合|,处理的文本行来自其他命令的输出。

2.2 如何定位或者匹配要修改的行
2.2.1 使用行号
x 数字 10 定位第10行;101定位101行
[root@localhost html]# sed '5s/laowang/xiaowang/' sed.txt

定位一个范围:
3,5 表示文本的第3~5行
[root@localhost html]# sed '3,5s/laowang/xiaowang/' sed.txt

定位第一行和最后一行:
1 $
[root@localhost html]# sed '$s/laowang/xiaowang/' sed.txt        //匹配最后一行

奇数行或偶数行
0~2 偶数行
1~2 奇数行

[root@localhost html]# sed '1~2s/laowang/xiaowang/' sed.txt

定位某行之后的n行
x,+2 第x行及之后的2行
[root@localhost html]# sed '2,+2s/laowang/xiaowang/' sed.txt

2.2.2 正则表达式匹配
[root@localhost html]# cat /etc/httpd/conf/httpd.conf | sed '/^Listen/s/80/8080/' | grep Listen
cat /etc/httpd/conf/httpd.conf | sed -n '/^Listen/s/80/8080/p'
例:在apache中找出监听端口哪一行并添加或插入一行
添加 cat /etc/httpd/conf/httpd.conf | sed '/^Listen/a\Listen 8080' | grep Listen    下

插入 cat /etc/httpd/conf/httpd.conf | sed '/^Listen/i\Listen 8080' | grep Listen  上

找到这一行并替换 cat /etc/httpd/conf/httpd.conf | sed '/^Listen/c\hahaha'

修改并备份 sed -i.bak '/^Listen/c\hahaha' /etc/httpd/conf/httpd.conf

[root@localhost conf]# ls
httpd.conf httpd.conf.bak magic

2.3 使用sed对文本增删改查

2.3.1 追加 命令:a

sed 'agebi' sed.txt
sed 'a\gebi' sed.txt
sed '2a\gebi' sed.txt
sed '2,4a\gebi' sed.txt
sed '2,+2a\gebi' sed.txt
sed '$a\gebi' sed.txt 
sed '1~2a\gebi' sed.txt (1~2:表示从1开始间隔2的数:1,3,5....)

2.3.2 插入 命令:i
[root@localhost html]# sed 'i\xiaowang' sed.txt

2.3.3 删 命令:d
[root@localhost html]# sed '2,4d' sed.txt

2.3.4 改
替换字符串 s substitute
更改某行 c change

替换字符串:
s substitute 替换字符串(改)
[flag]
g 全局(针对sed处理的每一行)
n 数字,处理替换掉该行中第几个出现的关键字
p print输出,打印到屏幕
w 保存到文件

[root@localhost html]# sed '1s/nice/nonice/' sed.txt
1.laowang is a nonice man!laowang is a nice man!
2.laowang is a nice man!laowang is a nice man!
3.laowang is a nice man!laowang is a nice man!
4.laowang is a nice man!laowang is a nice man!
5.laowang is a nice man!laowang is a nice man!
[root@localhost html]# sed '1s/nice/nonice/2' sed.txt
1.laowang is a nice man!laowang is a nonice man!
2.laowang is a nice man!laowang is a nice man!
3.laowang is a nice man!laowang is a nice man!
4.laowang is a nice man!laowang is a nice man!
5.laowang is a nice man!laowang is a nice man!
[root@localhost html]# sed '1s/nice/nonice/g' sed.txt
1.laowang is a nonice man!laowang is a nonice man!
2.laowang is a nice man!laowang is a nice man!
3.laowang is a nice man!laowang is a nice man!
4.laowang is a nice man!laowang is a nice man!
5.laowang is a nice man!laowang is a nice man!
[root@localhost html]# sed '1s/nice/nonice/gp' sed.txt
1.laowang is a nonice man!laowang is a nonice man!
1.laowang is a nonice man!laowang is a nonice man!
2.laowang is a nice man!laowang is a nice man!
3.laowang is a nice man!laowang is a nice man!
4.laowang is a nice man!laowang is a nice man!
5.laowang is a nice man!laowang is a nice man!
[root@localhost html]# sed '1s/nice/nonice/gw /tmp/sed1.txt' sed.txt
1.laowang is a nonice man!laowang is a nonice man!
2.laowang is a nice man!laowang is a nice man!
3.laowang is a nice man!laowang is a nice man!
4.laowang is a nice man!laowang is a nice man!
5.laowang is a nice man!laowang is a nice man!
[root@localhost html]# cat /tmp/sed1.txt
1.laowang is a nonice man!laowang is a nonice man!

更改某行 c **********修改一个范围
[root@localhost html]# sed 'c\jj' sed.txt    //修改全部
jj
jj
jj
jj
jj
[root@localhost html]# sed '2c\jj' sed.txt    //修改第二行
1.laowang is a nice man!laowang is a nice man!
jj
3.laowang is a nice man!laowang is a nice man!
4.laowang is a nice man!laowang is a nice man!
5.laowang is a nice man!laowang is a nice man!

*******需要注意更过多行时,需要加上\n
sed '2,4c\abc\nabc\nabc' sed.txt

2.3.5 查 p

[root@localhost html]# sed 'p' sed.txt      //打印输出
1.laowang is a nice man!laowang is a nice man!
1.laowang is a nice man!laowang is a nice man!
2.laowang is a nice man!laowang is a nice man!
2.laowang is a nice man!laowang is a nice man!
3.laowang is a nice man!laowang is a nice man!
3.laowang is a nice man!laowang is a nice man!
4.laowang is a nice man!laowang is a nice man!
4.laowang is a nice man!laowang is a nice man!
5.laowang is a nice man!laowang is a nice man!
5.laowang is a nice man!laowang is a nice man!

[root@localhost html]# sed -n 'p' sed.txt      //-n拟制输出
1.laowang is a nice man!laowang is a nice man!
2.laowang is a nice man!laowang is a nice man!
3.laowang is a nice man!laowang is a nice man!
4.laowang is a nice man!laowang is a nice man!
5.laowang is a nice man!laowang is a nice man!

3、选项
option:
-n 拟制输出,不输出未修改的行。强制输出用命令“p”
-i 修改源文件,需要强制备份一份源文件
-r 让sed支持扩展的正则表达式(不加选项即支持标准的正则表达式) 匹配你要修改的行
-f 指定文件。将sed的命令写到文件中,然后执行sed -f 文件名 要操作的文件
-e 允许一条命令执行多个sed子命令

-i 直接修改源文件,不建议,建议这么用 -i.bak 在修改前备份一份源文件
[root@localhost html]# sed -i '/^Listen/a\Listen 8080' /etc/httpd/conf/httpd.conf //可以直接修改
[root@localhost html]# sed -i.bak '/^Listen/a\Listen 8080' /etc/httpd/conf/httpd.conf //修改源文件同时保留一份备份的文件,文件名*.bak

-r 让sed支持扩展的正则表达式
删除/etc/passwd中每行的第一个字符
[root@localhost conf]# sed -r 's/(^.)(.*)/\2/' /etc/passwd     //扩展正则中的向前引用

删除/etc/passwd中每行的第二个字符
[root@localhost conf]# sed -r 's/(^.)(.)(.*)/\1\3/' /etc/passwd  //向前引用第一个(^.)和第三个(.*)不引用第二个,达到删除第二个字符的效果

[root@localhost ~]# cat a.txt
my me you are thetest
my me you are thetest
my me you are thetest
my me you are thetest
my me you are thetest
my me you are thetest
my me you are thetest

[root@localhost ~]# sed -r 's/(^.)(.)(.*$)/\1\3/' a.txt (删除第二个字符)---->向前引用实例,将a.txt文件分为三个子表达式,\1\3引用第一和第三个子表达式
m me you are thetest
m me you are thetest
m me you are thetest
m me you are thetest
m me you are thetest
m me you are thetest
m me you are thetest

[root@localhost ~]# sed -r 's/(^.)(.)(.*$)/\2\3/' a.txt (删除第一个字符)
y me you are thetest
y me you are thetest
y me you are thetest
y me you are thetest
y me you are thetest
y me you are thetest
y me you are thetest

-f 将sed的命令写到文件中,然后执行sed -f 文件名 要操作的文件
[root@localhost html]# cat a.txt
1s/laowang/xiaowang/g
[root@localhost html]# sed -f a.txt sed.txt
1.xiaowang is a nice man!xiaowang is a nice man!
2.laowang is a nice man!laowang is a nice man!
3.laowang is a nice man!laowang is a nice man!
4.laowang is a nice man!laowang is a nice man!
5.laowang is a nice man!laowang is a nice man!

-e 允许一条命令执行多个sed子命令
[root@localhost html]# sed -e 's/a/A/g' -e 's/nice/nonice/g' sed.txt
1.lAowAng is A nonice mAn!lAowAng is A nonice mAn!
2.lAowAng is A nonice mAn!lAowAng is A nonice mAn!
3.lAowAng is A nonice mAn!lAowAng is A nonice mAn!
4.lAowAng is A nonice mAn!lAowAng is A nonice mAn!
5.lAowAng is A nonice mAn!lAowAng is A nonice mAn!

对一个地址做多个操作:
sed [option] [address] {
command1
command2
...
} filename

示例:

#!/bin/bash
result=`sed '2,4{
s/a/A/g
s/nice/nonice/g
2i abc
}' sed.txt` echo -e "$result\n"

awk、sed、grep三大shell文本处理工具之sed的应用的更多相关文章

  1. awk、sed、grep三大shell文本处理工具之awk的应用

    awk 1.是什么 是一个编程语言.支持变量.数组.函数.流程控制(if...else/for/while) 单行程序语言. 2.工作流程 读取file.标准输入.管道给的数据,从第一行开始读取,逐行 ...

  2. awk、sed、grep三大shell文本处理工具之grep的应用

    1.基本格式grep pattern [file...](1)grep 搜索字符串 [filename](2)grep 正则表达式 [filename]在文件中搜索所有 pattern 出现的位置, ...

  3. shell文本处理工具总结

    shell文本处理工具总结 为了效率,应该熟练的掌握自动化处理相关的知识和技能,能力就表现在做同样的一件事情,可以做的很好的同时,耗时还很短. 再次总结shell文本处理的相关规则,对提高软件调试效率 ...

  4. Linux shell文本处理工具

    搞定Linux Shell文本处理工具,看完这篇集锦就够了 Linux Shell是一种基本功,由于怪异的语法加之较差的可读性,通常被Python等脚本代替.既然是基本功,那就需要掌握,毕竟学习She ...

  5. Linux Shell 文本处理工具集锦--Awk―sed―cut(row-based, column-based),find、grep、xargs、sort、uniq、tr、cut、paste、wc

    本文将介绍Linux下使用Shell处理文本时最常用的工具:find.grep.xargs.sort.uniq.tr.cut.paste.wc.sed.awk:提供的例子和参数都是最常用和最为实用的: ...

  6. Linux Shell 文本处理工具集锦 zz

    内容目录: find 文件查找 grep 文本搜索 xargs 命令行参数转换 sort 排序 uniq 消除重复行 用tr进行转换 cut 按列切分文本 paste 按列拼接文本 wc 统计行和字符 ...

  7. Linux Shell 文本处理工具集锦

    本文将介绍Linux下使用Shell处理文本时最常用的工具:find.grep.xargs.sort.uniq.tr.cut.paste.wc.sed.awk:提供的例子和参数都是最常用和最为实用的: ...

  8. [转] Linux Shell 文本处理工具集锦

    内容目录: find 文件查找 grep 文本搜索 xargs 命令行参数转换 sort 排序 uniq 消除重复行 用tr进行转换 cut 按列切分文本 paste 按列拼接文本 wc 统计行和字符 ...

  9. Linux Shell 文本处理工具集锦(转载)

    内容目录: find 文件查找 grep 文本搜索 xargs 命令行参数转换 sort 排序 uniq 消除重复行 用tr进行转换 cut 按列切分文本 paste 按列拼接文本 wc 统计行和字符 ...

随机推荐

  1. day15 Python函数递归,轻易不要用递归,容易搞出来内存溢出

    古之欲明明德于天下者,先治其国:欲治其国者,先齐其家:欲齐其家者,先修其身:欲修其身者,先正其心:欲正其心者,先诚其意:欲诚其意者,先致其知,致知在格物.物格而后知至,知至而后意诚,意诚而后心正,心正 ...

  2. ORA-01031: insufficient privileges 错误解决

    OS:linux redhat5.5  32bit 数据库:Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 1.问题描述:该数据库监 ...

  3. 新手根据菜鸟教程安装docker,从No package docker-io available开始遇到的坑...(转)

    转文地址:https://www.cnblogs.com/maodot/p/7654918.html 新手centos6.9安装docker时从遇到No package docker-io avail ...

  4. Linux 下 zip 文件解压中文乱码

    windows下的中文文件名拷贝到ubuntu下面以后,文件名直接变成乱码,原因为windows下的文件名以GBK编码,而Ubuntu下的文件 名为utf-8编码 1. ubuntu下有一个工具可以用 ...

  5. JavaScript高级程序设计学习(四)之引用类型(续)

    一.Date类型 其实引用类型和相关的操作方法,远远不止昨天的所说的那些,还有一部分今天继续补充. 在java中日期Date,它所属的包有sql包,也有util包.我个人比较喜欢用util包的.理由, ...

  6. 【Codeforces 3D】Least Cost Bracket Sequence

    Codeforces 3 D 题意:有一个括号序列,其中一些位置是问号,把第\(i\)个问号改成(需要\(a_i\)的代价,把它改成)需要\(b_i\)的代价. 问使得这个括号序列成立所需要的最小代价 ...

  7. php中按值传递和按引用传递的一个问题

    php中传递变量默认是按照值传递. 简单举个例子: <?php function testArray($arr){// &$arr $arr = array(1,2,3,); } $ar ...

  8. Luogu2045 方格取数加强版(K取方格数) 费用流

    题目传送门 题意:给出一个$N \times N$的方格,每个格子中有一个数字.你可以取$K$次数,每次取数从左上角的方格开始,每一次只能向右或向下走一格,走到右下角结束,沿路的方格中的数字将会被取出 ...

  9. Ionic App ActionSheet布局问题

    使用 $ionicActionSheet的时候界面不像Ihpnoe手机那样显示的整齐,但是在电脑浏览器里面却是好的,原因还是Ionic自带css的问题,网上的答案是ionic.min.css/ioni ...

  10. EZ 2018 07 06 NOIP模拟赛

    又是慈溪那边给的题目,这次终于没有像上次那样尴尬了, T1拿到了较高的暴力分,T2没写炸,然后T3写了一个优雅的暴力就203pts,Rank3了. 听说其它学校的分数普遍100+,那我们学校还不是强到 ...