The SED of the Three Musketeers of Linux
SED command is a powerful flow editor, it is a very important tool in text processing, can perfectly match the use of regular expressions, the function is extraordinary. When processed, it stores the line of the currently processed text in a temporary buffer, called the pattern space, and the SED command processes the contents of the buffer, sending the contents of the buffer to the screen after processing is complete. Next line is processed until the end of the file. Note that the content of the file before and after processing does not change unless you use redirection to store output or use specific options.
1. Use the format
sed [option] ' Adresscommand ' FILE ...
2. Parameters
[option]
-E script-e SCRIPT ... : Multiple scripts can be executed at the same time
-F FILENAME: Saves the specified command to the file filename, reads the contents from the filename file, executes the command sequentially
-I: Modify the source file content, use CAUTION!!!
-N: Silent mode, no longer displays the contents of the mode space
-R: Using extended regular expressions
Adress
Startline,endline: If 1,100
/regexp/:/internal use of regular expressions/, such as/^root/
/patter1/,/patter2/: The first line to match the pattern 1 to the beginning, to the end of the line matching the pattern 2
LineNumber: The specified row
.: When moving forward
$: Last line
Startline+n: Starting with the specified line and its backward n rows
Command
A \string: Appends a row after the specified line with the contents of STRING, adding "\" to Multiple lines
D: Delete rows that match the criteria
I \string: Appends a row before the specified line, with the contents of STRING
P: Show rows that match the criteria
R FILENAME: Adds the contents of the specified file FILENAME to the file specified line
W FileName: Writes the specified file specified row contents to the FILENAME file and saves
3. Special usage (more important)
Sed ' s/patter/string/modifier ' FILE: Find and replace (by default only the first string in each line is matched to the pattern), where "/" can be substituted by other special characters, but three must be the same. such as: ' s### ', ' [email protected]@@ '.
PATTER: What you want to find, support regular expressions, add-r option
String: Replace with string, back reference is supported
The string to which the reference matches:
Enclose the patter part of the string you want to refer to "()".
\1, \2 ...: Refer to the string that matches to, respectively, refer to 1th (), 2nd () ...
such as: Will L. The characters in e format are appended with R:like-->liker, Love-->lover, and are indicated as follows:
Sed-r ' s# (L.. e) #\1r# ' FILE
Modifier:
G: Indicates global substitution
I: Indicates ignoring case
4. Practical examples
# Create a file containing the following, which will use this file [[email protected] ~]# cat Person.txt 1,zhangsan,teacher2,lisi,ceo3,wangwu,student4, Zhaoliu,ceo5,huangquan,student
1) Append line: Add line after line 4th
<BR class= "Apple-interchange-newline" ><div id= "Inner-editor" ></div>
[Email protected] ~]# sed ' 4a \this line is newly added ' Person.txt1,zhangsan,teacher2,lisi,ceo3,wangwu,student4, Zhaoliu,ceothis Line is newly added5,huangquan,student
2) Delete the specified line: Delete line 3rd to last row
[[Email protected] ~]# sed ' 3, $d ' Person.txt1,zhangsan,teacher2,lisi,ceo
3) Do not print lines that contain the specified content: Print lines that do not include the CEO
[[Email protected] ~]# sed '/ceo/d ' person.txt 1,zhangsan,teacher3,wangwu,student5,huangquan,student
4) Print the line containing the specified content: Print the line containing the CEO
# The line containing the CEO will be printed two times by default [[email protected] ~]# sed '/ceo/p ' person.txt 1,zhangsan,teacher2,lisi,ceo2,lisi,ceo3,wangwu, student4,zhaoliu,ceo4,zhaoliu,ceo5,huangquan,student# can use the-n option to not output content in mode space [[email protected] ~]# sed-n '/ceo/p ' Person.txt 2,lisi,ceo4,zhaoliu,ceo
5) Find and replace with the specified content output: Replace the CEO with the CTO
[Email protected] ~]# sed ' s#ceo#cto#g ' person.txt 1,zhangsan,teacher2,lisi,cto3,wangwu,student4,zhaoliu,cto5, Huangquan,student
In summary, the SED command is a powerful text-search editing command that can be used with regular expressions, pipelines, and redirects for powerful functionality.
The SED of the Three Musketeers of Linux