sed text processing tool
One, sed editor
Sed is a stream editor, the stream editor will edit the data stream based on a set of rules provided in advance before the editor processes the data.
The sed editor can process the data in the data stream according to commands, which are either input from the command line or stored in a command text file.
1.1 sed workflow
1. Read: sed reads a line of content from the input stream (file, pipe, standard input) and stores it in a temporary buffer (also known as pattern space)
2. Execution: By default, all sed commands are executed sequentially in the pattern space. Unless the address of the row is specified, the sed command will be executed sequentially on all rows.
3. Display: Send the modified content to the output stream. After sending the data, the pattern space will be cleared.
Before all file contents are processed, the above process will be repeated until all contents are processed.
Note: By default, all sed commands are executed in the pattern space, so the input file will not change in any way, unless redirection is used to store the output.
1.2 Command format
sed -e '操作' 文件1 文件2 ... 就是普通的命令,但如果和下面的-n(其他的选项)一起使用时,不能省略
sed -n-e '操作' 文件1 文件2.. 不会输出结果
sed -f 脚本文件 文件1 文件2... 执行命令在脚本文件里,对后面的文件1、2...内容进行处理
sed -i -e '操作' 文件1 文件2... 会对文本进行直接的修改
sed -e 'n{
操作1
操作2
...
}' 文件1 文件2...
1.3 Common options
-e或--expression=:表示用指定命令来处理输入的文本文件,只有一个操作命令时可省略,一般在执行多个操作命令使用
-f或--file=:表示用指定的脚本文件来处理输入的文本文件。
-h或--help:显示帮助。
-n,--quiet或silent:禁止sed编辑器输出,但可以与p命令一起使用完成输出。
-i:直接修改目标文本文件。
1.4 Common operations
s:替换,替换指定字符。
d:删除,删除选定的行。
a:增加,在当前行下面增加一行指定内容。
i:插入,在选定行上面插入一行指定内容。
c:替换,将选定行替换为指定内容。
y:字符转换,转换前后的字符长度必须相同。
p:打印,如果同时指定行,表示打印指定行:如果不指定行,则表示打印所有内容;如果有非打印字符,则以ASCII码输出。其通常与"-n"选项一起使用。
=:打印行号。
l (小写L):打印数据流中的文本和不可打印的ASCIl字符(比如结束符$、制表符\t)
2. How to use sed editor
2.1 Use address
2.1.1 The sed editor has 2 addressing modes
1. Represent the line interval in number form
2. Use text mode to filter out the line
sed -n '1p' sed.txt 仅输出第一行内容
sed -n '$p' sed.txt 仅输出最后一行内容
sed -n '1,3p' sed.txt 打印1-3行内容
sed -n '1,+3p' sed.txt 打印1-4行内容(第一行和后面连续的3行)
sed '5q' sed.txt
sed -n 'p;n' sed.txt 打印奇数行,n表示移动到下一行
sed -n 'n;p' sed.txt 打印偶数行
sed -n '/user/p' /etc/passwd 打印含有user字符串的行 区分大小写的
sed -n '/^a/p' /etc/passwd 打印以a开头的行
sed -n '/bash$/p' /etc/passwd 打印/etc/passwd中以bash结尾的内容
sed -n '/ftp\|root/p' /etc/passwd
打印含有ftp或者root的内容
\是为了让|赋有意义,|或者的意思
sed -n '2,/nobody/p' /etc/passwd 打印第二行到流水遇到的第一个nobody
sed -n '2,/nobody/=' /etc/passwd = 表示输出行号,不打印内容
sed -n '2,/nobody/=;p' /etc/passwd 2-nobody输出行号,p打印所有内容
sed -nr '/ro{1,}t/p' /etc/passwd -r 表示支持正则表达式
ro{
1,}表示前面的子表达式o不少于1个,所以可以是rot、root、rooot、ro...t
2.2 Replace
行范围 s/旧字符串/新字符串/替换标记
2.2.1 Replacement mark
数字:表明新字符串将替换第几处匹配的地方
g:表明新字符串将会替换所有匹配的地方
p:打印与替换命令匹配的行,与-n一起使用
w 文件:将替换的结果写到文件中
sed -n 's/root/admin/p' /etc/passwd 将root替换成admin 只替换遇到的第一个字符串
sed -n 's/root/admin/2p' /etc/passwd 将流水遇到的第2个root替换成admin
sed -n 's/root/admin/gp' /etc/passwd 将所有的root都替换成admin
sed 's/root//g' /etc/passwd 将所有的root替换成空格,g表示所有
sed '1,20 s/^/#/' /etc/passwd 1-20行在每行开头加入#
sed '/^root/ s/$/#/' /etc/passwd 查找以root开头的行,在末尾添加#
sed -n '/^root/ s/$/#/p' /etc/passwd 相比上面的不会打印多余的内容,只打印修改的
sed -f scripts.sed sed.txt 用指定的脚本文件处理输入的文本文件
scripts.sed 脚本文件
sed.txt 文本文件
sed '1,20w out.txt' /etc/passwd 将查找的1-20行直接输出到文本文件里
sed '1,20 s/^/#/w out.txt' /etc/passwd 将文件out.txt -1-20行的开头加上#
sed -n 's/\/bin\/bash/\/bin\/csh/p' /etc/passwd 将/bin/bash替换成/bin/csh
这边的\是转义字符,为了使/具有符号意义
sed -n 's/ (\/bin\/bash) / (\/bin\/csh) /p' /etc/passwd
sed -n 's!/bin/bash!/bin/csh!p' /etc/passwd 使用转义字符作为字符串分隔符
2.3 Delete mark
sed 'd' sed.txt 全部删除
sed '3d' sed.txt 删除第3行
sed '2,4d' sed.txt 删除2-4行
sed '$d' sed.txt 删除最后一行
sed '/^$/d' testfile 删除空行
sed '/nologin$/d' /etc/passwd 删除以nologin结尾的行
sed '/nologin$/!d' /etc/passwd 删除 不 以nologin结尾的行
sed '/2/,/3/d' sed.txt 删除字符2所在行到字符3所在行之间的所有行
第1个字符打开删除服务,第2个字符关闭删除服务
sed '/3/,/7/d' sed.txt 同上
2.4 Insert mark
sed '/45/c ABC' sed.txt 将45替换成ABC
sed '/45/ y/45/AB/' sed.txt 在字符45所在的行,将45替换成AB,前后长度要一致
sed '1,3a ABC' sed.txt 1-3行每行下面都添加ABC a表示在指定行下面插入
sed '1i ABC' sed.txt 在第1行前插入ABC i表示在指定行上面插入
sed '5r /etc/sed1.txt' sed.txt 在第5行后面插入/etc/sed1.txt文本文件中的内容
sed '/root/{H;d};$G' /etc/passwd 将含有root字符串的行剪切并粘贴至行尾
H表示复制指定内容;d表示删除指定内容;{
H;d}表示剪切
G表示粘贴到指定行后
sed '1,2H;3,4G' sed.txt 在第3 行、第4行后面都复制插入1-2行