当前位置:网站首页>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

sed namely Stream EDitor, and vi Different ,sed It's a line editor
Sed It's reading a line from a file or pipe , Deal with a line , Output one line ; Read one more line , One more line , One more line , until The last line . Whenever a row is processed , Store the currently processed rows in a temporary buffer , be called Mode space (Pattern
Space, Then use sed Command processing buffer contents , After processing , Send the contents of the buffer to the screen . Then deal with a line , This is repeated , Until the end of the file . The design pattern of processing one line at a time makes sed A high performance ,sed When reading large files There will be no Caton . If you use vi Command to open dozens of M Hundreds of M The file of , It's obvious that there's a Caton , This is because vi The command to open a file is to load the file into memory at one time , Then open it again .Sed This situation is avoided , Line by line , It opens very fast , The execution speed is also very fast

2.sed Basic usage

Format :
sed [option]... 'script;script;...' [inputfile...]
Common options
-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 text
2. Single address :
  #: Specified row ,$: The last line
  /pattern/: Every line that can be matched by this pattern
3. 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 line
    2~2 Even number line

command :

p Print the contents of the current mode space , Append to default output
Ip Ignore case output
d Delete pattern space matching lines , And start the next cycle now
a [\]text Append text to the specified line , Support use \n Implement multiline appending
i [\]text Insert text before the line
c [\]text The replacement behavior is a single or multiple line of text
w file Save pattern matching lines to the specified file
r 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 space
q           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 substitution
p Show the lines that were replaced successfully
w   /PATH/FILE Save the replaced line to the file
I,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  

sed In addition to pattern space , It also supports keeping space (Hold Space, Use this space , The number in the pattern space can be
According to the , Temporarily save to hold space , So that subsequent processing , Achieve more powerful functions .
Common advanced commands
P Print mode space starts with \n Content , And append to the default output
h Overlay content from schema space to hold space
H Append content from pattern space to hold space
g Extract data from hold space and overlay into pattern space
G Extract content from hold space and append to pattern space
x Exchange content in schema space with content in retention space
n Read the next row of the matched row to cover the pattern space
N Read the next row of the matched row and append it to the pattern space
d Delete rows in schema space
D 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 make
Restart 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

 

原网站

版权声明
本文为[LC181119]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130616434083.html