Linux命令篇 - sed 命令
sed
sed - stream editor for filtering and transforming text;
sed:利用脚本来处理、编辑文本文件;
格式:sed [OPTION]... {script-only-if-no-other-script} [input-file]...
常用参数:
| OPTIONS | 意义 |
|---|---|
| -e或--expression=<script文件> | 以选项中指定的script来处理输入的文本文件 |
| -f<script文件>或--file=<script文件> | 以选项中指定的script文件来处理输入的文本文件 |
| -h或--help | 显示帮助 |
| -n或--quiet或--silent | 仅显示script处理后的结果 |
| -V或--version | 显示版本信息 |
| -n | 用于禁止打印所有内容 |
| G | 在每行数据后插入换行 |
| -i | 将sed修改后内容写入文件 |
参考案例:
# 案例数据 使用ctrl+c结束输出
$ cat > content.out
lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.lorem ipsum has been the industry's standard dummy text.
lorem ipsum is highly used by designers. lorem ipsum is great for developers.
lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
learn more about dummy text.
- 单个字符串替换(词或符号)
# 替换匹配的第一个word or characters
# 默认只会替换第一个匹配的lorem,后面的lorem不再替换
# lorem是匹配的词,Lorem是替换的词
$ sed 's/lorem/Lorem/' content.out
Lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.lorem ipsum has been the industry's standard dummy text.
Lorem ipsum is highly used by designers. lorem ipsum is great for developers.
Lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
learn more about dummy text.
- 多个字符串替换
# 替换匹配的全部word or characters
# 若需要替换匹配的全部则在''之间使用/g
$ sed 's/lorem/Lorem/g' content.out
Lorem ipsum is a dummy text. Lorem ipsum has been the industry's standard dummy text.Lorem ipsum has been the industry's standard dummy text.
Lorem ipsum is highly used by designers. Lorem ipsum is great for developers.
Lorem ipsum is used for dummy text. Lorem ipsum doesn't have meaning.
learn more about dummy text.
- 替换指定位置的单个字符串
# 将位置为第二的lorem替换为Lorem
# 在每一行中替换第n个出现的word or characters
# 使用 /1,/2 or n (any number) 指定替换位置
$ sed 's/lorem/Lorem/2' content.out
lorem ipsum is a dummy text. Lorem ipsum has been the industry's standard dummy text.lorem ipsum has been the industry's standard dummy text.
lorem ipsum is highly used by designers. Lorem ipsum is great for developers.
lorem ipsum is used for dummy text. Lorem ipsum doesn't have meaning.
learn more about dummy text.
- 多个字符替换 (类似tr命令)
# 将set1中出现的所有字符替换为set2中对应的字符
# 在''之间使用y参数
$ echo 'a for apple' | sed -e 'y/af/AF/'
A For Apple
- 替换指定开始位置的字符串
# 从指定位置2开始往后的lerem都被替换成Lorem
$ sed 's/lorem/Lorem/2g' content.out
lorem ipsum is a dummy text. Lorem ipsum has been the industry's standard dummy text.Lorem ipsum has been the industry's standard dummy text.
lorem ipsum is highly used by designers. Lorem ipsum is great for developers.
lorem ipsum is used for dummy text. Lorem ipsum doesn't have meaning.
learn more about dummy text.
- 指定行替换字符串
# 在s前面使用数字 表示指定行替换
# 在下面的命令中,我们可以发现只有第二行被字符串'Lorem'取代
$ sed '2 s/lorem/LOREM/' content.out
lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.lorem ipsum has been the industry's standard dummy text.
LOREM ipsum is highly used by designers. lorem ipsum is great for developers.
lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
learn more about dummy text.
- 显示文本的部分内容
# -n: 用于禁止显示全部内容
# p: 用于打印特定行
# 显示2到4行的内容
$ sed -n 2,4p content.out
lorem ipsum is highly used by designers. lorem ipsum is great for developers.
lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
learn more about dummy text.
- 显示指定行外的文本内容
# 显示除去1到2行外的内容
$ sed 1,2d content.out
lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
learn more about dummy text.
- 显示匹配行外的文本内容
$ sed '/learn/d' content.out
lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.lorem ipsum has been the industry's standard dummy text.
lorem ipsum is highly used by designers. lorem ipsum is great for developers.
lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
- 显示替换的行
# 显示被替换的行
# 最后一行learn more about dummy text.没有匹配lorem就不显示出来
$ sed -n 's/lorem/Lorem/p' content.out
Lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.lorem ipsum has been the industry's standard dummy text.
Lorem ipsum is highly used by designers. lorem ipsum is great for developers.
Lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
- 'p'和'-n'
# 打印第一行文本内容
$ sed -n '1p' content.out
lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.lorem ipsum has been the industry's standard dummy text.
# 打印指定范围行文本内容
# 打印第一行到第四行文本内容
$ sed -n '1,4p' content.out
lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.lorem ipsum has been the industry's standard dummy text.
lorem ipsum is highly used by designers. lorem ipsum is great for developers.
lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
learn more about dummy text.
# 打印多行文本内容
# 打印第一行和第四行文本内容
$ sed -n '1p;4p' content.out
# 打印一个文件内容
$ sed -n 'p' content.out
lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.lorem ipsum has been the industry's standard dummy text.
lorem ipsum is highly used by designers. lorem ipsum is great for developers.
lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
learn more about dummy text.
# 显示匹配字符串的行
$ sed -n '/lorem/p' content.out
# 输出包含数字的行
$ sed -n '/[0-9]/p' content.out
# 显示匹配的行并在前面添加`Matched--`内容
$ sed -n 's/^l/Matched--&/p' content.out
Matched--lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.lorem ipsum has been the industry's standard dummy text.
Matched--lorem ipsum is highly used by designers. lorem ipsum is great for developers.
Matched--lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
Matched--learn more about dummy text.
- 使用sed替代grep
# 使用sed完成grep的功能
$ sed -n '/root/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
- sed多参数
# 将所有出现的“lorem”替换为“lorem”,并删除与/learn/搜索模式匹配的行;
$ sed -e 's/lorem/Lorem/g' -e '/learn/d' content.out
Lorem ipsum is a dummy text. Lorem ipsum has been the industry's standard dummy text.Lorem ipsum has been the industry's standard dummy text.
Lorem ipsum is highly used by designers. Lorem ipsum is great for developers.
Lorem ipsum is used for dummy text. Lorem ipsum doesn't have meaning.
- 插入换行符
# 插入单行换行符
$ sed G content.out
# 插入多行换行符
$ sed 'G;G' content.out
- 备份文本并修改文本内容
# use 'i.<file extension>' for backup file name and -e editing.
# 创建一个原始文件'content.txt'的备份为'content.txt.bak'
$ sed -i.bak -e 's/lorem/LOREM/g' content.out
# Results
# 修改的文件
$ cat content.out
LOREM ipsum is a dummy text. LOREM ipsum has been the industry's standard dummy text.LOREM ipsum has been the industry's standard dummy text.
LOREM ipsum is highly used by designers. LOREM ipsum is great for developers.
LOREM ipsum is used for dummy text. LOREM ipsum doesn't have meaning.
learn more about dummy text.
# 备份文件
$ cat content.out.bak
lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.lorem ipsum has been the industry's standard dummy text.
lorem ipsum is highly used by designers. lorem ipsum is great for developers.
lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
learn more about dummy text.
- 删除匹配的行
# In the following output, the line with starting 'lorem' and ending with 'text.' is deleted
# 删除以lerem且test.结尾的行
$ sed -e 's/^lorem.*text.$//g' content.out
LOREM ipsum is a dummy text. LOREM ipsum has been the industry's standard dummy text.LOREM ipsum has been the industry's standard dummy text.
LOREM ipsum is highly used by designers. LOREM ipsum is great for developers.
LOREM ipsum is used for dummy text. LOREM ipsum doesn't have meaning.
learn more about dummy text.
- 添加数据到指定位置
# 可以看到“Here”被添加在每一行的前面
# 添加数据到行首
$ sed -e 's/.*/Here &/' content.out
Here LOREM ipsum is a dummy text. LOREM ipsum has been the industry's standard dummy text.LOREM ipsum has been the industry's standard dummy text.
Here LOREM ipsum is highly used by designers. LOREM ipsum is great for developers.
Here LOREM ipsum is used for dummy text. LOREM ipsum doesn't have meaning.
Here learn more about dummy text.
# 插入字符串在每行开始并换行
$ sed 'i \inserted line' content.out
inserted line
LOREM ipsum is a dummy text. LOREM ipsum has been the industry's standard dummy text.LOREM ipsum has been the industry's standard dummy text.
inserted line
LOREM ipsum is highly used by designers. LOREM ipsum is great for developers.
inserted line
LOREM ipsum is used for dummy text. LOREM ipsum doesn't have meaning.
inserted line
learn more about dummy text.
# 插入字符串在每行末尾并换行
$ sed 'a \Appended line' content.out
LOREM ipsum is a dummy text. LOREM ipsum has been the industry's standard dummy text.LOREM ipsum has been the industry's standard dummy text.
Appended line
LOREM ipsum is highly used by designers. LOREM ipsum is great for developers.
Appended line
LOREM ipsum is used for dummy text. LOREM ipsum doesn't have meaning.
Appended line
learn more about dummy text.
Appended line
- 提取数据
# 将提取所有可用的linux用户名
$ sed 's/\([^:]*\).*/\1/' /etc/passwd
- 打印不带注释#和空行的数据
# 打印不带注释和空行的数据
$ sed -e 's/#.*//;/^$/d' content.out
LOREM ipsum is a dummy text. LOREM ipsum has been the industry's standard dummy text.LOREM ipsum has been the industry's standard dummy text.
LOREM ipsum is highly used by designers. LOREM ipsum is great for developers.
LOREM ipsum is used for dummy text. LOREM ipsum doesn't have meaning.
learn more about dummy text.
- 移除带有注释#的行
$ sed 's/#.*//' /etc/yum.repos.d/CentOS-Base.repo
- 从文本种提取ip地址
# 添加测试数据
$ vi content.txt
lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.
lorem ipsum is highly used by designers. lorem ipsum is great for developers.
lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
learn more about dummy text.
122.65.49.2
221.11.165.233
219.158.9.97
219.158.19.137
# 复杂版 - 提取ip地址
$ sed '/\n/!s/[0-9.]\+/\n&\n/;/^\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\n/P;D' content.txt
# 简化版 - 提取ip地址
$ sed -n '/[0-9]/p' content.txt
# 使用管道(|)将sed与其他命令组合使用
$ ip addr | sed -n '/inet/p' |sed -e 's/ */ /g'|cut -d ' ' -f3
127.0.0.1/8
::1/128
192.168.188.188/24
fe80::400e:cc35:e4ab:8c3f/64
172.17.0.1/16
fe80::42:65ff:fee9:cfb0/64
fe80::4cf2:b7ff:feb4:44fa/64
fe80::4478:34ff:fe5b:8d9a/64
- 数据提取并重定向并写到新的文件中
$ sed -n '/root/w rootpwd.txt' /etc/passwd
$ cat rootpwd.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
Linux命令篇 - sed 命令的更多相关文章
- Linux实战教学笔记12:linux三剑客之sed命令精讲
第十二节 linux三剑客之sed命令精讲 标签(空格分隔): Linux实战教学笔记-陈思齐 ---更多资料点我查看 1,前言 我们都知道,在Linux中一切皆文件,比如配置文件,日志文件,启动文件 ...
- Linux中使用sed命令或awk命令修改常规配置文件
一.方案: Linux中使用sed命令或awk命令修改常规配置文件 二.步骤: 1.假设有一个a.txt,内容如下: #!/bin/bash aa= bbb= ccc= #ddd= 2.如果想要把里面 ...
- 快速理解linux流编辑器sed命令
原创 杜亦舒性能与架构 之前介绍过 awk 命令,sed 命令同样是非常重要的文本处理工具,涉及到linux shell开发时,几乎是避不开这两大利器的 sed 是 stream editor 的简写 ...
- day14 linux三剑客之sed命令
day14 linux三剑客之sed命令 sed命令 Sed 主要用来自动编辑一个或多个文件.简化对文件的反复操作.编写转换程序等. sed(流式编辑器) : sed主要用来修改文件. 1.sed命令 ...
- linux命令总结sed命令详解
Sed 简介 sed 是一种新型的,非交互式的编辑器.它能执行与编辑器 vi 和 ex 相同的编辑任务.sed 编辑器没有提供交互式使用方式,使用者只能在命令行输入编辑命令.指定文件名,然后在屏幕上查 ...
- 【Linux 命令】sed 命令
文章转载自:https://www.jianshu.com/p/779f40985b20 文本分隔:------ # 在每一行后面增加一空行. sed G # 在每一行后面增加两行空行. sed &q ...
- linux shell 用sed命令在文本的行尾或行首添加字符
转自 http://www.cnblogs.com/aaronwxb/archive/2011/08/19/2145364.html 昨天写一个脚本花了一天的2/3的时间,而且大部分时间都耗在了sed ...
- linux三剑客之sed命令
一.前言 我们都知道,在Linux中一切皆文件,比如配置文件,日志文件,启动文件等等.如果我们相对这些文件进行一些编辑查询等操作时,我们可能会想到一些vi,vim,cat,more等命令.但是这些命令 ...
- Linux Shell编程 sed命令
概述 sed 是一种几乎可以应用在所有 UNIX 平台(包括 Linux)上的轻量级流编辑器,体积小.所以,它可以对从如管道这样的标准输入中接收的数据进行编辑. sed 主要是用来将数据进行选取.替换 ...
随机推荐
- window10使用putty传输文件到Linux服务器
由于Linux和Linux可以使用scp进行传输文件,而window系统无法向Linux传输文件,当然,有xshell等等类似的工具可以进行操作:putty工具就可以实现,毕竟zip压缩包也不大,启动 ...
- 为什么 Java 中的 String 是不可变的(Immutable)?
Java 中的 String 不可变是因为 Java 的设计者认为字符串使用非常频繁,将字 符串设置为不可变可以允许多个客户端之间共享相同的字符串.
- volatile 修饰符的有过什么实践?
一种实践是用 volatile 修饰 long 和 double 变量,使其能按原子类型来读写. double 和 long 都是 64 位宽,因此对这两种类型的读是分为两部分的,第一次 读取第一个 ...
- 学习Apache(四)
介绍 Apache HTTP 服务器被设计为一个功能强大,并且灵活的 web 服务器, 可以在很多平台与环境中工作.不同平台和不同的环境往往需要不同 的特性,或可能以不同的方式实现相同的特性最有效率. ...
- 学习Haproxy (八)
Unix套接字命令(Unix Socket commands) socat是一个多功能的网络工具,名字来由是"Socket CAT",可以看作是netcat的N倍加强版,socat ...
- 学习ELK日志平台(一)
一.需求及基础: 场景: 1.开发人员不能登录线上服务器查看详细日志 2.各个系统都有日志,日志数据分散难以查找 3.日志数据量大,查询速度慢,或者数据不够实时 4.一个调用会涉及到多个系统,难以在这 ...
- 设计一个简单的devops系统
前言 公司设计的RDMS挺好用的,我也照猫画虎简单的设计一个DevOps系统,与大家分享,不足之处欢迎拍砖,以免误人子弟 前置条件 gitlab gitlab-runner k8s 1. gitlab ...
- Python这些位运算的妙用,绝对让你大开眼界
位运算的性能大家想必是清楚的,效率绝对高.相信爱好源码的同学,在学习阅读源码的过程中会发现不少源码使用了位运算.但是为啥在实际编程过程中应用少呢?想必最大的原因,是较为难懂.不过,在面试的过程中,在手 ...
- 基于HTML5的网络拓扑图(1)
什么是网络拓扑 网络拓扑,指构成网络的成员间特定的排列方式.分为物理的,即真实的.或者逻辑的,即虚拟的两种.如果两个网络的连接结构相同,我们就説它们的网络拓扑相同,尽管它们各自内部的物理接线.节点间距 ...
- 使用 Vuex + Vue.js 构建单页应用【新篇】
使用 Vuex + Vue.js 构建单页应用[新篇] 在去年的七月六号的时候,发布了一篇 使用 Vuex + Vue.js 构建单页应用 的文章,文章主要是介绍 vuex 的基本使用方法,发现对大部 ...