sed tool (editing script artifact!)

sed tool (editing script artifact!)


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

Three, sed command format

  • Command format
格式1:sed  -e  ‘操作’  文件1 文件2 ......

格式2:sed  -n -e  '操作'   文件1  文件2 .......

格式3:sed  -f  脚本文件   文件1  文件2 .......

格式4:sed  -i  -e  '操作'  文件1  文件2.......

格式5:
sed  -e  'n{
操作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)

Fourth, the use of the sed command

1. Print content

① When -n is not added, the sed editor will also print once

[root@localhost ~] # vim shuzi.txt
[root@localhost ~] # cat shuzi.txt 
one
two
three
four
five
[root@localhost ~] # sed 'p' shuzi.txt 
one
one
two
two
three
three
four
four
five
five
[root@localhost ~] # sed -n 'p' shuzi.txt 
one
two
three
four
five

②Print the line number, if you add -n, it will limit the output of the sed editor, and you will not see the content

[root@localhost ~] # sed '=' shuzi.txt
1
one
2
two
3
three
4
four
5
five
[root@localhost ~] # sed -n '=' shuzi.txt
1
2
3
4
5

③Execute multiple operation commands by using ";" or -e

[root@localhost ~] # sed -n '=;p' shuzi.txt
1
one
2
two
3
three
4
four
5
five
[root@localhost ~] # sed -n -e '=' -e 'p' shuzi.txt
1
one
2
two
3
three
4
four
5
five

Supplement: Enter directly after the single quotation mark """

[root@localhost ~] # sed -n '
> =
> p
> ' shuzi.txt
1
one
2
two
3
three
4
four
5
five

④Print ASCII characters

[root@localhost ~] # sed -n 'l' shuzi.txt 
one$
two$
three$
four$
five$

2. Use address

  • The sed editor has 2 addressing modes:
    • Represent the row interval in numeric form
    • Use text mode to filter out lines

①Print line

[root@localhost ~] # sed -n '1p' shuzi.txt #打印第一行
one
[root@localhost ~] # sed -n '$p' shuzi.txt #打印最后一行
five
[root@localhost ~] # sed -n '1,3p' shuzi.txt #打印1到3行
one
two
three
[root@localhost ~] # sed -n '3,$p' shuzi.txt #打印3到最后一行
three
four
five
[root@localhost ~] # sed -n '1,+3p' shuzi.txt #打印包括1之后的连续3行
one
two
three
four
[root@localhost ~] # sed '5q' shuzi.txt #输出5行以后退出
one
two
three
four
five

②Print odd-numbered lines
Step 1: sed reads the first line, and p prints it out.
Step 2: Here n means move to the next line (the second line at this time).
Step 3: The sed command ends, switch to the next line again (At this time, it is the third line)
Step 4: Repeat the steps one, two and three until the last line

Printing even-numbered lines is'n;p' is the same as printing odd-numbered lines

[root@localhost ~] # sed -n 'p;n' shuzi.txt #打印奇数行
one
three
five
[root@localhost ~] # sed -n 'n;p' shuzi.txt #打印偶数行
two
four
[root@localhost ~] # sed -n '2,${n;p}' shuzi.txt #打印从2开始的奇数行
three
five

③Filter and print out relevant lines

 #在/etc/passwd文件中过滤出含有root的行,并打印出来
[root@localhost ~] # sed -n '/root/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

#从/etc/passwd文件中打印以‘r’开头的行
[root@localhost ~] # sed -n '/^r'/p  /etc/passwd 
root:x:0:0:root:/root:/bin/bash
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin

#从/etc/passwd文件中打印以‘bash’结尾的行
[root@localhost ~] # sed -n '/bash$'/p  /etc/passwd
root:x:0:0:root:/root:/bin/bash
muhonghuan:x:1000:1000:muhonghuan:/home/muhonghuan:/bin/bash

#打印文件里包含ftp或root的行
[root@localhost ~] # sed -n '/ftp\|root/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

#从第二行开始打印到包含ftp的行
[root@localhost ~] # sed -n '2,/ftp/p' /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

#-r表示支持正则表达式
[root@localhost ~] # sed -nr '/ro{1,}t/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

3. Delete the row

①Delete the specified line

[root@localhost ~] # cat shuzi.txt #查看文件
one
two
three
four
five

#全删后输出,不影响原文件,因为没有重定向
[root@localhost ~] # sed 'd' shuzi.txt 

#删除第3行
[root@localhost ~] # sed '3d' shuzi.txt 
one
two
four
five

#删除2到4行
[root@localhost ~] # sed '2,4d' shuzi.txt 
one
five

#删除最后一行
[root@localhost ~] # sed '$d' shuzi.txt 
one
two
three
four

②Filter and delete the specified row

[root@localhost ~] # cat shuzi.txt 
one

two
three


four
five

#删除空行
[root@localhost ~] # sed '/^$/d' shuzi.txt 
one
two
three
four
five
[root@localhost ~] # cat shuzi.txt 
one
two
three
four
five

#删除以e结尾的行
[root@localhost ~] # sed '/e$/d' shuzi.txt 
two
four
#删除不以e结尾的行
[root@localhost ~] # sed '/e$/!d' shuzi.txt 
one
three
five

③Use the following with caution

That is to turn on the row delete function from the first position, turn off the row delete function to the second position, and delete by row

[root@localhost ~] # cat shuzi.txt 
one
two
three
four
five
#从e开始删除,到h结束,但是后面字符里含有e还会开始删除,直到有h结束
[root@localhost ~] # sed '/e/,/h/d' shuzi.txt 
four

4. Replace

  • format
行范围  s/旧字符串/新字符串/替换标记
  • 4 types of replacement markers
    Number: indicates that the new string will replace the matching place
    g: indicates that the new string will replace all matching places
    p: print the line that matches the replacement command, use
    w file with -n : will The result of the replacement is written to the file
#将第一行的第一个root替换为admin并打印出来
[root@localhost ~] # sed -n 's/root/admin/p' /etc/passwd     
admin:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/admin:/sbin/nologin

#将第一行的第二个root替换为admin
[root@localhost ~] # sed -n 's/root/admin/2p' /etc/passwd   
root:x:0:0:admin:/root:/bin/bash

#替换所有的root
[root@localhost ~] # sed -n 's/root/admin/gp' /etc/passwd 
admin:x:0:0:admin:/admin:/bin/bash
operator:x:11:0:operator:/admin:/sbin/nologin

#删除所有的root
[root@localhost ~] # sed -n 's/root//gp' /etc/passwd  
:x:0:0::/:/bin/bash
operator:x:11:0:operator:/:/sbin/nologin

#将1-5行的开头添加#号
[root@localhost ~] # sed '1,5 s/^/#/' /etc/passwd 
#root:x:0:0:root:/root:/bin/bash
#bin:x:1:1:bin:/bin:/sbin/nologin
#daemon:x:2:2:daemon:/sbin:/sbin/nologin
#adm:x:3:4:adm:/var/adm:/sbin/nologin
#lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

#把以root开头的行结尾的$替换为#
[root@localhost ~] # sed -n '/^root/ s/$/#/p' /etc/passwd 
root:x:0:0:root:/root:/bin/bash#

#创建一个用于sed命令的脚本
[root@localhost ~] # vim script.sed 
s/1/22/                                #将1替换为22,下面以此类推
s/5/66/
#然后直接使用该脚本对文件进行操作                        
[root@localhost ~] # sed -f script.sed  file    

#将/etc/passwd中的1-20行输出保存到file.txt文件中
[root@localhost ~] # sed '1,20w file.txt' /etc/passwd  

#将/etc/passwd 中的1-20行的开头添加#后保存到out.txt文件中
[root@localhost ~] # sed '1,20 s/^/#/w out.txt' /etc/passwd      

#将/bin/bash替换成/bin/csh,这里在“/”前面加了转义符“\”是防止“/”具有其他的功能,转义符使其只有普通字符的功能
[root@localhost ~] # sed -n 's/\/bin\/bash/\/bin\/csh/p' /etc/passwd #转义符看着很乱,可以使用“!”或者“#”作为字符串的分隔符,作用同上    
[root@localhost ~] # sed -n 's!/bin/bash!/bin/csh!p' /etc/passwd     

Supplement: Use the sed tool to query and extract the IP address

[root@localhost ~]#/sbin/ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.2.3  netmask 255.255.255.0  broadcast 192.168.2.255
        inet6 fe80::f411:5214:9150:9903  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:46:34:bb  txqueuelen 1000  (Ethernet)
        RX packets 4485  bytes 435517 (425.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1300  bytes 149354 (145.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost ~]#/sbin/ifconfig ens33 | grep 'inet '
        inet 192.168.2.3  netmask 255.255.255.0  broadcast 192.168.2.255

#sed 's/^.*inet //g'用来删除前面的inet部分,即用空字符将其全部替换替换;sed 's/ *netmask.*$//g'   用来删除后面netmask部分,原理同上
[root@localhost ~]#/sbin/ifconfig ens33 | grep 'inet ' | sed 's/^.*inet //g' | sed 's/ *netmask.*$//g'   
192.168.2.3

Guess you like

Origin blog.csdn.net/qq_35456705/article/details/112147560