Sed editor workflow and usage method

One, sed concept

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.

1. Workflow of sed editor

  • The sed editor can process the data in the data stream according to commands, which are either entered from the command line or stored in a command text file.
    The workflow of sed mainly includes three processes of reading, executing and displaying:

  • 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)

  • Execution: By default, all sed commands are executed sequentially in the pattern space. Unless the address of the line is specified, the sed command will be executed sequentially on all lines.

  • Display: Send the modified content to the output stream. After sending the data, the pattern space will be emptied. Before all the file contents are processed, the above process will be repeated until all the 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.

2. sed command format

  • Command format
格式一:
sed [选项] '操作' 文件1 [文件2] ...
格式二:
sed [选项] '选项{
操作1
操作2
...
}' 文件1 [文件2] ...

  • Common options:
-e 或--expression=:表示用指定命令来处理输入的文本文件,只有一个操作命令时可省略,一般在执行多个操作命令使用
-f 或--file=:表示用指定的脚本文件来处理输入的文本文件。
-h 或--help:显示帮助。
-n、--quiet 或 silent:禁止sed编辑器输出,但可以与p命令一起使用完成输出。
-i:直接修改目标文本文件。
  • Common operations:
s:替换,替换指定字符。
d:删除,删除选定的行。
a:增加,在当前行下面增加一行指定内容。
i:插入,在选定行上面插入一行指定内容。
c:替换,将选定行替换为指定内容。
y:字符转换,转换前后的字符长度必须相同。
p:打印,如果同时指定行,表示打印指定行;如果不指定行,则表示打印所有内容;如果有非打印字符,则以 ASCII 码输出。其通常与“-n”选项一起使用。
=:打印行号。
l(小写L):打印数据流中的文本和不可打印的ASCII字符(比如结束符$、制表符\t)

Use the sed command

1. Print content

sed -n 'p' abc.txt

Insert picture description here

#方式2的变种,在输出“'”后直接回车,然后在“>”前输出操作,最后以“ ' 文件”结尾
sed -n '
> =
> p
> ' abc.txt

Insert picture description here

sed '=' abc.txt
sed -n '=' abc.txt

Insert picture description here

sed -n 'l' abc.txt

Insert picture description here

2. Use address to find

The sed editor has 2 addressing modes:
1. Express the line interval in number form
2. Use text mode to filter out lines
Insert picture description here

#打印第一行,这里“1p”是数字1不是字母l
sed -n '1p' abc.txt 

Insert picture description here

sed -n '$p' abc.txt        #打印最后一行

Insert picture description here

sed -n '1,3p' abc.txt      #打印一到三行

Insert picture description here

sed -n '3,$p' abc.txt      #打印三到最后一行

Insert picture description here

sed -n '1, +3p' abc.txt    #打印1之后的连续3行,即1-4行

Insert picture description here

sed -n 'p;n' abc.txt       #打印奇数行; n表示移动到下一行

Insert picture description here

#从第2行开始执行n和p的操作,也是奇数行
sed -n '2,${n;p}' abc.txt

Insert picture description here

sed -n 'n;p' abc.txt       #打印偶数行

Insert picture description here

#输出前5行信息后退出,q表示退出
sed '5q' abc.txt

Insert picture description here
#Print the line starting
with'r' from the /etc/passwd file, pay attention to the case sed -n'/^r/p' /etc/passwd
Insert picture description here

sed -n ' /user/p' /etc/passwd    #//搜索包含user的行进行打印,区分大小写

Insert picture description here

#从/etc/passwd文件中打印以‘bash’结尾的行,注意大小写
sed -n '/bash$'/p  /etc/passwd

Insert picture description here

#打印文件里包含ftp和root的行
sed -n '/ftp\|root/p' /etc/passwd

Insert picture description here

#从第二行开始打印到包含nobody的行
sed -n '2,/ftp/p' /etc/passwd

Insert picture description here

sed -nr '/ro{1,}t/p' /etc/passwd    #-r表示支持正则表达式

Insert picture description here

3. Delete rows

sed 'd' abc.txt           #不指定行号,全部删除

Insert picture description here

#删除第二行
sed '2d' abc.txt

Insert picture description here

#删除2到4行
sed '2,4d' sed.txt

Insert picture description here

#删除最后一行
sed '$d' abc.txt

Insert picture description here

#删除空行
sed '/^$/d' abc.txt

Insert picture description here
Insert picture description here

#删除以c结尾的行
sed '/c$/d' abc.txt

Insert picture description here

#除了c结尾的都删了,“!”表示取反操作
sed '/c$/!d' abc.txt

Insert picture description here

#从第一个位置打开行删除功能,到第二个位置关闭行删除功能,按行删除(慎用)
sed '/1/,/c/d' sed.txt

Insert picture description here

Insert picture description here

4. Replace

格式:
sed [选项] '行范围 s/旧字符串/新字符串/替换标记'

4种替换标记:
数字:表明新字符串将替换第几处匹配的地方
g:表明新字符串将会替换所有匹配的地方
p:打印与替换命令匹配的行,与-n一起使用
w 文件:将替换的结果写到文件中
sed -n 's/root/admin/p' /etc/passwd    #将匹配行的第一个root更改为admin

Insert picture description here

sed -n 's/root/admin/2p' /etc/passwd   #将匹配行的第二个root更改为admin

Insert picture description here

sed -n 's/root/admin/gp' /etc/passwd   #将匹配行的所有root更改为admin 

Insert picture description here

sed '1,20 s/^/#/' /etc/passwd          #1-20行行首添加#号

Insert picture description here

#1-5 行的结尾添加#号
sed '/^root/ s/$/#/' /etc/passwd

Insert picture description here

sed -f 123.sed 123.txt                #-f以指定的脚本文件来处理输入文件

Insert picture description here
Insert picture description here
Insert picture description here

#将/etc/passwd中的1-20行输出保存到out.txt文件中
sed -n '1,5w out.txt' /etc/passwd
#将/etc/passwd 中的1-20行的开头添加#后保存到out.txt文件中
sed -n '1,20 s/^/#/w out1.txt' /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		#使用"!"、"#"、"@"作为字符串分隔符也可实现

#将内容有a的行,整行替换为ABCDE
sed '/a/c ABCDE' abc.txt

Insert picture description here

#将内容bc装换为23,注意使用“y”时需要转换前后的字符长度必须相同
sed '/bc/ y/bc/23/' abc.txt

5. Insert

#在1-3行,每行的下面都插入b
sed '1,3a b' 123.txt

Insert picture description here

#在1-3行,每行的下面都插入b
sed '1,3i c' 123.txt

Insert picture description here

 #在第六行后导入该文件内容
sed '6r /root/1234.txt'  123.txt

Insert picture description here

#将包含root的行剪切到末尾,H表示复制到剪切板,G表示粘贴到指定行后
sed '/abc/{H;d};$G' sed.txt

Insert picture description here

Guess you like

Origin blog.csdn.net/zhangyuebk/article/details/114818365