Secretly learn shell script sed editor

sed editor

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

Second, the workflow of sed

  • 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 cleared. Before all file contents are processed, the above process will be repeated until all contents are processed.
  • 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.

Three, 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)

Four, use the sed command

1. Print content

sed -n 'p' sed.txt

Insert picture description here

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

Insert picture description here

sed -n 'l' sed.txt

Insert picture description here

sed -n '=;p' sed.txt
sed -n -e '=' -e 'p' sed.txt 

Insert picture description here

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

Insert picture description here

2. Use address

  • The sed editor has 2 addressing modes:
    • Represent the row interval in numeric form
    • Use text mode to filter out lines
      Insert picture description here
#打印第一行,这里“1p”是数字1不是字母l
sed -n '1p' sed.txt 

Insert picture description here

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

Insert picture description here

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

Insert picture description here

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

Insert picture description here

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

Insert picture description here

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

Insert picture description here

#打印奇数行
#第一步:sed读取第一行,p打印出来
#第二步:这里n表示移动到下一行(此时是第二行)
#第三步:sed命令结束,再次切换到下一行(此时是第三行)
#第四步:重复一二三的步骤,直至最后一行
sed -n 'p;n' sed.txt
#打印偶数行,和上面的步骤差不多
sed -n 'n;p' sed.txt

Insert picture description here

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

Insert picture description here

#在/etc/passwd文件中过滤出含有root的行,并打印出来,注意大小写
sed -n '/root/p' /etc/passwd

Insert picture description here

#从/etc/passwd文件中打印以‘r’开头的行,注意大小写
sed -n '/^r'/p  /etc/passwd

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,/nobody/p' /etc/passwd

Insert picture description here

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

Insert picture description here

3. Delete rows

#全删
sed 'd' sed.txt

Insert picture description here

#删除第三行
sed '3d' sed.txt

Insert picture description here

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

Insert picture description here

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

Insert picture description here

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

Insert picture description here

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

Insert picture description here

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

Insert picture description here

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

Insert picture description here

4. Replace

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

4种替换标记:
数字:表明新字符串将替换第几处匹配的地方
g:表明新字符串将会替换所有匹配的地方
p:打印与替换命令匹配的行,与-n一起使用
w 文件:将替换的结果写到文件中

Insert picture description here

#将每行的第一个root替换成admin
sed -n 's/root/admin/p' /etc/passwd

Insert picture description here

#将每行的第二个root替换成admin
sed -n 's/root/admin/2p' /etc/passwd

Insert picture description here

#将每行的所有root替换成admin
sed -n 's/root/admin/gp' /etc/passwd
#将每行的所有root删除
sed 's/root//g' /etc/passwd

Insert picture description here
Insert picture description here

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

Insert picture description here

#在以root开头的行的结尾添加#号
sed '/^root/ s/$/#/' /etc/passwd

Insert picture description here

vim script.sed      #创建一个用于sed命令的脚本,以sed结尾方便辨识           
s/1/11/             #将1替换成11,下面以此类推
s/a/zb/
s/555/888/
sed -f script.sed sed.txt

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

Insert picture description here
Insert picture description here

#将/bin/bash替换成/bin/csh
sed -n 's/\/bin\/bash/\/bin\/csh/p' /etc/passwd #“\”是转义字符,用来取消“/”的特殊意义
sed -n 's!/bin/bash!/bin/csh!p' /etc/passwd		#使用“!”作为字符串分隔符

Insert picture description here
Insert picture description here

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

Insert picture description here

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

Insert picture description here

5. Insert

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

Insert picture description here

#在第一行的上面插入ABC
sed '1i ABC' sed.txt

Insert picture description here

 #在第五行后导入该文件内容
sed '5r /root/bianji/sed1.txt' sed.txt

Insert picture description here

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

Insert picture description here

#将1,2行复制到3和4行的下面,注意第三行和第四行都会复制1,2两行的内容
sed '1,2H;3,4G' sed.txt

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_51326240/article/details/111766857