第七节 流编辑器sed(stream editor)(7.2.1)

3,2 常用编辑命令
对文件内容的编辑命令 说明
d 删除匹配的行
p 打印匹配到的行
a文本内容 将文本内容添加到匹配行的下一行
i文本内容 将文本内容添加到匹配行的上一行
c文本内容 用文本内容替换匹配到的所有行
r 读入文件内容追加到匹配行的后面
R 读入文件一行的内容追加到匹配行的后面
w /dir/filename 将匹配行的内容另存到/dir/filename中
s /pattern/replacement/flag 根据正则表达式进行匹配,将匹配到的内容替换为replacment, flag可以指定 g(表示全局替换,默认只替换每行第一个), num1(表示)

示例:

生成测试文件

shell 复制代码
sed -e '\#adm#,$#:#@@@#g' testfile

a)删除行

shell 复制代码
sed '1d' testfile
sed '$d' testfile
sed '/root/d' testfile
sed '/root/! d' testfile

b)显示行

shell 复制代码
sed -n '1p' testfile
sed -n '$p' testfile
sed -n '/root/p' testfile
sed -n '/root/! p' testfile

c)添加,插入,替换行

shell 复制代码
sed '1ahello world' testfile 
sed '$ahello world' testfile
sed '/adm/ahello world' testfile
sed '/adm/ihello world' testfile
sed '/adm/! ihel1o\nworld' testfile

d)整合文件

shell 复制代码
vim numfile1
1
2
6
vim numfile2
2
3
4
sed '1rnumfile2' numfile1
sed '1Rnumfile2' numfile1

e)保存处理结果

shell 复制代码
sed '/root/w file' testfile
cat file
root: x: 0:0: root:/root:/bin/bash

f)字符替换

shell 复制代码
sed 's/:/##/' testfile
sed 's/:/##/2' testfile 
sed 's/:/##/g' testfile 
sed 's/^./##/g' testfile
sed 's/^\< [a-z]*[a-Z \>//' testfile 
sed 's/^\(...\)\(t\)/\2/g' testfile #元素向后引用
t: x: 0:0: root:/root:/bi n/bash
相关推荐
金銀銅鐵4 小时前
如何统计当前目录下所有 java 文件的总行数?
shell
Data_Journal11 小时前
Scrapyd:分步教程
开发语言·python·microsoft·golang·编辑器·html·iphone
逐步前行14 小时前
ESP32-S3-VSCode扩展ESP-IDF
ide·vscode·编辑器
zhangfeng11333 天前
WebIDE 容器中 AtomCode 报 `ATOMCODE_SIG_STALE` 排查实录:一次由 8 小时时钟偏差引发的“悬案”
编辑器·ai编程·atomcode
山峰哥3 天前
数据库工程与查询优化案例深度复盘‌
数据库·sql·oracle·编辑器·深度优先·宽度优先
javgo.cn4 天前
Markweave:开源 Markdown-first WYSIWYG 编辑器
开源·编辑器
YZJenny4 天前
在服务器上安装使用code-server(Web 版 VS Code)
ide·vscode·编辑器
柒号华仔4 天前
「速通Shell」Shell 编程性能优化
shell
lingran__5 天前
Linux 基础常用指令万字详解(下)|一切皆文件:重定向、管道、日志 与 Shell 原理
linux·运维·服务器·shell·管道·重定向·打包压缩
zhangfeng11335 天前
nano vim 有没有快速编辑的 方式 比如鼠标选择 ,然后快速修改 替换
编辑器·vim