the sed usage of Linux
Common options:
- N : Use Quiet (silent) mode. In the usage of general sed, all data from stdin is generally listed on the screen. However, if you add the-n parameter, only the line (or action) that is specially processed by SED is listed.
- e : The Action of SED is edited directly on the instruction list mode ;
-f : directly the action of SED is written in a file, and-f filename can perform the SED action within filename,
-r : -i Modify the contents of the read file directly instead of the screen output.
Common commands:
a:New , a can be followed by a string, and these strings will appear on a new line ( the current next 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 boom;
I: Insert, i can be followed by a string, and these strings will appear on a new line ( the current line );
P: Print, that is, a selection of information printed. Normally P will work with parameter sed-n ~
s: Instead, can be directly replaced by the work of the mile! Usually this S action can be paired with formal notation!
For example 1,20s/old/new/g is!
Example :
Delete a row
[[email protected] Ruby] # sed ' 1d ' ab#删除第一行
[[email protected] ruby]# sed ' $d ' ab#删除最后一行
[[email protected] ruby]# sed ' 1,2d ' ab#删除第一行到第二行
[[email protected] ruby]# sed ' 2, $d ' AB#删除第二行到最后一行
show a row
[[email protected] Ruby] # sed-n ' 1p ' ab#显示第一行
[[email protected] ruby]# sed-n ' $p ' ab#显示最后一行
[[email protected] ruby]# sed-n ' 1,2p ' ab#显示第一行到第二行
[[email protected] ruby]# sed-n ' 2, $p ' AB#显示第二行到最后一行
Querying using a pattern
[[email protected] ruby] # sed-n '/ruby/p ' ab #查询包括关键字ruby所在所有行
[[email protected] ruby] # sed-n '/\$/p ' ab #查询包括关键字 $ where all lines, using backslashes \ Masking Special Meanings
add one or more lines of string
[email protected] ruby]# Cat AB
hello!
Ruby is me,welcome to my blog.
End
[[email protected] ruby]# sed ' 1a drink tea ' ab#第一行后增加字符串 "Drink Tea"hello!
Drink Tea
Ruby is me,welcome to my blog.
End
[[email protected] ruby]# sed ' 1,3a drink tea ' ab
#第一行到第三行后增加字符串 "Drink Tea"
hello!
Drink Tea
Uby is me,welcome to my blog.
Drink Tea
End
Drink Tea
[[email protected] ruby]# sed ' 1a drink tea\ nor coffee ' ab#第一行后增加多行, use line break \ n
hello!
Drink Tea
or Coffee
Ruby is me,welcome to my blog.
End
Instead of one row or more rows
[[email protected] ruby]# sed ' 1c Hi ' AB#第一行代替为Hi
Hi
Ruby is me,welcome to my blog.
End
[[email protected] ruby]# sed ' 1,2c Hi ' ab#第一行到第二行代替为Hi
Hi
End
Replace a section in a row
format:sed' s/the string to replace/the new string/g' (the string to be replaced can be used as a regular expression)
[[email protected] ruby]# sed-n '/ruby/p ' ab | sed ' s/ruby/bird/g '#替换ruby为bird
[[email protected] ruby]# sed-n '/ruby/p ' ab | sed ' s/ruby//g '#删除ruby
insert
# sed-i ' $a bye ' ab #在文件ab中最后一行直接输入 "Bye"
[[email protected] ruby]# Cat ab
hello!
Ruby is me,welcome to my blog.
End
Bye
Delete a matching row
Sed-i '/Match string/d ' filename (note: If the match string is a variable, you need "" instead of ". Remember as if it were)
Replace a string in a matching row
Sed-i '/Match string/s/Replace source string/Replace target string/g ' filename
This article is from the "one small step per day" blog, so be sure to keep this source http://fenyuer.blog.51cto.com/11265169/1889354
The SED usage of Linux