Linux sed command detailed explanation

1. Introduction to sed command

The full name of sed is: Stream EDitor (stream editor)
The Linux sed command uses scripts to process text files, and sed can process and edit text files according to script instructions. Sed is mainly used to automatically edit one or more files, simplify repeated operations on files, write conversion programs, etc.

Second, the operation mode of sed

When processing data, Sed reads one line at a time from the input source and stores it in what is called the pattern space. All Sed transformations happen in pattern space. Transformations are described by single-letter commands on the command line or provided in external Sed script files. Most Sed commands can be preceded by an address or a range of addresses to limit their scope.

3. Related options of sed

 -n, --quiet, --silent    取消自动打印模式空间
 -e 脚本, --expression=脚本   添加“脚本”到程序的运行列表
 -f 脚本文件, --file=脚本文件  添加“脚本文件”到程序的运行列表
 --follow-symlinks    直接修改文件时跟随软链接
 -i[扩展名], --in-place[=扩展名]    直接修改文件(如果指定扩展名就备份文件)
 -l N, --line-length=N   指定“l”命令的换行期望长度
 --posix  关闭所有 GNU 扩展
 -r, --regexp-extended  在脚本中使用扩展正则表达式
 -s, --separate  将输入文件视为各个独立的文件而不是一个长的连续输入
 -u, --unbuffered  从输入文件读取最少的数据,更频繁的刷新输出
 --help     打印帮助并退出
 --version  输出版本信息并退出
 -a ∶新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
 -c ∶取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
 -d ∶删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
 -i ∶插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
 -p ∶列印,亦即将某个选择的资料印出。通常 p 会与参数 sed -n 一起运作~
 -s ∶取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法

Fourth, the basic usage of sed

1. sed syntax

Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

2.sed viewing function

① View the contents of lines 5 to 8 of the passwd file

[root@control data]# sed -n '5,8 p' passwd
rootaaaa1a1:x:0:0:root:/aa1a2sroot:/bin/bash
rootaaaa1a1:x:0:0:root:/aa1a2sroot:/bin/bash
rootaaaa1a1:x:0:0:root:/aa1a2sroot:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

② Check the lines starting with roo in the passwd file

[root@control data]# sed -n '/^roo/ p' passwd
root:x:0:0:root:/root:/bin/bash

③ Ignore case, print out the line containing the root character

[root@control data]# sed -n '/root/I p' passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

3. The search function of sed

Find the line in the passwd file that has the string /bin/bash

[root@control data]# sed -n '\%/bin/bash% p' passwd
root:x:0:0:root:/root:/bin/bash
admin:x:1001:1001::/home/admin:/bin/bash
user1:x:1002:1002:test user:/home/user1:/bin/bash
ituser1:x:2002:2002::/data/itusers:/bin/bash
redhat:x:2003:2003::/home/redhat:/bin/bash
huawei:x:2004:2004::/home/huawei:/bin/bash
user3:x:2000:2000::/home/user3:/bin/bash
user2:x:2019:2019::/home/user2:/bin/bash
user4:x:2025:2025::/home/user4:/bin/bash
user5:x:2026:2026::/home/user5:/bin/bash
user6:x:2027:2027::/home/user6:/bin/bash
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
user99:x:2028:2028::/home/user99:/bin/bash

4. Delete the printout of lines 2~5 of /data/passwd

[root@control data]# sed '2,5 d' passwd |head
root:x:0:0:root:/root:/bin/bash
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
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin

5. Newly added by behavior unit

① Add a new string after the fourth line on the file passwd

root@control data]# sed -e 4a\it-test passwd | head

rootaaaa1a1:x:0:0:root:/aa1a2sroot:/bin/bash
rootaaaa1a1:x:0:0:root:/aa1a2sroot:/bin/bash
rootaaaa1a1:x:0:0:root:/aa1a2sroot:/bin/bash
it-test
rootaaaa1a1:x:0:0:root:/aa1a2sroot:/bin/bash
rootaaaa1a1:x:0:0:root:/aa1a2sroot:/bin/bash
rootaaaa1a1:x:0:0:root:/aa1a2sroot:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

② passwd first add huawei before

[root@control data]# sed '1 i\huawei ' passwd |head
huawei 
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
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

6.sed replacement function

① replace the third line of passwd with redhat

[root@control data]# sed '3 c\redhat' passwd
[root@control data]# sed '3 c\redhat' passwd |head
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
redhat
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

7.sed's find and replace function

Find the bin strings from 5 to 10 of passwd and replace them with aaaa

[root@control data]# sed -n '5,10 s/bin/aaaa/ p' passwd |head
lp:x:4:7:lp:/var/spool/lpd:/saaaa/nologin
sync:x:5:0:sync:/saaaa:/bin/sync
shutdown:x:6:0:shutdown:/saaaa:/sbin/shutdown
halt:x:7:0:halt:/saaaa:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/saaaa/nologin
operator:x:11:0:operator:/root:/saaaa/nologin

8.sed operates on the original file

① Delete the first line of the original file

Original file

 cat passwd |head
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
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
[root@control data]# 

after delete operation

[root@control data]# sed -i '1 d' passwd
[root@control data]# cat passwd |head
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

② Backup before modifying the original file

[root@control data]# sed -i.bak '1 d' passwd


insert image description here

Guess you like

Origin blog.csdn.net/jks212454/article/details/116066856