Sed is a good file processing tool, itself is a pipe command, mainly in the behavior of the unit processing, you can replace the data rows, delete, add, select and other specific work, the following first understand the use of SED
The SED command line format is:
sed [-nefri] ' command ' input text
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: The action of SED is written in a text directly, and-f filename can perform the SED action within filename; -r:sed's actions support the syntax of extended formal notation. (Presupposition is the basic formal notation of French law) -I: Directly modifies the contents of the read file 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 row) C: Replace, C can be followed by strings, these strings can replace the line between N1,N2! D: Delete, because it is deleted, so d is usually not followed by any parameters; I: Insert, I can be followed by the string, and these strings will appear on a new line (the current line); P: Print, that is, print out a selected material. Normally p is used with parameter sed-n S: Replace, can be replaced directly, usually S-action can be paired with formal notation |
Example: (Suppose we have a file named Lele)
[[Email protected] ~] #cat Lelehello!lele is me,welcome to my blog.end
Delete a row
[[Email protected] ~] # sed ' 1d ' lele #删除第一行 [[email protected] ~] # sed ' $d ' lele #删除最后一行 [[email protected] ~] # SE d ' 1,2d ' Lele #删除第一行到第二行 [[email protected] ~] # sed ' 2, $d ' Lele #删除第二行到最后一行
Show a row
[[Email protected] ~] # sed-n ' 1p ' Lele #显示第一行 [[email protected] ~] # sed-n ' $p ' Lele #显示最后一行 [[email protected] ~] # Sed-n ' 1,2p ' Lele #显示第一行到第二行 [[email protected] ~] # sed-n ' 2, $p ' Lele #显示第二行到最后一行
Querying using a pattern
[[Email protected] ~] # sed-n '/lele/p ' Lele #查询包括关键字ruby所在所有行 [[email protected] ~] # sed-n '/\$/p ' lele# query includes all lines where the keyword $ is located, so Use backslash \ Mask Special meaning
Add one or more lines of string
[[Email protected] ~] # cat Lelehello!lele is me,welcome to my blog.end[[email protected] ~] # sed ' 1a drink tea ' Lele #第 After a row, add the string "Drink tea" Hello!drink Tearuby is me,welcome to my blog. End[[email protected] ~] # sed ' 1,3a drink tea ' lele #第一行到第三行后增加字符串 "drink Tea" Hello!drink Tearuby is me,welcome to my blog . Drink Teaenddrink Tea[[email protected] ~] # sed ' 1a drink tea\nor coffee ' lele #第一行后增加多行, using line breaks \nhello!drink Teaor coffee Ruby is me,welcome to my blog.end
Instead of one row or more rows
[[Email protected] ~] # sed ' 1c Hi ' Lele #第一行代替为HiHiruby is me,welcome to my blog.end[[email protected] ~] # SED ' 1,2c Hi ' Lele #第一行到第二行代替为HiHiend
Replace a section in a row
Format: sed ' s/string to replace/new string/g ' (the string to replace can be used with regular expressions)
[[Email protected] ~] # sed-n '/ruby/p ' Lele | Sed ' s/ruby/bird/g ' #替换ruby为bird [[email protected] ~] # sed-n '/ruby/p ' Lele | Sed ' s/ruby//g ' #删除ruby
Insert
[[Email protected] ~] # sed-i ' $a bye ' Lele #在文件ab中最后一行直接输入 "Bye" [[email protected] ~]# cat Lelehello!ruby is Me,wel Come to my Blog.endbye
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 was) replace a string in the matching row sed-i '/Match string/s/Replace the source string/replace the target string/g ' filename
Linux sed usage