当前位置:网站首页>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
边栏推荐
- ip2long与long2IP 分析
- [疑难杂症]pip运行突然出现ModuleNotFoundError: No module named ‘pip‘
- Users, groups, and permissions
- 【无标题】
- Query whether a field has an index with MySQL
- 图形对象的创建与赋值
- The URL modes supported by ThinkPHP include four common modes, pathinfo, rewrite and compatibility modes
- 人均瑞数系列,瑞数 4 代 JS 逆向分析
- Realize a simple version of array by yourself from
- - Oui. Migration entièrement automatisée de la Sous - base de données des tableaux d'effets sous net
猜你喜欢
Sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]
Master formula. (used to calculate the time complexity of recursion.)
Master公式。(用于计算递归的时间复杂度。)
Several ways to clear floating
Visual stdio 2017 about the environment configuration of opencv4.1
Four functions of opencv
[statistical learning methods] learning notes - Chapter 5: Decision Tree
Sorting, dichotomy
2022广东省安全员A证第三批(主要负责人)考试练习题及模拟考试
Day-19 IO stream
随机推荐
Find ID value MySQL in string
Leetcode brush questions: binary tree 19 (merge binary tree)
Smart cloud health listed: with a market value of HK $15billion, SIG Jingwei and Jingxin fund are shareholders
谷歌浏览器如何重置?谷歌浏览器恢复默认设置?
Leetcode skimming: binary tree 22 (minimum absolute difference of binary search tree)
Day-24 UDP, regular expression
聊聊Redis缓存4种集群方案、及优缺点对比
Leetcode skimming: binary tree 25 (the nearest common ancestor of binary search tree)
Creation and assignment of graphic objects
CMU15445 (Fall 2019) 之 Project#2 - Hash Table 详解
test
Adopt a cow to sprint A shares: it plans to raise 1.85 billion yuan, and Xu Xiaobo holds nearly 40%
. Net ultimate productivity of efcore sub table sub database fully automated migration codefirst
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
Visual stdio 2017 about the environment configuration of opencv4.1
Users, groups, and permissions
Charles: four ways to modify the input parameters or return results of the interface
Day-15 common APIs and exception mechanisms
[statistical learning method] learning notes - support vector machine (I)
ip2long与long2IP 分析