Linux命令篇-sed 命令

sed – stream editor for filtering and transforming text;

sed:利用脚本来处理、编辑文本文件;

格式:sed [OPTION]… {script-only-if-no-other-script} [input-file]…

常用参数:

OPTIONS 意义 -e或–expression=

参考案例:

案例数据 使用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.' 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
  • 路径替换
创建测试文本
$ cat << 'EOF' > a.out
base_path = /aaa/bbb/ccc
base_path1 = /aaa/bbb/ccc
base_path2 = /aaa/bbb/ccc
EOF

备份文本并修改文本内容
$ sed -i.bak "s#base_path = /aaa/bbb/ccc#base_path = /ccc/ddd/eee#g" a.out

查看修改后的文件
$ cat a.out
base_path = /ccc/ddd/eee
base_path1 = /aaa/bbb/ccc
base_path2 = /aaa/bbb/ccc

查看修改前的文件
$ cat a.out.bak
base_path = /aaa/bbb/ccc
base_path1 = /aaa/bbb/ccc
base_path2 = /aaa/bbb/ccc
  • 特殊字符替换成回车并统计行数
$ cat > 2.out <
  • 回车替换成特殊字符
$ cat > 3.out <

Original: https://www.cnblogs.com/HOsystem/p/16211837.html
Author: HOsystem
Title: Linux命令篇-sed 命令

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/606784/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • python2.6.6安装Image模块

    python2.6.6安装Image模块1、下载Image模块源码地址:http://www.pythonware.com/products/pil/index.htm2、加压文件…

    Linux 2023年6月14日
    083
  • 虚拟机无法联网

    状态:虚拟机使用NAT模式的VMnet8网卡, ping baidu.com 无法联通 解决: ipconfig查看物理机网络: ip配置:把网关 GATEWAY 改为 192.1…

    Linux 2023年6月8日
    077
  • mybatis-plus详细讲解

    本文笔记都是观看狂神老师视频手敲的,视频地址:https://www.bilibili.com/video/BV17E411N7KN 学java后端的都可以去看一下,从基础到架构很…

    Linux 2023年6月7日
    0111
  • Python Docstring 风格和写法学习

    什么是Python Docstring 和Java类似,Python也通过注释形式的Docstring给程序、类、函数等建立文档。通过Docstring建立的文档不仅对人来说有更好…

    Linux 2023年6月14日
    091
  • 什么是虚拟计算机集群

    这个问题来自近期几位网友的私信,他们不约而同问到一个问题:什么是虚拟计算机集群?Laxcus分布式操作系统是如何做的?下面就正式回答一下这个问题。 在我们传统的认知里,或者大家平常…

    Linux 2023年6月6日
    094
  • 重写并自定义依赖的原生的Bean方法

    转载请注明出处: 在项目开发过程中,往往是直接应用很多jar包中依赖且声明好的Bean,拿来即用,但很多场景也需要对这些原生的Bean 进行自定义,定制化封装,这样在项目使用的过程…

    Linux 2023年6月15日
    0120
  • rabbitmq-安装部署及基础操作

    yum 安装 rabbitmq # centos7 编译安装rabbitmq 在安装RabbitMQ中需要注意:1、RabbitMQ依赖于Erlang,需要先安装Erlang2、E…

    Linux 2023年6月14日
    0104
  • Redis主从复制搭建及原理

    1 简介 1.1 Redis在单机、单节点、单实例下存在的问题 单机故障 内存容量有限 访问压力 Redis主从架构主要解决的问题:单机故障和访问压力,通过主从架构可以将访问流量分…

    Linux 2023年6月13日
    090
  • 主机存活探测程序

    一、ICMP协议原理 什么是icmp协议 因特网控制报文协议ICMP(Internet Control Message Protocol)是一个差错报告机制,是TCP/IP协议簇中…

    Linux 2023年6月7日
    099
  • Linux下的SELINUX

    理解Linux下的SELinux 长久以来,每当遇到授权问题或者新安装的主机,我的第一反应是通过 setenforce 0命令禁用SELinux,来减少产生的权限问题,但是这并不是…

    Linux 2023年6月7日
    082
  • 第2次作业:支付宝案例分析

    1.介绍产品相关信息 *你选择的产品是? 支付宝 *为什么选择该产品作为分析? 在使用支付宝前,像交学费这种金额比较大的金钱来往都得去银行处理,在银行排队通常需要很多时间,尤其是办…

    Linux 2023年6月8日
    071
  • 使用PowerShell收集多台服务器的性能计数器

    写在前面 当管理多台Windows Server服务器时(无论是DB、AD、WEB以及其他的应用服务器),当出现性能或其他问题后,参阅性能计数器都是一个非常好的维度从而推测出问题可…

    Linux 2023年5月28日
    086
  • linux中实时监控目录中生成的文件,并钉钉告警

    inotify是一个API 需要通过开发应用程序进行调用,对于大多数用户来讲这有着许多不便,inotify-tools的出现弥补了这一不足。 inotify-tools是一套组件,…

    Linux 2023年6月6日
    0108
  • 嵌入式软件架构设计-程序分层

    1 前言 在嵌入式MCU软件开发过程中,程序分层设计也是重中之重,关系到整个软件开发过程中的协同开发,降低系统软件的复杂度(复杂问题分解)和依赖关系、同时有利于标准化,便于管理各层…

    Linux 2023年6月7日
    0137
  • Django_模型详解

    Django_模型ORM Django中内嵌了ORM框架,不需要直接编写SQL语句进行数据库操作,而是通过定义模型类,操作模型类来完成对数据库中表的增删改查和创建等操作。 O是ob…

    Linux 2023年6月7日
    094
  • Jmeter 使用Json提取请求数据

    使用Json提取器可以提取请求响应数据 Json提取器 位置: 后置处理器-》Json提取器 使用介绍 1,变量名 变量名,其他部分引用方式: ${变量名}若提取多个变量,多个之间…

    Linux 2023年6月8日
    079
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球