Sed editor for shell script

sed editor

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 entered from the command line or stored in a command text file.

sed workflow

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

  • Before all the contents of the file 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 the output is stored by redirection.

sed 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)
Example:

Print content

Insert picture description here

Print line number

Insert picture description here

Insert picture description here

Insert picture description here

You can also use Enter directly after outputting "'", and then output the operation before ">", and finally end with "'file"
sed -n '
> =
> p
>' sed.txt

Insert picture description here

Print ASCII characters

Insert picture description here

Use address

The sed editor has two addressing modes:
expressing the line interval in number form
and filtering out the line in text mode

Represent the row interval in numeric form

Insert picture description here

#印奇数行
#The first step: sed reads the first line, and p prints it out
#The second step: where n means move to the next line (this time is the second line)
#3rd step: the sed command ends, switch again To the next line (the third line at this time)
#4th step: Repeat the steps of one, two and three until the last line
sed -n'p;n' sed.txt #Print
even-numbered lines, similar to the above steps
sed -n 'n;p' sed.txt

Insert picture description here

Use text mode to filter out lines

Insert picture description here

Delete row

Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here

replace

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

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

Insert picture description here

cat sed.sh
vim script.sed      #创建一个用于sed命令的脚本,以sed结尾方便辨识           
s/o/66/             #将o替换成66,下面以此类推
s/t/44/
sed -f script.sed sed.txt

Insert picture description here

#将/etc/passwd中的1-3行输出保存到out.txt文件中
sed -n '1,3w out.txt' /etc/passwd
#将/etc/passwd 中的1-3行的开头添加#后保存到out.txt文件中
sed -n '1,3 s/^/#/w out.txt' /etc/passwd

Insert picture description here

将/bin/bash替换成/bin/csh
#将/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

#将内容有a的行,整行替换为ABC
sed '/o/c ZXC' sed.txt
#将内容ee转换为12,注意使用“y”时需要转换前后的字符长度必须相同
sed '/ee/ y/ee/12/' sed.sh

Insert picture description here

insert

#在1-3行,每行的下面都插入ZXC
sed '1,3a ZXC' sed.sh
#在第一行的上面插入ZXC
sed '1i ZXC' sed.sh

Insert picture description here

#将带有/root的行剪切到末尾,H表示复制到剪切板,d表示删除,G表示粘贴到指定行之后
sed '/one/{H;d};$G' sed.sh 
#将1,2行复制到3和4行的下面,注意第三行和第四行都会复制1,2两行的内容
sed '1,2H;3,4G' sed.sh

Insert picture description here

Guess you like

Origin blog.csdn.net/shengmodizu/article/details/114821683