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)

大家都在看

  • 【数学建模相关】matplotlib画多个子图(散点图为例 左右对照画图)

    @ 例题 例图 代码展示 例题 乙醇偶合制备 C4 烯烃 C4 烯烃广泛应用于化工产品及医药的生产,乙醇是生产制备 C4 烯烃的原料。 在制备过程中,催化剂组合(即:Co 负载量、…

    Linux 2023年6月8日
    0101
  • 抓到 Netty 一个隐藏很深的内存泄露 Bug | 详解 Recycler 对象池的精妙设计与实现

    欢迎关注公众号:bin的技术小屋,如果大家在看文章的时候发现图片加载不了,可以到公众号查看原文 本系列Netty源码解析文章基于 4.1.56.Final版本 最近在 Review…

    Linux 2023年6月6日
    0121
  • U盘如何安装centos7系统?U盘安装centos7详细安装图解教程

    一般来说,无论是Windows还是linux的IOS系统镜像,我们都可以使用UltraIOS(软碟通)这款软件制作U盘启动工具,不过考虑到不少小白依然不会如何操作,所以今天考虑写一…

    Linux 2023年6月8日
    0108
  • 阿里云-docker上安装redis

    1、取最新版的 Redis 镜像 这里我们拉取官方的最新版本的镜像: $ docker pull redis:latest 2、查看本地镜像 使用以下命令来查看是否已安装了 red…

    Linux 2023年5月28日
    086
  • Tomcat下载安装以及配置方法

    Tomcat环境变量配置方法 注意一定要在java环境配置成功之后再来配置tomcat。我这里仅展现在Windows系统下载的安装方法 Tomcat下载地址如下: https://…

    Linux 2023年6月7日
    0101
  • ArchLinux安装-2022-01-12

    这篇教程,是我基于B站up住theCW的视频教程整理的,其中添加了一些我在安装n次之后的经验(虽然失败过几次,但我现在安装不会再出差错,所以请放心的看此教程) 当然,我认为theC…

    Linux 2023年6月13日
    0103
  • Log4j 日志框架

    Log4j(Log for java)是 Apache 的一个开源项目,通过使用 Log4j,可以控制日志信息输送的目的地是控制台或文件等,也可以控制每一条日志的输出格式。通过定义…

    Linux 2023年6月8日
    0139
  • Typora+gitee+picgo搭建本地博客环境

    前言 现在现成的博客平台数不胜数,如果选择服务器+自建博客也有很多方案,可是本人对相片、博客等信息数据总是有本地和互联网各存储一遍才放心的习惯,所以作者本人选择了csdn、博客园、…

    Linux 2023年6月7日
    0125
  • rtmp和rtsp的区别

    刚刚接触到视频推流,搞不清楚rtmp和rtsp到底有什么区别 1.视频传输 RTSP+RTP主要用于IPTV,原因是传输数据使用的是UDP,在网络环境比较稳定的情况下,传输效率是比…

    Linux 2023年6月8日
    0100
  • django基础

    目录 django三板斧 登录功能 静态文件 request对象方法 pycharm链接MySQL django链接MySQL django orm简介 orm基本使用 *orm数…

    Linux 2023年6月7日
    0125
  • JDK 环境变量配置

    一、环境准备 Windows10 jdk-9.0.1 二、下载合适的JDK版本,安装JDK 三、环境变量配置 1、右键桌面上”我的电脑”>>&#…

    Linux 2023年6月8日
    090
  • 2021年3月-第02阶段-前端基础-Flex 伸缩布局-移动WEB开发_流式布局

    移动web开发流式布局 1.0 移动端基础 1.1 浏览器现状 PC端常见浏览器:360浏览器、谷歌浏览器、火狐浏览器、QQ浏览器、百度浏览器、搜狗浏览器、IE浏览器。 移动端常见…

    Linux 2023年6月8日
    0129
  • PhpCms V9调用指定栏目子栏目文章的方法

    PhpCms V9调用指定栏目子栏目文章的方法 第一种,直接写父类id {pc:content action=”lists” catid=”父类…

    Linux 2023年6月13日
    0112
  • 多级缓存-redis缓存预热

    冷启动:服务刚刚启动时,Redis中并没有缓存,如果所有商品数据都在第一次查询时添加缓存,可能会给数据库带来较大压力。 缓存预热:在实际开发中,我们可以利用大数据统计用户访问的热点…

    Linux 2023年5月28日
    092
  • SpringBoot中通过AOP整合日志文件

    1.SpringBoot中通过AOP整合日志文件 1. 导入相关的依赖 org.springframework.boot spring-boot-starter org.sprin…

    Linux 2023年6月14日
    0102
  • 利用Tensorboard可视化模型、数据和训练过程

    在60分钟闪电战中,我们像你展示了如何加载数据,通过为我们定义的 nn.Module的子类的model提供数据,在训练集上训练模型,在测试集上测试模型。为了了解发生了什么,我们在模…

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