当前位置:网站首页>Sed of three swordsmen in text processing
Sed of three swordsmen in text processing
2022-07-07 12:57:00 【LC181119】
1.sed working principle

2.sed Basic usage
sed [option]... 'script;script;...' [inputfile...]
-n Do not output mode space content to screen , Do not print automatically-e Multi point editing-f FILE Read the edit script from the specified file-r, -E Using extended regular expressions-i.bak Back up the file and edit it in place-s Treat multiple files as separate files , Instead of a single continuous long file stream# explain :-ir I won't support it-i -r Support-ri Support-ni Will empty the file
script Format :
' Address order
Address format :
1. No address : Process the full text2. Single address :#: Specified row ,$: The last line/pattern/: Every line that can be matched by this pattern3. Address range :#,# # from # Go to the first place # That's ok ,3,6 From 3 Go to the first place 6 That's ok#,+# # from # Row to +# That's ok ,3,+4 From 3 Go to the first place 7 That's ok/pat1/,/pat2/#,/pat//pat/,#4. Stepping :~1~2 Odd line2~2 Even number line
command :
p Print the contents of the current mode space , Append to default outputIp Ignore case outputd Delete pattern space matching lines , And start the next cycle nowa [\]text Append text to the specified line , Support use \n Implement multiline appendingi [\]text Insert text before the linec [\]text The replacement behavior is a single or multiple line of textw file Save pattern matching lines to the specified filer file After reading the text of the specified file to the matching line in the pattern space= Print line numbers for lines in schema space! Reverse matching row processing in pattern spaceq End or exit sed
Find overrides
s/pattern/string/ Modifier Search and replace , Other separators are supported , It can be in other forms :[email protected]@@,s###Replace modifier :g In line global substitutionp Show the lines that were replaced successfullyw /PATH/FILE Save the replaced line to the fileI,i Ignore case
Example : Get partition utilization
[[email protected] ~]#df | sed -En '/^\/dev\/sd/[email protected]* ([0-9]+)%.*@\[email protected]'
3
31
13
Example : take IP Address
[[email protected] ~]#ifconfig eth0 |sed -nr "2s/[^0-9]+([0-9.]+).*/\1/p"
10.0.0.8
[[email protected] ~]#ifconfig eth0 | sed -En '2s/^[^0-9]+([0-9.]{7,15}).*/\1/p'
10.0.0.6
[[email protected] ~]#ifconfig eth0 | sed -rn '2s/^[^0-9]+([0-9.]+) .*$/\1/p'
10.0.0.8
[[email protected] ~]#ifconfig eth0 | sed -n '2s/^.*inet //p' | sed -n 's/
netmask.*//p'
10.0.0.8
[[email protected] ~]#ifconfig eth0 | sed -n '2s/^.*inet //;s/ netmask.*//p'
10.0.0.8
[[email protected] ~]#ifconfig eth0 | sed -rn '2s/(.*inet )([0-9].*)(
netmask.*)/\2/p'
10.0.0.8
3.sed Advanced usage
P Print mode space starts with \n Content , And append to the default outputh Overlay content from schema space to hold spaceH Append content from pattern space to hold spaceg Extract data from hold space and overlay into pattern spaceG Extract content from hold space and append to pattern spacex Exchange content in schema space with content in retention spacen Read the next row of the matched row to cover the pattern spaceN Read the next row of the matched row and append it to the pattern spaced Delete rows in schema spaceD If the pattern space contains line breaks , Delete the text in the pattern space up to the first newline character , It doesn't read new input lines , And makeRestart the loop with the synthesized pattern space . If the pattern space does not contain line breaks , It's like sending out d Command to start a normal new cycle
Example : Print even lines
[[email protected] ~]#seq 10 | sed -n 'n;p'
2
4
6
8
10
[[email protected] ~]#seq 10 | sed -n '2~2p'
2
4
6
8
10
[[email protected] ~]#seq 10 | sed '1~2d'
2
4
6
8
10
[[email protected] ~]#seq 10 | sed -n '1~2!p'
2
4
6
8
10
边栏推荐
- Master formula. (used to calculate the time complexity of recursion.)
- Day-14 common APIs
- MySQL导入SQL文件及常用命令
- 2022危险化学品生产单位安全生产管理人员考题及在线模拟考试
- Aike AI frontier promotion (7.7)
- 通讯协议设计与实现
- 【无标题】
- How to reset Google browser? Google Chrome restore default settings?
- Visual stdio 2017 about the environment configuration of opencv4.1
- [疑难杂症]pip运行突然出现ModuleNotFoundError: No module named ‘pip‘
猜你喜欢
How to apply @transactional transaction annotation to perfection?
2022a special equipment related management (boiler, pressure vessel and pressure pipeline) simulated examination question bank simulated examination platform operation
数据库安全的重要性
Adopt a cow to sprint A shares: it plans to raise 1.85 billion yuan, and Xu Xiaobo holds nearly 40%
Day-15 common APIs and exception mechanisms
[pytorch practice] use pytorch to realize image style migration based on neural network
ACL 2022 | 序列标注的小样本NER:融合标签语义的双塔BERT模型
Cookie
聊聊Redis缓存4种集群方案、及优缺点对比
What if does not match your user account appears when submitting the code?
随机推荐
【二叉树】删点成林
Day-19 IO stream
ip2long之后有什么好处?
《ASP.NET Core 6框架揭秘》样章[200页/5章]
Layer pop-up layer closing problem
【从 0 开始学微服务】【01】什么是微服务
[learn microservices from 0] [03] explore the microservice architecture
Cmu15445 (fall 2019) project 2 - hash table details
事务的七种传播行为
Day21 multithreading
怎样重置火狐浏览器
HZOJ #235. Recursive implementation of exponential enumeration
How to continue after handling chain interruption / sub chain error removed from scheduling
Talk about four cluster schemes of redis cache, and their advantages and disadvantages
谷歌浏览器如何重置?谷歌浏览器恢复默认设置?
[difficult and miscellaneous]pip running suddenly appears modulenotfounderror: no module named 'pip‘
@Resource和@Autowired的区别?
通过Keil如何查看MCU的RAM与ROM使用情况
How to reset Firefox browser
ICLR 2022 | 基于对抗自注意力机制的预训练语言模型