Linux|常用命令|sed

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的更多相关文章
- linux 常用命令大全
linux 常用命令大全 系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统 ...
- Linux常用命令速查备忘
Linux常用命令速查备忘 PS:备忘而已,详细的命令参数说明自己man 一. 启动,关机,登入,登出相关命令 [login] 登录 [logout] 登出 [exit] 登出 [shutdown ...
- Linux常用命令大全(非常全!!!)
Linux常用命令大全(非常全!!!) 最近都在和Linux打交道,感觉还不错.我觉得Linux相比windows比较麻烦的就是很多东西都要用命令来控制,当然,这也是很多人喜欢linux的原因,比较短 ...
- 【原创】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/, ...
- 转载Alpine Linux常用命令
Alpine Linux常用命令 目录 一:Alpine Linux开启SSH远程登陆 1.简介: 2.配置 3.配置命令 4.重启服务 二:Alpine Linux源管理 1.简介 2.国内源简介: ...
- 【转载】Linux常用命令
Linux常用命令大全(非常全!!!) 转载出处:https://www.cnblogs.com/yjd_hycf_space/p/7730690.html 系统信息 arch 显示机器的处理器架构( ...
- Linux常用命令-文本查看篇
前言 Linux常用命令中,除了cat还有很多其他用于文本查看的命令.本文将简单介绍一下这些文本查看的命令. 全文本显示--cat cat可能是常用的一个文本查看命令了,使用方法也很简单: cat f ...
- Linux常用命令全称
Linux常用命令全称 pwd:print work directory 打印当前目录 显示出当前工作目录的绝对路径 ps: process status(进程状态,任务管理器) 常用参数: ...
- Linux常用命令大全(转)
(转)Linux常用命令大全(非常全!!!) 最近都在和Linux打交道,感觉还不错.我觉得Linux相比windows比较麻烦的就是很多东西都要用命令来控制,当然,这也是很多人喜欢linux的原因, ...
- 18-10-09 Linux常用命令大全(非常全!!!)
Linux常用命令大全(非常全!!!) Linux常用命令大全(非常全!!!) 最近都在和Linux打交道,感觉还不错.我觉得Linux相比windows比较麻烦的就是很多东西都要用命令来控制, ...
随机推荐
- 沁恒CH32F103C8T6(三): PlatformIO DAPLink和WCHLink下载配置
目录 沁恒CH32F103C8T6(一): Keil5环境配置,示例运行和烧录 沁恒CH32F103C8T6(二): Linux PlatformIO环境配置, 示例运行和烧录 沁恒CH32F103C ...
- windows系统配置Nginx使用SSL证书实现Https反向代理
Nginx反向代理服务,可以代理接收请求,并把请求转发到设置好的其他服务器上. 例如,Nginx服务器为 100.101.102.103,A服务为 100.101.102.104 ,通过Nginx配置 ...
- 配置Nginx使用Active Directory 做认证
配置Nginx使用AD做认证 nginx.conf 配置 http { ldap_server ldap { url ldap://xxx:389/DC=test,DC=com?sAMAccountN ...
- 解决for循环中空格的问题
[root@node-01 ~]# cat 1 a b c ab cd 如果想按行循环遍历出文件中内容,直接使用for是有问题的,第一行按空格分隔的会有问题 [root@node-01 ~]# for ...
- Java 继承01
继承 ●示例 class Person { public String name; Person(){ System.out.println("Person Constrctor...&qu ...
- mysql自连接?
一.自连接 /* 自己查询自己 把一张表看成是两张表. 表的设计. */ SELECT * from depart; -- 具体的查询方法,查询 name ,并给添加别名. select d1.nam ...
- MySQL 5.7.19 简易安装、卸载教程
前言:传统的 exe 文件安装的MySQL,安装后特别难卸载,而且一旦处理不好,就容易出错,想再安装别的版本也不容易.因为这种方式的安装,虽然是不断的下一步,但是卸载的时候需要处理很多,在本文最后,有 ...
- Codeforces Round #739 (Div. 3)
A. Dislike of Threes 简单的水题,预处理即可 AC_CODE #include <bits/stdc++.h> using namespace std; templat ...
- 使用OpenFileDialog打开文件和使用FolderBrowserDialog选定文件夹
选定文件夹 string foldPath = ""; FolderBrowserDialog dialog = new FolderBrowserDialog(); dialog ...
- dotnet 替换 ASP.NET Core 的底层通讯为命名管道的 IPC 库
这是一个用于本机多进程进行 IPC 通讯的库,此库的顶层 API 是采用 ASP.NET Core 的 MVC 框架,其底层通讯不是传统的走网络的方式,而是通过 dotnetCampus.Ipc 开源 ...