当前位置:网站首页>命令行编辑器 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 参数,可以直接修改文件内容。 但是,这项操作是非常危险的,虽然很多时候很方便,但使用之前一定要小心。
边栏推荐
猜你喜欢

Make learning pointer easier (2)

Full explanation of ThreadLocal source code (threadlocalmap)

Cloud native (30) | kubernetes' app store Helm

Details of istio micro service governance grid traffic management core resource controller

A pang's operation record

IJCAI 2022 | greatly improve the effect of zero sample learning method with one line of code. Nanjing Institute of Technology & Oxford proposed the plug and play classifier module

Hardware development notes (VII): basic process of hardware development, making a USB to RS232 module (VI): creating 0603 package and associating principle graphic devices

Explore tidb lightning source code to solve the found bugs

本地可视化工具连接阿里云centOS服务器的redis

让学指针变得更简单(二)
随机推荐
What is low code for digital Nova? What is no code
Firewall foundation Huawei H3C firewall web page login
阿胖的操作记录
每日刷题记录 (六)
基于JSP实现医院病历管理系统
Three traversal methods of binary tree
Cloud native (30) | kubernetes' app store Helm
Make learning pointer easier (1)
Viewpager2 usage record
Make learning pointer easier (2)
Size end byte order
Neo4j: basic introduction (I) installation and use
Centos7 command line installation Oracle11g
Snipaste, the world's strongest screenshot software
[medical segmentation] unet3+
7 killer JS lines of code
快讯:华为启动鸿蒙开发者大赛;腾讯会议发布“万室如意”计划
Cool in summer
Implementation of recruitment website based on SSM
[day 27] given an integer n, print out the full permutation from 1 to n | Full Permutation template