当前位置:网站首页>命令行编辑器 sed 基础用法总结
命令行编辑器 sed 基础用法总结
2022-06-27 12:55:00 【用户3147702】
1. 概述
sed 是一种 linux 命令行编辑器。 当处理文本时,sed 将当前处理的行存储在临时缓冲区中,称为“模式空间”,然后 sed 命令对缓冲区中的内容进行处理,完成后将缓冲区内容输出到标准输出,接着处理以下行,直到文件尾部。 sed 主要用来自动编辑一个或多个文件,简化文件的反复操作。
2. sed 介绍
2.1. 参数
sed 命令的参数
参数 | 功能 |
---|---|
-n | 使用安静模式,只显示被处理的行 |
-e | 直接在命令列模式上进行 sed 动作的编辑 |
-f | 运行指定文件内的 sed 命令 |
-r | 支持延伸型正则的语法(否则只支持基础语法) |
-i | 直接修改文件,而不是仅输出到标准输出 |
2.2. 函数
sed 命令可以使用的函数
函数 | 功能 |
---|---|
a | 新增字符串到下一行 |
c | 取代 n1,n2 之间的行 |
d | 删除 n1,n2 之间的行 |
i | 插入字符串到上一行 |
p | 打印指定的列 |
s | 替换指定字符串为另一个指定字符串 |
3. 新增、删除行
3.1. 删除行
下面展示了如何删除 2 到 5 行。
nl /etc/passwd | sed '2,5d'
如上文所述,d 函数用于删除(为了示例清晰,首先使用 nl 命令列出了文本的行号),因此,该命令删除了 2 到 5 行。
可见,第 2 到 5 行已经被删除。
那么,只删除第 2 行呢?
nl /etc/passwd | sed '2d'
删除第 3 到最后一行呢?
nl /etc/passwd | sed '3,$d'
3.2. 新增行
下面展示了在第 2 行后加上 ’drink tea’
nl /etc/passwd | sed '2a drink tea'
那么,如何将数据添加到第 2 行前面呢?
nl /etc/passwd | sed '2i drink tea'
可是,怎么增加两行呢。
nl /etc/passwd | sed '2a Drink tea or ......\
> drink beer ?'
4. 行的替换和显示
下面展示了将 2 到 5 行替换为 ’No 2-5 line’
nl /etc/passwd | sed '2,5c No 2-5 line'
下面展示了只显示文件中 5-7 行的方法。
nl /etc/passwd | sed -n '5,7p'
5. 数据的查找
查找文件中含有 root 关键字的行。
nl /etc/passwd | sed '/root/p'
可是,好像并没有得到我们想要的结果,那是因为查找到的行和其他行混合输出,混淆了查询结果,这个时候就要通过参数 -n 指定静默模式显示。
nl /etc/passwd | sed -n '/root/p'
6. 数据的查找并删除
删除/etc/passwd所有包含root的行。
nl /etc/passwd | sed '/root/d'
7. 数据的查找并替换
nl /etc/passwd | sed -n '/root/{s/bin/replace/;p}'
首先 sed 在静默模式下查找了 root 字符串,然后执行了大括号中的代码。 sed ’s/要被取代的字串/新的字串/g’ 这个命令将字符串替换成了另一个指定字符串。
8. 多点编辑
上面的命令先执行了查找,后执行了替换命令,名执行了打印命令 p。 那么,怎么在一条命令里执行多个 sed 命令呢?
nl /etc/passwd | sed -e '3,$d' -e 's/bash/blueshell/'
-e 参数指示了多点编辑,即先删除了第 3 行到文件末尾,然后将剩余行的 bash 替换为 blueshell。
9. 直接修改文件内容
上述操作全部将结果输出到标准输出。 通过 -i 参数,可以直接修改文件内容。 但是,这项操作是非常危险的,虽然很多时候很方便,但使用之前一定要小心。
边栏推荐
- 如何下载带有超链接的图片
- Today's sleep quality record 78 points
- Pre training weekly issue 51: reconstruction pre training, zero sample automatic fine tuning, one click call opt
- Viewpager2 usage record
- Make learning pointer easier (2)
- 局域网即时通讯软件应该怎么选择
- Local visualization tool connects to redis of Alibaba cloud CentOS server
- 微服务如何拆分
- Cloud native (30) | kubernetes' app store Helm
- Bluetooth health management device based on stm32
猜你喜欢
Deeply convinced plan X - system foundation summary
Implementation of recruitment website based on SSM
ZABBIX supports nail alarm
Hardware development notes (VII): basic process of hardware development, making a USB to RS232 module (VI): creating 0603 package and associating principle graphic devices
A pang's operation record
抖音实战~公开/私密短视频互转
AGCO AI frontier promotion (6.27)
What else can PLM do?
[acwing] explanation of the 57th weekly competition
Snipaste, the world's strongest screenshot software
随机推荐
VS调试技巧
[dynamic programming] - Knapsack Problem
After the deployment is created, the pod problem handling cannot be created
Differences in perspectives of thinking
Two TCP flow control problems
A pang's operation record
硬件开发笔记(七): 硬件开发基本流程,制作一个USB转RS232的模块(六):创建0603封装并关联原理图元器件
Cool in summer
Principle of printf indefinite length parameter
大小端字节序
C语言 函数指针与回调函数
【第27天】给定一个整数 n ,打印出1到n的全排列 | 全排列模板
TCP 流控问题两则
《预训练周刊》第51期:重构预训练、零样本自动微调、一键调用OPT
今天运气不错
夏日里的清凉
It is so simple to remove the payment restrictions on VIP, YuQue and Zhihu in Baidu Library
OpenFeign服务接口调用
Full explanation of ThreadLocal source code (threadlocalmap)
Size end byte order