当前位置:网站首页>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
13Example : 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.83.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
边栏推荐
- 免费手机号码归属地API查询接口
- 环境配置篇
- xshell评估期已过怎么办
- 【无标题】
- Day-17 connection set
- Sequoia China completed the new phase of $9billion fund raising
- 通讯协议设计与实现
- Common knowledge of one-dimensional array and two-dimensional array
- Leetcode skimming: binary tree 27 (delete nodes in the binary search tree)
- visual stdio 2017关于opencv4.1的环境配置
猜你喜欢

Leetcode skimming: binary tree 22 (minimum absolute difference of binary search tree)

数据库安全的重要性

红杉中国完成新一期90亿美元基金募集

Several ways to clear floating

Master公式。(用于计算递归的时间复杂度。)

博文推荐|Apache Pulsar 跨地域复制方案选型实践

Aike AI frontier promotion (7.7)

图形对象的创建与赋值

Leetcode brush questions: binary tree 19 (merge binary tree)
![[statistical learning method] learning notes - logistic regression and maximum entropy model](/img/f7/857d053cc2cee81c24919aafab3c6e.png)
[statistical learning method] learning notes - logistic regression and maximum entropy model
随机推荐
基于NeRF的三维内容生成
Layer pop-up layer closing problem
处理链中断后如何继续/子链出错removed from scheduling
How to reset Google browser? Google Chrome restore default settings?
明星企业普渡科技大裁员:曾募资超10亿 腾讯红杉是股东
图像像素读写操作
Charles: four ways to modify the input parameters or return results of the interface
. Net ultimate productivity of efcore sub table sub database fully automated migration codefirst
Session
Leetcode brush questions: binary tree 19 (merge binary tree)
Day-19 IO stream
Lingyunguang of Dachen and Xiaomi investment is listed: the market value is 15.3 billion, and the machine is implanted into the eyes and brain
Importance of database security
. Net ultimate productivity of efcore sub table sub database fully automated migration codefirst
.Net下极限生产力之efcore分表分库全自动化迁移CodeFirst
数据库安全的重要性
ISPRS2021/遥感影像云检测:一种地理信息驱动的方法和一种新的大规模遥感云/雪检测数据集
Day22 deadlock, thread communication, singleton mode
[statistical learning method] learning notes - support vector machine (I)
环境配置篇