Concept of 1.sed
Sed means the stream editor, which is commonly used as a filter in shell scripts and makefile, which introduces the output of the previous program to the input of SED, which is converted to another format output through a series of editing commands. SED is based on basic mode, and sed and vi all originate from the early Unix Ed tools, so many SED commands are the same as the last command of VI.
How the 2.sed works
Sed is an online editor that processes a single line of content at a time. When processing, stores the currently processed rows in a temporary
In the flush area, called the pattern space, and then uses the SED command to process the contents of the buffer, processing
When finished, send the contents of the buffer to the screen. Then the next line is processed, so it repeats until the end of the file. File
The content does not change unless you use redirection to store the output. SED is primarily used to automatically edit one or more files;
Repeated operation of the file.
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/83/4F/wKiom1dwiGCgTXtkAAA7A0-RzcY477.png-wh_500x0-wm_3 -wmp_4-s_3842695725.png "title=" 1-100 Digui. PNG "alt=" Wkiom1dwigcgtxtkaaa7a0-rzcy477.png-wh_50 "/>
3.sed command-line format
The basic format of the SED command line is:
SED option ' script ' file1 file2 ...
Sed option-f scriptfile file1 file2 ...
The files processed by SED can be either redirected from a standard input, or when command-line arguments are passed in, and command-line arguments can be
To pass in multiple files at once, SED is processed sequentially. The edit command for SED can be passed directly to the command line arguments, or it can be written
into a script file and then specify with the-f parameter , the format of the Edit command is/pattern/action
where pattern is a regular expression, action is an edit operation . The SED program reads the pending file one line at a time, if a
A line matches the pattern, the corresponding action is executed, and if a command has no pattern and only the action, the
The action will be used for each row of the file to be processed.
4.sed Commands and options
Command
A\: Adds one or more rows after the current line. Multiple rows, except for the last row, need "\" to continue at the end of each line
C\: Replaces the text in the current line with the new text after this symbol. In addition to the last row, the end of each line needs to be
Use "\" to continue the line
I\: Inserts text before the current line. Multiple rows except the last row, the end of each line must be "\" To continue the row D delete row
H: Copy the contents of the pattern space to the staging buffer
H: Append the contents of the pattern space to the staging buffer
G: Copy the contents of the staging buffer to the schema space, overwriting the original content
G: Append the contents of the staging buffer to the schema space, appended to the original content
L: List non-printable characters
P: Print Line
N: Reads into the next input line and starts processing it from the next command instead of the first command
Q: End or exit sed
R: Read input line from file
! : Apply commands to all rows except the selected row
S: replace one string with another
G: Global substitution within a row
W: Write the selected line to the file
X: Swap the contents of the staging buffer with the pattern space
Y: Replace the character with another character (you cannot use the Y command on a regular expression)
Note: The red font is marked with the key to master
Options
-E: Multiple edits, which are used when multiple SED commands are applied to the input line
-N: Cancel default output
-F: Specifies the file name of the SED script
Basic use of 5.sed
1>/pattern/p: print lines that match pattern
Use the P command to be aware that SED is outputting the contents of the pending file to the standard output along with the processing results.
So the P command indicates that the lines that match the pattern are printed in addition to the contents of the file. Like a
The contents of file files are
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/83/68/wKiom1dyZQWCuYbhAAAU-FFU0VY805.png-wh_500x0-wm_3 -wmp_4-s_4112053561.png "title=" 1-100 Digui. PNG "alt=" Wkiom1dyzqwcuybhaaau-ffu0vy805.png-wh_50 "/>
The lines that begin with 1 are satisfied and printed:
650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/83/67/wKioL1dyZYPCnuUcAAAXnOXpeU0948.png-wh_500x0-wm_3 -wmp_4-s_832390478.png "title=" 1-100BC. PNG "alt=" Wkiol1dyzypcnuucaaaxnoxpeu0948.png-wh_50 "/>
To output only the processing results, you should add the-n option, which is equivalent to the grep command
650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/83/68/wKiom1dyZfnRPR0tAAAYzgZbFCU161.png-wh_500x0-wm_3 -wmp_4-s_1220942217.png "title=" 1-100while. PNG "alt=" Wkiom1dyzfnrpr0taaayzgzbfcu161.png-wh_50 "/>
2> /pattern/d : Deleting rows that match pattern
Delete the line ending with 6 in file
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/83/68/wKiom1dyZtCBMnd4AAAR9AcOl-Q754.png-wh_500x0-wm_3 -wmp_4-s_1293817635.png "title=" BC seq. PNG "alt=" Wkiom1dyztcbmnd4aaar9acol-q754.png-wh_50 "/>
Note that the SED command does not modify the original file, and the Delete command only indicates that some rows do not print output, rather than deleting from the original file.
3> /pattern/s/pattern1/pattern2/: Find the line that matches the pattern, if you don't add g ,
Replace the first string of the row that matches pattern1 with pattern2, and if you add G , replace all pattern1 of that row with pattern2.
650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/83/68/wKiom1dyaEGTd0lUAAAQI73PZPU815.png-wh_500x0-wm_3 -wmp_4-s_4116588856.png "style=" Float:none; "title=" client. PNG "alt=" Wkiom1dyaegtd0luaaaqi73pzpu815.png-wh_50 "/>
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/83/67/wKioL1dyaELQyQTDAAAUabeiPto516.png-wh_500x0-wm_3 -wmp_4-s_3548722649.png "style=" Float:none; "title=" fib d.png "alt=" Wkiol1dyaelqyqtdaaauabeipto516.png-wh_50 "/>
4> the use of & symbols and \1 in substitutions
The & symbol corresponds to the string itself to be replaced, \1 and & conform to the same effect 650) this.width=650; "Src=" http://s3.51cto.com/wyfs02/M02/83/67/ Wkiol1dya3pybj9baaaqtzmof4k311.png-wh_500x0-wm_3-wmp_4-s_3140469335.png "title=" 1-100 Digui. PNG "alt=" Wkiol1dya3pybj9baaaqtzmof4k311.png-wh_50 "/>
5> Sed-i: The action will modify the original file
650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M02/83/68/wKiom1dybMmzcTfZAAAZQaRPWH8170.png-wh_500x0-wm_3 -wmp_4-s_551490340.png "title=" 1-100 Digui. PNG "alt=" Wkiom1dybmmzctfzaaazqarpwh8170.png-wh_50 "/>
6> addressing
For example, to print 1-2 lines of content in a file
650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M02/83/67/wKioL1dybUig6aLbAAAOLqSls1U307.png-wh_500x0-wm_3 -wmp_4-s_3126930137.png "title=" 1-100 Digui. PNG "alt=" Wkiol1dybuig6albaaaolqsls1u307.png-wh_50 "/>
5. Pattern Space and hold space
sed Normally reads the processed rows into the pattern space, in the script
The "sedcommand (SED command)" is processed one after the other until the script finishes executing. And then
The line is output, the pattern space is emptied, and then, after repeating the action, the file
The new line is read in until the file is processed.
In general, the processing of data uses only pattern space, as the logic above
Can accomplish the main task. But at some point, the use of holding space (hold spaces) can also
Bring unexpected results.
mode space: You can think of the pipeline inside the project, the data between it to deal with.
Maintain space: Can imagine as a warehouse, we are in the process of data processing, as a staging area of information.
Under normal circumstances, if you do not show use some advanced commands, keep space will not be used to!
SED command:
+ g:[address[,address]]g copy contents of hold space to pattern space, the contents of the original pattern space are cleared
+ g:[address[,address]]g Append the contents of hold space to the pattern space\n
+ h:[address[,address]]h Copy the contents of the pattern space into hold space, the original the contents of the hold space are cleared .
+ h:[address[,address]]h Append the contents of the pattern space to the hold space\n
+ d:[address[,address]]d Delete all lines in the pattern and read the next line into the pattern
+ d:[address[,address]]d Delete the first line in multiline pattern, not read in the next line
+ x: Swap to preserve space and pattern space content
1> add a blank line after each line in the test file
650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/83/67/wKioL1dycf3RsL03AAAMCDmhOww099.png-wh_ 500x0-wm_3-wmp_4-s_4001954183.png "style=" Float:none; "title=" 1-100 Digui. PNG "alt=" wkiol1dycf3rsl03aaamcdmhoww099.png-wh_50 "/>650) this.width=650; src=" http://s3.51cto.com/wyfs02/M02 /83/68/wkiom1dycf6tq958aaam4rinj-8899.png-wh_500x0-wm_3-wmp_4-s_349494262.png "title=" 1-100BC. PNG "style=" Float:none; "alt=" Wkiom1dycf6tq958aaam4rinj-8899.png-wh_50 "/>
2> using SED to simulate the function of TAC (reverse output)
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/83/68/wKiom1dycx6CtOEKAAAdYmSiTiQ182.png-wh_500x0-wm_3 -wmp_4-s_266542240.png "title=" 1-100 Digui. PNG "alt=" Wkiom1dycx6ctoekaaadymsitiq182.png-wh_50 "/>
the process of parsing reverse order:
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/83/69/wKiom1dyd1uT_tqGAAA6QDgh3Xk562.png-wh_500x0-wm_3 -wmp_4-s_4132844403.png "title=" 1-100 Digui. PNG "alt=" Wkiom1dyd1ut_tqgaaa6qdgh3xk562.png-wh_50 "/>
If it is the first line, read to the pattern space, and then copy it to hold space, not the most
After the next line, delete the contents of the pattern space,
To the pattern, copying the pattern to hold, repeating the above process, will reverse the contents of the file
The order.
3> append match line to end of file
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/83/69/wKiom1dyevPwhaapAAAZwG1_C-A605.png-wh_500x0-wm_3 -wmp_4-s_4280487654.png "style=" Float:none; "title=" 1-100 Digui. PNG "alt=" Wkiom1dyevpwhaapaaazwg1_c-a605.png-wh_50 "/>
650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/83/68/wKioL1dyevTDh2R2AAAWimVAFqE674.png-wh_500x0-wm_3 -wmp_4-s_4129081130.png "style=" Float:none; "title=" 1-100BC. PNG "alt=" Wkiol1dyevtdh2r2aaawimvafqe674.png-wh_50 "/>
4> Row and column conversions
This article is from the "Learning Journey" blog, please make sure to keep this source http://10541571.blog.51cto.com/10531571/1793782
The working principle and use of SED