The SED command line format is:
sed [-nefri] ' command ' Enter text
Common options:
-N: Use Quiet (silent) mode. In general sed usage, all data from stdin is generally listed on the screen. However, if you add the-n parameter, only the line (or action) that is specifically handled by SED will be listed.
-E: SED action edit directly on instruction column mode;
-F: Directly write the action of SED in a file, the-f filename can perform the SED action in filename;
-r:sed's actions support the syntax of extended formal notation. (Presupposition is the basic formal representation of French law)
-I: Directly modify the contents of the file read, not by the screen output.
Common commands:
A: New, a can be followed by a string, and these strings will appear on the new line (the current line) ~
C: Replace, C can be followed by strings, these strings can replace the line between N1,N2!
D: Delete, because it is deleted ah, so d usually do not pick up any of the drums;
I: Insert, I can be followed by strings, and these strings will appear on a new line (the current line);
P: Print, that is, to print out a selection of data. Normally p will work with the parameter Sed-n ~
S: Replace and can be directly substituted for work miles! Usually this s action can be paired with formal notation! For example, 1,20s/old/new/g is!
For example: (assuming we have a file named AB)
Delete a row
[Root@localhost Ruby] # sed ' 1d ' ab #删除第一行
[Root@localhost Ruby] # sed ' $d ' ab #删除最后一行
[Root@localhost Ruby] # sed ' 1,2d ' ab #删除第一行到第二行
[Root@localhost Ruby] # sed ' 2, $d ' AB #删除第二行到最后一行
Show a row
. [Root@localhost Ruby] # sed-n ' 1p ' ab #显示第一行
[Root@localhost Ruby] # sed-n ' $p ' ab #显示最后一行
[Root@localhost Ruby] # sed-n ' 1,2p ' ab# show the first line to the second row
[Root@localhost Ruby] # sed-n ' 2, $p ' ab# show the second line to the last line
Using patterns for querying
[Root@localhost Ruby] # sed-n '/ruby/p ' ab #查询包括关键字ruby所在所有行
[Root@localhost Ruby] # sed-n '/\$/p ' ab# query includes all lines of the keyword $, using backslash \ Mask Special meaning
Add one or more lines of string
[Root@localhost ruby]# Cat AB
Hello!
Ruby is me,welcome to my blog.
End
[Root@localhost Ruby] # sed ' 1a drink tea ' ab #第一行后增加字符串 "Drink Tea"
Hello!
Drink tea
Ruby is me,welcome to my blog.
End
[Root@localhost Ruby] # sed ' 1,3a drink tea ' ab #第一行到第三行后增加字符串 ' drink tea '
Hello!
Drink tea
Ruby is me,welcome to my blog.
Drink tea
End
Drink tea
[Root@localhost Ruby] # sed ' 1a drink tea\nor coffee ' ab #第一行后增加多行, using line breaks \ n
Hello!
Drink tea
or coffee
Ruby is me,welcome to my blog.
End
Replace one or more lines
[Root@localhost Ruby] # sed ' 1c hi ' ab# the first line instead of hi
Hi
Ruby is me,welcome to my blog.
End
[Root@localhost Ruby] # sed ' 1,2c hi ' ab# the first line to the second line instead of hi
Hi
End
Replace a part of a row
Format: sed ' s/string to replace/new string/g ' (string to be replaced can be in regular expression)
[Root@localhost Ruby] # sed-n '/ruby/p ' ab | Sed ' s/ruby/bird/g ' #替换ruby为bird
[Root@localhost Ruby] # sed-n '/ruby/p ' ab | Sed ' s/ruby//g ' #删除ruby
Insert
[Root@localhost Ruby] # sed-i ' $a bye ' ab #在文件ab中最后一行直接输入 ' bye '
[Root@localhost ruby]# Cat AB
Hello!
Ruby is me,welcome to my blog.
End
Bye