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)

大家都在看

  • Linux之Nginx模块扩展

    404. 抱歉,您访问的资源不存在。 可能是URL不正确,或者对应的内容已经被删除,或者处于隐私状态。 [En] It may be that the URL is incorre…

    Linux 2023年5月27日
    079
  • 离线版centos8环境部署迁移监控操作笔记

    嗨咯,前两天总结记录了离线版centos8下docker的部署笔记,今天正好是2021年的最后一天,今天正好坐在本次出差回家的列车上,车上没有上面事做,索性不如把本次离线版cent…

    Linux 2023年6月14日
    0107
  • Docker 打包部署web项目 [Dockerfile方式]

    系统环境 IDE: IDEA 2020.2 Linux: WSL2 Ubuntu 20.04 Docker: 20.10.8 参考资料 博客园: Dockerfile常用指令介绍 …

    Linux 2023年6月14日
    0106
  • IDEA生成带参数和返回值注释

    步骤说明 打开IDEA进入点击左上角 – 文件 – 设置 – 编辑器 – 活动模板 新建活动模板 填写模板文本 编辑变量 添加变量表…

    Linux 2023年6月6日
    0110
  • RAID磁盘阵列

    RAID磁盘阵列 *本章重点:了解各RAID级别的原理优缺点及常用级别实现,企业中厂商大多提供了硬RAID方案。 1、什么是RAID? “RAID”一词是由…

    Linux 2023年6月7日
    095
  • Docker安装使用及私有仓库搭建

    1 概念 1.1 基本概念 Docker daemon​ 守护进程,运行在宿主机上,用户通过DockerClient客户端Docker命令与Docker daemon交互。Dock…

    Linux 2023年5月27日
    098
  • Java面向对象之各种变量详解

    在Java中一定有很多变量让大家头疼,成员变量、类变量、局部变量等等,今天就来分别认识认识他们吧! Java面向对象之各种变量详解 前言 在 Java语言中, 根据定义变量位置的不…

    Linux 2023年6月13日
    081
  • 快速上手FastJSON

    作为一名后端开发而言肯定会接触数据,把数据提供给前端或者把数据存储起来,目前比较火热的传输格式是json,给前端传json是再常见不过啦,甚至是往db里面直接存入json。 在ja…

    Linux 2023年6月14日
    084
  • Redis的slot迁移

    1) 在目标节点B 上执行,从节点A 导入slot 到节点B : CLUSTER SETSLOT8 IMPORTING src– A-node-id 对于迁移的slot…

    Linux 2023年5月28日
    071
  • Ubuntu18.04 显卡驱动安装(解决各种疑难杂症)

    步骤 下载驱动 准备工作 进行安装 检查安装 下载驱动 首先我们需要去官网下载显卡驱动 打开浏览器,在百度搜索框中搜索:显卡驱动 下载 在 手动搜索驱动程序一栏,根据自己的显卡进行…

    Linux 2023年6月14日
    0120
  • 以STM32和FPGA为核心的多组件协调工作系统

    posted @2019-06-09 22:04 xutopia 阅读(709 ) 评论() 编辑 Original: https://www.cnblogs.com/xutopi…

    Linux 2023年6月14日
    0126
  • .NET客户端实现Redis中的管道(PipeLine)与事物(Transactions)

    序言 Redis中的管道(PipeLine)特性:简述一下就是,Redis如何从客户端一次发送多个命令,服务端到客户端如何一次性响应多个命令。 Redis使用的是客户端-服务器模型…

    Linux 2023年5月28日
    0122
  • cdn缓存顺序优先级

    posted @2022-09-26 17:28 爱折腾的大臭臭 阅读(10 ) 评论() 编辑 Original: https://www.cnblogs.com/linuxsh…

    Linux 2023年6月6日
    0119
  • redis的事件处理机制

    redis的事件处理机制 redis是单进程,单线程模型,与nginx的多进程不同,与golang的多协程也不同,”工作的工人”那么少,可那么为什么redi…

    Linux 2023年5月28日
    0112
  • 小记:音频格式转化ByPython(上)

    近日新买了个耳机,店家附送了一些周董的无损音乐资源,收到货后迫不及待的下载试听,才发现这些资源是wav格式的,导入播放器后歌名、作者、专辑等全是未知,当时想着是不是店家的资源有问题…

    Linux 2023年6月8日
    0115
  • Redis 基础

    Redis 基础 Redis 定位 – 特性 关系型数据库 特性 非关系型数据库 特性 Redis 特性 Redis 安装 – 启动 – 使用 …

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