SED的适用场景

SED是Stream EDitor的简称,也就是流编辑器,适用在不打开文件的情况下,增删改查文件内容

SED command in UNIX stands for stream editor and it can perform lots of functions on file like searching, substitution or find and replace, insertion or deletion.By using SED you can edit files even without opening them, which is much quicker way to find and replace something in file, than first opening that file in VI Editor and then changing it.

语法格式

#第一种语法格式
STDOUT | sed [option] <command> #第二种语法格式
sed [option] <command> <file>
sed [option] <command> <<EOF
EOF

command使用双引号包裹,变量生效

command使用单引号包裹,单引号包裹变量后生效

command建议使用单引号包裹,避免非变量生效,如sed '$d'和sed "$d"

== <[area] action>

Option

参数说明

-n #常和p搭配
-e #-e <command> -e <command>
-f #读取文件中的<command>
-i #更新文件(默认是输出到STDOUT)
-r #使用扩展正则表达式,建议使用,语法便利

命令的执行顺序对结果有影响

sed -e <command> -e <commmand> $file

sed 'command;command' $file

sed <command> $file | sed <command> $file

Action作用的行范围

范围 示例
缺省默认所有行
n1 command 第n行
$ command 尾行
n1,n2 command 第n1~n2行
n1,+n 第n1~n1+n行
/ptn1/ command 所有匹配到的行
/ptn1/,/ptn2/ command ptn=ptn1,匹配则区间开始,ptn=ptn2,匹配则区间结束,闭区间,反复执行
/ptn1/,+n command 匹配ptn1即随后n行,反复执行
n1,/ptn2/ command 从n1行到匹配ptn2的闭区间,以及后续匹配ptn2的所有行
/ptn1/,n2 command 从匹配ptn1到n2行的闭区间,以及后续匹配ptn1的所有行

Action类型:行的增删改查

p print

i insert

a append

r read

w write

c

s

y

d

增

#目标行后新增行
sed -r '[area] a <line content>' $file
#目标行前新增行
sed -r '[area] i <line content>' $file
#目标行后新增多行,方式一,单引号包裹command
sed -r '[area] a <line content>\
<line content>\
<line content>' $file
#目标行后新增多行,方式二,双引号包裹command
sed -r "[area] a <line content>\n<line content>\n<line content>" $file
#复制目标行
sed -r "[area] p" $file

删

sed -r '[area] d'

改

#定界符,定界符出现在样式内部时,需要进行转义
echo -e … | sed -r '[area] s/origin/new/' $file
echo -e … | sed -r '[area] s|origin|new|' $file
echo -e … | sed -r '[area] s:origin:new:' $file #每行只替换第一个匹配的字符串
echo -e … | sed -r '[area] s/origin/new/' $file
echo -e … | sed -r '[area] s/origin/new/1' $file #每行替换所有匹配的字符串
echo -e … | sed -r '[area] s/origin/new/g' $file #每行替换第n个及后续匹配的字符串
echo -e … | sed -r '[area] s/origin/new/ng' $file #匹配忽略大小写
echo -e … | sed -r '[area] s/origin/new/ngi' $file #匹配和反向引用,&代表匹配的部分
sed -r '[area] s/pattern/prefix&/g' $file #目标字符串前添加字符串
sed -r '[area] s/pattern/&postfix/g' $file #目标字符串后添加字符串
sed -r '[area] s/^/prefix&/g' $file #在所有行首添加
sed -r '[area] s/$/&postfix/g' $file #在所有行末添加
#分组(匹配的一部分)和反向引用
sed -rn '[area] s/.*(ptn1).*(ptn2).*/\1\2/ p' $file
#分组(匹配的一部分)和反向引用,提取字符串
sed -rn '[area] s/.*(ptn1).*/\1/ p' $file #将多行替换成一行
sed '[area] c new line' $file

查

sed -rn '[area] p' $file

正则Regular Expression

推荐使用扩展语法sed -r ...,统一的规范。

扩展 默认 效果
? \?
* *
+ \+
{n1} \{n1\}
{n1,n2} \{n1,n2\}
{n1,} \{n1,\}
^ ^
$ $
[] []
[^] [^]
[a-zA-Z0-9] [a-zA-Z0-9]
\w \w 字母数字下划线
\W \W 字母数字下划线
\s \s
\S \S
() \(\)

Linux|常用命令|sed的更多相关文章

  1. linux 常用命令大全

    linux 常用命令大全 系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统 ...

  2. Linux常用命令速查备忘

    Linux常用命令速查备忘   PS:备忘而已,详细的命令参数说明自己man 一. 启动,关机,登入,登出相关命令 [login] 登录 [logout] 登出 [exit] 登出 [shutdown ...

  3. Linux常用命令大全(非常全!!!)

    Linux常用命令大全(非常全!!!) 最近都在和Linux打交道,感觉还不错.我觉得Linux相比windows比较麻烦的就是很多东西都要用命令来控制,当然,这也是很多人喜欢linux的原因,比较短 ...

  4. 【原创】Linux基础之linux常用命令之文本替换

    linux常用命令之文本替换 1 vi vi test_file :%s/h/h1/g 注释:全文替换,将h替换为h1 :1,4s/h/h1/g 注释:将第1行到第4行的h替换为h1 :%s/\n/, ...

  5. 转载Alpine Linux常用命令

    Alpine Linux常用命令 目录 一:Alpine Linux开启SSH远程登陆 1.简介: 2.配置 3.配置命令 4.重启服务 二:Alpine Linux源管理 1.简介 2.国内源简介: ...

  6. 【转载】Linux常用命令

    Linux常用命令大全(非常全!!!) 转载出处:https://www.cnblogs.com/yjd_hycf_space/p/7730690.html 系统信息 arch 显示机器的处理器架构( ...

  7. Linux常用命令-文本查看篇

    前言 Linux常用命令中,除了cat还有很多其他用于文本查看的命令.本文将简单介绍一下这些文本查看的命令. 全文本显示--cat cat可能是常用的一个文本查看命令了,使用方法也很简单: cat f ...

  8. Linux常用命令全称

    Linux常用命令全称 pwd:print work directory   打印当前目录 显示出当前工作目录的绝对路径 ps: process status(进程状态,任务管理器)    常用参数: ...

  9. Linux常用命令大全(转)

    (转)Linux常用命令大全(非常全!!!) 最近都在和Linux打交道,感觉还不错.我觉得Linux相比windows比较麻烦的就是很多东西都要用命令来控制,当然,这也是很多人喜欢linux的原因, ...

  10. 18-10-09 Linux常用命令大全(非常全!!!)

     Linux常用命令大全(非常全!!!)   Linux常用命令大全(非常全!!!) 最近都在和Linux打交道,感觉还不错.我觉得Linux相比windows比较麻烦的就是很多东西都要用命令来控制, ...

随机推荐

  1. ElasticSearch+Kibana+Packetbeat

    一.介绍 Packetbeat 是一个实时网络数据包分析工具,通过嗅探应用服务器之间的网络通讯,来解码应用层协议类型如HTTP.MySQL.redis等等,关联请求与响应,并记录每个事务有意义的字段. ...

  2. 005 Linux 命令三剑客之-sed

    grep:数据查找定位 awk:数据切片,数据格式化,功能最复杂 sed:数据修改 01 Linux 命令三剑客? 三剑客各有所长,和锅锅一一搞起就是了! sed:擅长数据修改. grep:擅长数据查 ...

  3. python浮点数计算--5

    #!/usr/bin/python #coding=utf-8 i=1.0 j=3 print(i*j) print(i+j) print(i**j) 备注:无论是哪种运算,只要有操作数是浮点数,py ...

  4. 一起玩转玩转LiteOS组件:TinyFrame

    摘要:TinyFrame是一个简单的用于解析串口(如 UART.telnet.套接字等)通信数据帧的库. 本文分享自华为云社区<LiteOS组件尝鲜-玩转TinyFrame>,作者:Lio ...

  5. python03day

    回顾 pycharm简单使用 while循环 结构 pass while实现打印1-2+3-4+--+99 格式化输出:针对str,让字符串中某些位置变为动态可传入的 % s str d digist ...

  6. Task+ConcurrentQueue多线程编程

    队列(Queue)代表了一个先进先出的对象集合.当您需要对各项进行先进先出的访问时,则使用队列.当您在列表中添加一项,称为入队,当您从列表中移除一项时,称为出队. ConcurrentQueue< ...

  7. bom-对话框

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Java定时器Timer使用方法详解

    感谢大佬:https://www.jb51.net/article/129808.htm 一.概念 定时计划任务功能在Java中主要使用的就是Timer对象,它在内部使用多线程的方式进行处理,所以它和 ...

  9. 导出SQL语句

    转载请注明来源:https://www.cnblogs.com/hookjc/ if(!($db_conn=mysql_connect($db_server,$db_name,$db_pass))){ ...

  10. git 下载及更新

    转载请注明来源:https://www.cnblogs.com/hookjc/ 在完成了创建之后,GitHub会提示你如何向这个Repository上传代码.  首次上传: Git 本地上传 添加新文 ...