1、sed介绍
sed(Stream Editor)是一种流文本编辑器,它可以在输入文件中执行基本文本转换。它可以将文本中的字符、行和单词进行删除、替换和添加等操作,而无需更改原始文件。
sed语法:
sed [选项] 'script' input_file
script:由一个或多个sed编辑命令组成的脚本。每个编辑命令可以包括地址或正则表达式,用于指定应用命令的行。
input_file:要处理的输入文件。
用法: sed [选项]... {脚本(如果没有其他脚本)} [输入文件]...
-n, --quiet, --silent
取消自动打印模式空间
--debug
对程序运行进行标注
-e 脚本, --expression=脚本
添加“脚本”到程序的运行列表
-f 脚本文件, --file=脚本文件
添加“脚本文件”到程序的运行列表
--follow-symlinks
直接修改文件时跟随软链接
-i[扩展名], --in-place[=扩展名]
直接修改文件(如果指定扩展名则备份文件)
-l N, --line-length=N
指定“l”命令的换行期望长度
--posix
关闭所有 GNU 扩展
-E, -r, --regexp-extended
在脚本中使用扩展正则表达式
(为保证可移植性使用 POSIX -E)。
-s, --separate
将输入文件视为各个独立的文件而不是单个
长的连续输入流。
--sandbox
在沙盒模式中进行操作(禁用 e/r/w 命令)。
-u, --unbuffered
从输入文件读取最少的数据,更频繁的刷新输出
-z, --null-data
使用 NUL 字符分隔各行
--help 打印帮助并退出
--version 输出版本信息并退出
-
参数选项:
-e
:允许在同一行上指定多个编辑命令。-n
:取消自动打印模式空间的内容。-i
:直接修改文件内容,而不是在标准输出中显示结果(谨慎使用)。
2、常用的sed使用示例
(1)替换文本
这将在1.txt中将所有的“is”替换为“iis”。
sed 's/is/iis/g' 1.txt
ubuntu@VM-12-3-ubuntu:~$ cat 1.txt
this is test
yes or no
create a xptxt is
ubuntu@VM-12-3-ubuntu:~$ sed 's/is/iis/g' 1.txt
thiis iis test
yes or no
create a xptxt iis
ubuntu@VM-12-3-ubuntu:~$
如下,加了-i参数是直接永久修改原始文件,不加-i是直接显示修改效果,确定更改之前最好先备份!
ubuntu@VM-12-3-ubuntu:~$ cat 1.txt
this is test
yes or no
create a xptxt is
ubuntu@VM-12-3-ubuntu:~$ sed -i 's/is/iis/g' 1.txt
ubuntu@VM-12-3-ubuntu:~$ cat 1.txt
thiis iis test
yes or no
create a xptxt iis
ubuntu@VM-12-3-ubuntu:~$
提示:在macOS系统中,你需要指定一个空字符串作为备份文件的后缀名,否则会报错。例如
sed -i '' 's/old/new/g' file.txt
(2)删除匹配行
sed '/yes/d' 1.txt
# 原文本内容
ubuntu@VM-12-3-ubuntu:~$ cat 1.txt
thiis iis test
yes or no
create a xptxt iis
# 删除包含“yes”的所有行
ubuntu@VM-12-3-ubuntu:~$ sed '/yes/d' 1.txt
thiis iis test
create a xptxt iis
ubuntu@VM-12-3-ubuntu:~$
(3)插入文本到特定行之前
sed '2i\ new line' 1.txt
# 原文本内容
ubuntu@VM-12-3-ubuntu:~$ cat 1.txt
thiis iis test
yes or no
create a xptxt iis
# 将在第3行之前插入“new line”
ubuntu@VM-12-3-ubuntu:~$ sed '2i\new line' 1.txt
thiis iis test
new line
yes or no
create a xptxt iis
ubuntu@VM-12-3-ubuntu:~$
(4)打印特定行
sed -n '3p' 1.txt
# 原文本内容
ubuntu@VM-12-3-ubuntu:~$ cat 1.txt
thiis iis test
yes or no
create a xptxt iis
# 只打印第3行
ubuntu@VM-12-3-ubuntu:~$ sed -n '3p' 1.txt
create a xptxt iis
ubuntu@VM-12-3-ubuntu:~$
(5)多个命令组合
sed -e 's/yes/nono/g' -e '2,4d' 1.txt
# 原文本内容
ubuntu@VM-12-3-ubuntu:~$ cat 1.txt
thiis iis test
yes or no
create a xptxt iis
qwe
rtp and res
# 将先进行替换,然后删除第2到第4行的内容
ubuntu@VM-12-3-ubuntu:~$ sed -e 's/yes/nono/g' -e '2,4d' 1.txt
thiis iis test
rtp and res
ubuntu@VM-12-3-ubuntu:~$
(6)在匹配行后面添加文本
sed '/res/a\new line' 1.txt
# 原文本内容
ubuntu@VM-12-3-ubuntu:~$ cat 1.txt
thiis iis test
yes or no
create a xptxt iis
qwe
rtp and res
# 在包含“res”的行后面添加“new line”
ubuntu@VM-12-3-ubuntu:~$ sed '/res/a\new line' 1.txt
thiis iis test
yes or no
create a xptxt iis
qwe
rtp and res
new line
ubuntu@VM-12-3-ubuntu:~$
(7)替换指定行的内容
sed '3c\11111' 1.txt
# 原文本内容
ubuntu@VM-12-3-ubuntu:~$ cat 1.txt
thiis iis test
yes or no
create a xptxt iis
qwe
rtp and res
将第3行的内容替换为“11111”:
ubuntu@VM-12-3-ubuntu:~$ sed '3c\11111' 1.txt
thiis iis test
yes or no
11111
qwe
rtp and res
ubuntu@VM-12-3-ubuntu:~$
(8)如何将 test1 test2 test3 改成"test1","test2","test3"
ubuntu@VM-12-3-ubuntu:~$ cat 2.txt
test1 test2 test3
ubuntu@VM-12-3-ubuntu:~$ sed -i 's/^/"/' 2.txt |sed -i 's/ /","/g' 2.txt |sed -i 's/$/"/' 2.txt
ubuntu@VM-12-3-ubuntu:~$ cat 2.txt
test1","test2","test3"
注意:以上都没有使用-i参数直接修改文件内容,而是将修改后的结果输出到标准输出,根据情况进行使用修改!!