当前位置:网站首页>Summary of basic usage of command line editor sed
Summary of basic usage of command line editor sed
2022-06-27 13:27:00 【User 3147702】
1. summary
sed It's a kind of linux Command line editor . When dealing with text ,sed Store the currently processed rows in a temporary buffer , be called “ Mode space ”, then sed Command to process the contents of the buffer , When finished, output the contents of the buffer to standard output , Next process the following lines , Until the end of the file . sed Mainly used to automatically edit one or more files , Simplify the repeated operation of files .
2. sed Introduce
2.1. Parameters
sed Arguments to the command
Parameters | function |
|---|---|
-n | Use quiet mode , Only the processed rows are displayed |
-e | Directly in command line mode sed Editing of actions |
-f | Run... In the specified file sed command |
-r | Support extended regular syntax ( Otherwise, only the basic syntax is supported ) |
-i | Modify the document directly , Instead of just exporting to standard output |
2.2. function
sed Functions that commands can use
function | function |
|---|---|
a | Add a new string to the next line |
c | replace n1,n2 Between the lines |
d | Delete n1,n2 Between the lines |
i | Insert the string to the previous line |
p | Print the specified column |
s | Replace the specified string with another specified string |
3. newly added 、 Delete row
3.1. Delete row
The following shows how to delete 2 To 5 That's ok .
nl /etc/passwd | sed '2,5d'As mentioned above ,d Function is used to delete ( For example clarity , use first nl The command lists the line numbers of the text ), therefore , This command deletes 2 To 5 That's ok .
so , The first 2 To 5 The row has been deleted .
that , Just delete the second 2 Line? ?
nl /etc/passwd | sed '2d'Delete the first 3 To the last line ?
nl /etc/passwd | sed '3,$d'3.2. New row
The following shows that in the 2 Add after the line ’drink tea’
nl /etc/passwd | sed '2a drink tea'that , How to add data to the 2 OK, ahead ?
nl /etc/passwd | sed '2i drink tea'But , How to add two lines .
nl /etc/passwd | sed '2a Drink tea or ......\
> drink beer ?'4. Line replacement and display
The following shows how to 2 To 5 Replace the line with ’No 2-5 line’
nl /etc/passwd | sed '2,5c No 2-5 line'The following shows how to display only the 5-7 The method .
nl /etc/passwd | sed -n '5,7p'5. Data search
Find files that contain root Keyword line .
nl /etc/passwd | sed '/root/p'But , It doesn't seem to get the result we want , That's because the rows found are mixed with other rows , Confusing query results , At this time, the parameters must be passed -n Specifies the silent mode display .
nl /etc/passwd | sed -n '/root/p'6. Find and delete data
Delete /etc/passwd All contain root The line of .
nl /etc/passwd | sed '/root/d'7. Find and replace data
nl /etc/passwd | sed -n '/root/{s/bin/replace/;p}'First sed Found... In silent mode root character string , Then the code in braces is executed . sed ’s/ String to be replaced / New string /g’ This command replaces the string with another specified string .
8. Multi point editing
The above command performs the search first , Then the replacement command is executed , Name executes the print command p. that , How to execute multiple in one command sed Orders ?
nl /etc/passwd | sed -e '3,$d' -e 's/bash/blueshell/'-e Parameter indicates multipoint editing , That is to say, the... Is deleted first 3 Line to the end of the file , And then the remaining lines of bash Replace with blueshell.
9. Directly modify the contents of the document
All the above operations output the results to the standard output . adopt -i Parameters , You can modify the contents of the file directly . however , This operation is very dangerous , Although it is very convenient in many times , But be careful before using .
边栏推荐
- Journal quotidien des questions (6)
- Explore tidb lightning source code to solve the found bugs
- 嵌入式开发:嵌入式基础——回调函数
- 今日睡眠质量记录78分
- zabbix支持钉钉报警
- jvm 性能调优、监控工具 -- jps、jstack、jmap、jhat、jstat、hprof
- On the complexity of software development and the way to improve its efficiency
- Local visualization tool connects to redis of Alibaba cloud CentOS server
- OpenFeign服务接口调用
- GCC compiling dynamic and static libraries
猜你喜欢

外包2年的我终于上岸了!记录我的字节跳动3轮面试,希望帮助到大家!

让学指针变得更简单(一)

Size end byte order

大小端字节序

Deeply convinced plan X - system foundation summary

创建Deployment后,无法创建Pod问题处理

【TcaplusDB知识库】TcaplusDB-tcapsvrmgr工具介绍(三)

Make learning pointer easier (1)

Does Xinhua San still have to rely on ICT to realize its 100 billion enterprise dream?

Openhgnn releases version 0.3
随机推荐
jvm 性能调优、监控工具 -- jps、jstack、jmap、jhat、jstat、hprof
对半查找(折半查找)
Yuweng information, a well-known information security manufacturer, joined the dragon lizard community to build an open source ecosystem
Journal quotidien des questions (6)
Cool in summer
手把手教你搭一个永久运行的个人服务器!
After the deployment is created, the pod problem handling cannot be created
What kind of air conditioner is this?
VS调试技巧
ViewPager2使用记录
mysql 锁机制与四种隔离级别
命令行编辑器 sed 基础用法总结
[tcapulusdb knowledge base] Introduction to tcapulusdb tcapsvrmgr tool (III)
Stack calculation (whether the order of entering and leaving the stack is legal) - Code
Size end byte order
快讯:华为启动鸿蒙开发者大赛;腾讯会议发布“万室如意”计划
How to choose LAN instant messaging software
Airbnb double disk microservice
Istio微服务治理网格流量管理核心资源控制器详解
Tiktok practice ~ public / private short video interchange