当前位置:网站首页>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
边栏推荐
- Session
- 2022聚合工艺考试题模拟考试题库及在线模拟考试
- MySQL导入SQL文件及常用命令
- @Resource和@Autowired的区别?
- Conversion from non partitioned table to partitioned table and precautions
- 高瓴投的澳斯康生物冲刺科创板:年营收4.5亿 丢掉与康希诺合作
- Polymorphism, final, etc
- PHP调用纯真IP数据库返回具体地址
- [learn wechat from 0] [00] Course Overview
- Leetcode brush question: binary tree 24 (the nearest common ancestor of binary tree)
猜你喜欢
ACL 2022 | 序列标注的小样本NER:融合标签语义的双塔BERT模型
红杉中国完成新一期90亿美元基金募集
. Net ultimate productivity of efcore sub table sub database fully automated migration codefirst
Session
Charles: four ways to modify the input parameters or return results of the interface
Day-16 set
Image pixel read / write operation
2022 polymerization process test question simulation test question bank and online simulation test
Day-18 hash table, generic
聊聊Redis缓存4种集群方案、及优缺点对比
随机推荐
Cryptography series: detailed explanation of online certificate status protocol OCSP
Design and implementation of communication protocol
Leetcode skimming: binary tree 23 (mode in binary search tree)
详解ThinkPHP支持的URL模式有四种普通模式、PATHINFO、REWRITE和兼容模式
2022A特种设备相关管理(锅炉压力容器压力管道)模拟考试题库模拟考试平台操作
在字符串中查找id值MySQL
Polymorphism, final, etc
test
《ASP.NET Core 6框架揭秘》样章[200页/5章]
Sorting, dichotomy
【无标题】
PHP calls the pure IP database to return the specific address
【从 0 开始学微服务】【03】初探微服务架构
How to reset Google browser? Google Chrome restore default settings?
ICLR 2022 | 基于对抗自注意力机制的预训练语言模型
Simple implementation of call, bind and apply
Day-17 connection set
Aike AI frontier promotion (7.7)
Image pixel read / write operation
Visual stdio 2017 about the environment configuration of opencv4.1