当前位置:网站首页>Sed command
Sed command
2022-04-21 15:29:00 【qq_ fifty-two million eight hundred and twenty-five thousand si】
Catalog
3、sed Replacement operation of
One 、sed summary
1、sed Principle
sed Is a flow editor , The flow editor will edit the data flow based on a set of rules provided in advance before the editor processes the data .
sed The editor can process the data in the data stream according to the command , These commands are either entered from the command line , Or stored in a command text .
2、sed The process of
(1) Read process
sed From the input stream ( file 、 The Conduit 、 The standard input ) Read a line in and store it in a temporary buffer ( Also called pattern space )
(2) Execute the process
By default , be-all sed Commands are executed smoothly in pattern space , Unless the address of the line is specified , otherwise sed command , Will be executed on all lines in turn .
(3) Show process
Send the modified content to the output stream . After sending the data , Mode space will be cleared , Before all the file contents are processed , The above process is repeated , Until the content is processed .
3、sed Common options for
(1) Command format
sed -e ' operation ' file 1 file 2
sed -n -e ' operation ' file 1 file 2
sed -f Script files file 1 file 2
sed -e -i ' operation ' file 1 file 2
(2) Common options
(1)-e or --expression
Indicates that the input text file is processed with the specified command , When there is only one operation command, you can omit , It is generally used when executing multiple operation commands .
(2)-f or --file
Indicates that the input text file is processed with the specified script file
(3)-h or --help
Display help
(4)-n or s --quiet
prohibit sed Editor output , But you can p Command to complete the output
(5)i
Directly modify the text file
(3) Common operations
(1)s
Replace specified characters
(2)d
Delete the specified row
(3)a
Add a line of specified content to the specified line
(4)i
Insert a line of specified content on the specified line
(5)c
Replace the selected line with the specified content
(6)y
Character conversion , The converted characters must be the same length
(7)p
Print , If you also specify a row , Indicates to print the specified line ; If no line is specified , It means printing everything ; If there are non printing characters , with AscII Code output . It is usually associated with _n" Use options together
(8)=
Print line number
(9)l
Print text in the data stream and unprintable ASCII character ( Like the terminator s、 tabs \t)
Two 、sed Use
1、sed Query operation of
(1) Method 1
Use sed Command view /etc/shdaow
[root@localhost ~]# sed ' ' /etc/shadow
root:$6$3Rfh74ZoYVL60OLJ$atijevnzniTepczYYLv.WAJ9bmIT0vuOlM.7prgCRUjAeiDoeKd89c96baB32ajZH6G1/rq6DuVXyf3ktp7jY/::0:99999:7:::
bin:*:17110:0:99999:7:::
daemon:*:17110:0:99999:7:::
adm:*:17110:0:99999:7:::
lp:*:17110:0:99999:7:::
sync:*:17110:0:99999:7:::
shutdown:*:17110:0:99999:7:::
halt:*:17110:0:99999:7:::
mail:*:17110:0:99999:7:::
operator:*:17110:0:99999:7:::

(2) Method 2
[root@localhost ~]# sed -n 'p ' /etc/shadow
root:$6$3Rfh74ZoYVL60OLJ$atijevnzniTepczYYLv.WAJ9bmIT0vuOlM.7prgCRUjAeiDoeKd89c96baB32ajZH6G1/rq6DuVXyf3ktp7jY/::0:99999:7:::
bin:*:17110:0:99999:7:::
daemon:*:17110:0:99999:7:::
adm:*:17110:0:99999:7:::
lp:*:17110:0:99999:7:::
sync:*:17110:0:99999:7:::
shutdown:*:17110:0:99999:7:::
halt:*:17110:0:99999:7:::
mail:*:17110:0:99999:7:::
operator:*:17110:0:99999:7:::
View the specified line
[root@localhost ~]# sed -n '3p' /etc/shadow
daemon:*:17110:0:99999:7:::

Using regular expressions
matching root Beginning line
[root@localhost ~]# sed -n '/^root/p' /etc/shadow
root:$6$3Rfh74ZoYVL60OLJ$atijevnzniTepczYYLv.WAJ9bmIT0vuOlM.7prgCRUjAeiDoeKd89c96baB32ajZH6G1/rq6DuVXyf3ktp7jY/::0:99999:7:::

View consecutive rows
see /etc/shadow Of 3 To 6 The content of the line
[root@localhost ~]# sed -n '3,6p' /etc/shadow
daemon:*:17110:0:99999:7:::
adm:*:17110:0:99999:7:::
lp:*:17110:0:99999:7:::
sync:*:17110:0:99999:7:::

Look at the last line of the file
[root@localhost ~]# sed -n '$p' /etc/shadow
zs:$6$LA7n3jCI3IvhMBHS$.9YqBUKpKwb9DHSIPX9KDVV4YJ5g04GxFQI8jdGx8XJoMqEgkdLrlAd03ahyCcjejZ7iKuO.Iv5OSlGxoSCTD.::0:99999:7:::
2、sed Delete operation
Deleting a specified row is not really deleting , Just show the deleted results , Not really deleted , If you want to run in text, you need to cooperate -i command
Delete empty lines in text
[root@localhost ~]# sed '/^$/d' test.txt
12
24
23
334
234

Delete the line specified in the file
[root@localhost ~]# sed '2d' t2.txt
123
789
321
451

3、 Replace
Command format
sed Designated line 's/ String to replace / Replaced string / Replace mark
(1)g
Indicates that you want to replace all matching rows
(2)w
Save the replaced results to the document
Put... In the file 321 Replace with 345
[root@localhost ~]# sed 's/321/345/' t2.txt
123
456
789
345
451

4、sed Add content operation
Add... Below the second line 234
[root@localhost ~]# sed '2a 234' t2.txt
123
456
234
789
321
451
版权声明
本文为[qq_ fifty-two million eight hundred and twenty-five thousand si]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211526094116.html
边栏推荐
- 嵌入式驱动模块的加载与卸载
- Page 92 group informatization planning scheme enterprise application integration solution
- Mysql的安装与卸载
- 百度云盘好用还是阿里云盘好用?
- Take it easy, just talk about the soft test
- LaTeX常用公式查询
- MySQL8. 0 correct password change posture
- ABAQUS loads according to coordinates - analytical field load mapping
- How to set start date query statement in SQL Server
- Embedded development: three skills of reusing development board for testing
猜你喜欢

BetterScroll源码阅读顺便学习TypeScript

EmlParse:一款超轻量级的批量解析EML格式电子邮件的工具

Introduction to openlayers (II)

Elemetn form control --- automatically locate the position of the field when it is submitted without passing the verification field

What is an email address? Easy to use email registration application

92页集团信息化规划方案企业应用集成解决方案

手把手教你实现手绘风格图形

智慧园区数融通-数字化赋能运营管理平台解决方案

Page 92 group informatization planning scheme enterprise application integration solution

LeetCode 654:最大二叉树
随机推荐
[binary search - medium] 1498 Number of subsequences satisfying the condition
Take it easy, just talk about the soft test
JUC学习记录
[binary search - medium] sword finger offer II 070 Sort numbers that appear only once in the array
AcWing 1788. Why do cows cross the road (simulation)
AcWing 1854. 晋升计数(模拟)
Easy Language cef3 get request returned source
无常损失简单解释
Elemetn form control --- automatically locate the position of the field when it is submitted without passing the verification field
AcWing 1854. Promotion count (Analog)
Introduction to openlayers (I)
注册登录程序1
What is an email address? Easy to use email registration application
APP评测的网站有哪些?
[binary search - simple] sword finger offer II 072 take a square root
Mysql
Is Baidu cloud disk easy to use or Alibaba cloud disk easy to use?
汉诺塔游戏与递归
Deltix Round, Summer 2021 E. Equilibrium
Openharmony camera user driven framework
