Linux SED Command Summary
Introduction
Sed is an online editor that processes a single line of content at a time. When processing, the currently processed rows are stored in a temporary buffer called pattern space, followed by the SED command to process the contents of the buffer, and after processing is done, the contents of the buffer are sent to the screen. Then the next line is processed, so it repeats until the end of the file. The file content does not change unless you use redirection to store the output. SED is mainly used to automatically edit one or more files, to simplify the repeated operation of the file, to write the conversion program and so on.
[[email protected] ~]# sed [-nefr] [Action] option with parameter:-n : Use Quiet (silent) mode. In general sed usage, all data from STDIN is generally listed on the terminal. However, if the -n parameter is added, only the line (or action) that has been specially processed by sed will be listed. -E&NBSP: Edit the sed action directly in command-line mode;-F&NBSP: Directly write sed action in a file, -f filename It is possible to run the filename within the sed action;-r :sed 's actions support the syntax of the extended formal notation. (The default is the basic formal notation of French)-i : Directly modifies the contents of the read file, not the output to the terminal. Action Description:  [N1[,N2]]FUNCTIONN1, N2 : Not necessarily exist, generally represents the "select the number of lines of action", for example, if my action is required in 10 to 20 Between lines, the " 10,20[Action behavior] " FUNCTION:A : New, a can be appended to the 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 usually does not take any of the back, i : Insert , i can be followed by strings, which appear on a new line (the current line); p : Print, that is, the selected data is printed. Usually p will be used in conjunction with parameter sed -n ; s : Replace, can be directly replaced by work! Usually this s action can be paired with formal notation! such as 1,20s/old/new/g;
Example: (Suppose we have a file named Test.txt)
Delete a row
[[Email protected] Ruby ] # sed ' 1d ' test.txt #删除第一行
[[email protected] ruby]# sed ' $d ' test.txt #删除最后一行
[[email protected] ruby]# sed ' 1,2d ' test.txt #删除第一行到第二行
[[email protected] ruby]# sed ' 2, $d ' test.txt #删除第二行到最后一行
Show a row
. [[Email protected] Ruby ] # sed-n ' 1p 'Test.txt#显示第一行
[[email protected] ruby]# sed-n ' $p ' test.txt #显示最后一行
[[email protected] ruby]# sed-n ' 1,2p 'Test.txt#显示第一行到第二行
[[email protected] ruby]# sed-n ' 2, $p 'Test.txt#显示第二行到最后一行
query using patterns
[[email protected] Ruby] # sed-n '/ruby/p ' test.txt #查询包括关键字ruby所在所有行
[[email protected] ruby] # sed-n '/\$/p ' test.txt #查询包括关键字 $ where all lines, using backslashes \ Masking special meaning
[email protected] ~]# cat Test.txt
Asfkjlsfj
Asfkjls
Asfkjlswer
Fkjlswer
Fkjlswerhfh
Fkjlerhfh
[Email protected] ~]# sed ' 1a ljx tea ' test.txt #第一行后增加字符串 ' ljx tea '
Asfkjlsfj
LJX Tea
Asfkjls
Asfkjlswer
Fkjlswer
Fkjlswerhfh
Fkjlerhfh
[[Email protected] ~]# sed ' 1,3a ljx tea ' test.txt #第一行到第三行后增加字符串 ' ljx tea '
Asfkjlsfj
LJX Tea
Asfkjls
LJX Tea
Asfkjlswer
LJX Tea
Fkjlswer
Fkjlswerhfh
Fkjlerhfh
[[Email protected] ~]# sed ' 1a ljx tea\nor lll ' test.txt #第一行后增加多行, using newline characters \ n
Asfkjlsfj
LJX Tea
or lll
Asfkjls
Asfkjlswer
Fkjlswer
Fkjlswerhfh
Fkjlerhfh
Instead of one row or more rows
[Email protected] ~]# sed ' 1c Hi ' test.txt #第一行代替为Hi
Hi
Asfkjls
Asfkjlswer
Fkjlswer
Fkjlswerhfh
Fkjlerhfh
[Email protected] ~]# sed ' 1,3c Hi ' test.txt #第一行到第三行代替为Hi
Hi
Fkjlswer
Fkjlswerhfh
Fkjlerhfh
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] ~]# sed-n '/fk/p ' test.txt |sed ' s/fk/iii/g ' #替换fk为iii
Asiiijlsfj
Asiiijls
Asiiijlswer
Iiijlswer
Iiijlswerhfh
Iiijlerhfh
[Email protected] ~]# sed-n '/fk/p ' test.txt |sed ' s/fk//g ' #删除fk
Asjlsfj
Asjls
Asjlswer
Jlswer
Jlswerhfh
Jlerhfh
Insert
[[email protected] ruby] # sed-i ' $a bye ' test.txt #在文件ab中最后一行直接输入 "Bye"
[email protected] ruby]# cat Test.txt
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/1913129
Linux SED Command Summary