Linux sed editor

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

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.

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

2. Common options

-e或--expression=: 表示用指定命令来处理输入的文本文件,只有一个操作命令时可省略,一 般在执行多个操作命令使用

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

h或--help: 显示帮助。

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

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

3. Common operations


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

2. Demonstration of using sed editor

1. Print content

sed -n 'p' 55.txt   打印文件内容

Insert picture description here
Insert picture description here

sed -n '=' 55.txt  打印行号

Insert picture description here

sed -n 'l' 55.txt  打印文件中不可打印的ASCIII字符(比如结束符$、制表符\t)

Insert picture description here

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

Insert picture description here

2. Use address

The sed editor has 2 addressing modes:

  • 1. Represent the row interval in digital form
  • 2. Use text mode to filter trips
打印第一行
sed -n '1p' 55.txt  

Insert picture description here

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

Insert picture description here

打印13行
sed -n '1,3p' 55.txt

Insert picture description here

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

Insert picture description here

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

Insert picture description here

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

Insert picture description here

打印奇数行
sed -n 'p;n' 55.txt

Insert picture description here

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

Insert picture description here

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

Insert picture description here

/etc/passwd文件中过滤出含有User的行,并打印出来 区分大小写
sed -n '/User/p' /etc/passwd

Insert picture description here

/etc/passwd文件中打印以‘a’开头的行
sed -n '/^a/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

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

Insert picture description here

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

Insert picture description here

3. Delete the row

全删
sed 'd' 55.txt

Insert picture description here

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

Insert picture description here

删除24行
sed '2,4d' 55.txt

Insert picture description here

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

Insert picture description here

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

Insert picture description here

sed '/nologin$/d' /etc/passwd      #删除包含nologin字符的行

Insert picture description here

sed '/nologin$/!d' /etc/passwd     #不删除包含nologin的行

Insert picture description here

从第一个位置打开行删除功能,到第二个位置关闭行删除功能
sed '/2/,/3/d' 66.txt

Insert picture description here

4. Replace

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

Insert picture description here

将每行的所有root删除
sed 's/root//g' /etc/passwd

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

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

Insert picture description here
Insert picture description here

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

Insert picture description here
Insert picture description here

5. Insert

sed '/45/c ABC' 1.txt  #将含有45的行,替换为ABC

Insert picture description here

45换为AB,注意使用“y”时需要转换前后的字符长度必须相同
sed '/45/ y/45/AB/' 1.txt

Insert picture description here

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

Insert picture description here

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

Insert picture description here

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

Insert picture description here

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

Insert picture description here
Insert picture description here

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

Insert picture description here

Guess you like

Origin blog.csdn.net/IHBOS/article/details/114793864