shell script sed command

Sed editor overview

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.

sed editor workflow

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

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.

sed command format

sed -e '格式' 文件1 文件2 ...
sed -n -e ‘操作’ 文件1 文件2 ...
sed -f 脚本文件 文件1 文件2 ...
sed -i -e '操作' 文件1 文件2 ...

sed -e ' n {
操作1
操作2
...
} ' 文件1 文件2
**常用选项**
-e或- -expression=: 表示用指定命令来处理输入的文本文件,只有一个操作命令时可省略,一 般在执行多个操作命令使用

f或- -file=: 表示用指定的脚本文件来处理输入的文本文件。

h或- -help: 显示帮助。

-n、- -quiet或silent:禁止sed编辑器输出,但可以与p命令一起使用完成输出。

-i: 直接修改目标文本文件。

sed common operations

s:替换,替换指定字符。
d:删除,删除选定的行。
a:增加,在当前行下面增加一行指定内容。
i:插入,在选定行上面插入一行指定内容。
c:替换,将选定行替换为指定内容。
y:字符转换,转换前后的字符长度必须相同。
p:打印,如果同时指定行,表示打印指定行;如果不指定行,则表示打印所有内容;如果有非打印字符,则以 ASCII 码输出。其通常与“-n”选项一起使用。
=:打印行号。
l(小写L):打印数据流中的文本和不可打印的ASCII字符(比如结束符$、制表符\t)

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

Addressing mode (check)

**sed编辑器有2种寻址方式**
1、以数字形式表示行区间
2、用文本模式来过滤出行
sed -n '1p' 1.txt        #打印第一行
sed -n '$p' 1.txt       #打印最后一行

Insert picture description here

sed -n '1,3p' 1.txt      #打印一到三行
sed -n '3,$p' 1.txt     #打印三到最后一行
sed -n '$=;$p' 1.txt    #打印最后一行显示行号

Insert picture description here

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

Insert picture description here

sed -n 'p;n' 1.txt       #打印奇数行; n表示移动到下一行
sed -n 'n;p' 1.txt       #打印偶数行
sed -n '2,${n;p}' 1.txt       #从第二行开始,n移动下一行,p打印,表示打印奇数行
sed -n '2,${p;n}' 1.txt       #从第二行开始,n移动下一行,p打印,表示打印偶数行

Insert picture description here

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

Insert picture description here

sed -n ' /^a/p' /etc/passwd     #搜索以a开头的行进行打印

Insert picture description here

sed -n '/bash$/p' /etc/passwd       #打印以bash结尾的行

Insert picture description here

sed -n '/a\|b/p' /etc/passwd   #搜索包含a或者b的行进行打印

Insert picture description here

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

Insert picture description here

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

Insert picture description here

delete

sed 'd' 1.txt          #不指定行号,全部删除
sed '3d' 1.txt          #删除第三行
sed '2,4d' 1.txt       #删除2-4行
sed '$d' 1.txt         #删除最后一行

Insert picture description here
Insert picture description here

sed '/^$/d' 1.txt      #删除空行
sed '/nologin$/d' /etc/passwd      #删除包含nologin字符的行
sed '/nologin$/!d' /etc/passwd     #不删除包含nologin的行

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

sed '/2/,/4/d'  1.txt #遇到2开启删除,遇到4关闭删除

Insert picture description here

Replace (change)

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

The passwd file contains the root line
Insert picture description here

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

Insert picture description here

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

Insert picture description here

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

Insert picture description here

sed -ne ‘1,10 s/^/#/p’ -ne ‘1,10 =’ /etc/passwd #1-10行行首添加#号

Insert picture description here

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

Insert picture description here

Insert (increase)

sed '1c 10' 1.txt #第1行,替换为10

Insert picture description here

sed ‘5i abc’ 1.txt #在第五行,行上插入abc
sed ‘5a abc’ 1.txt #在第五行,行下插入abc

Insert picture description here

sed -r '5a abc\n123' 1.txt #在第五行行下添加abc和123

Insert picture description here

sed '5r /etc/resolv.conf' 1.txt  #在第五行后导入该文件内容  r代表读取 

Insert picture description here

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

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/MQ107/article/details/114822657