Sed is one of the most commonly used commands in operations, and it's time to take notes and record them on weekends. SED is a line-of-text editor that is processed on a per-row basis for plain ASCII code. sed when the text is processed, row-by-line content is read into memory for processing and the processing result is output. This memory space is called "pattern space" because sed does not have to be processed on every line, and some rows can be processed according to certain patterns. By default, the source file is not modified, only the data in the pattern space is processed. Prints the contents of the pattern space after processing is finished.
Sed several grammar rules:
1.sed startline,endline file
From the starting line to the end line, such as 1,100, from the first row to the line
2. Pattern Matching rows
Sed/^root/file lines starting with root
3. / mode 1/,/ mode 2/ indicates that the first time the file is matched to the line of pattern 1 to the first time by mode 2 Match to the end of the line, all the lines in the middle
4. give only one value , line number means specifying a specific row precisely
Sed 100 file Precise handling of section 100 line ,$ sed $ file $-1 Represents the penultimate line
5. specify the starting line, using +n, to start with the specified line , N rows backward , and a total of n+1 Line Contents
Sed 2,+10 file
Several commands commonly used in SED:
d: Delete the specified row
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/70/30/wKiom1WzVSDjfM2YAABc4wEXzrU490.jpg "style=" float: none; "title=" Image 1.png "alt=" Wkiom1wzvsdjfm2yaabc4wexzru490.jpg "/>
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/70/2D/wKioL1WzVwiDS3jXAABdRwZSfco192.jpg "style=" float: none; "title=" Image 2.png "alt=" Wkiol1wzvwids3jxaabdrwzsfco192.jpg "/>
from the first line, delete the backward line , deleting two rows altogether
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/70/30/wKiom1WzVVqxQwnAAABu4AgFr84117.jpg "title=" Picture 3.png "alt=" Wkiom1wzvvqxqwnaaabu4agfr84117.jpg "/>
P: show rows that match the criteria
The following example shows that the condition is displayed two times, because the sed command also displays the contents of the pattern space by default, and the p command displays the matching rows, so there are two lines
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/70/30/wKiom1WzVfuQgAnWAABbZ2c3mJY525.jpg "title=" Picture 4.png "alt=" wkiom1wzvfuqganwaabbz2c3mjy525.jpg "/>
-N Displays only the rows that match the criteria:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/70/2D/wKioL1WzWDaQIQhbAABFycwsGlw333.jpg "title=" Picture 5.png "alt=" Wkiol1wzwdaqiqhbaabfycwsglw333.jpg "/>
A: use a \string--- to append a new row after the specified line, with the contents of string
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/70/30/wKiom1WzVnjTKBzDAABqgQPlj0M804.jpg "title=" Picture 6.png "alt=" Wkiom1wzvnjtkbzdaabqgqplj0m804.jpg "/>
to add two rows after matching a row , the string \ n wraps the line
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/70/2D/wKioL1WzWH_yMolwAAClYtfXh0A937.jpg "title=" Picture 7.png "alt=" Wkiol1wzwh_ymolwaaclytfxh0a937.jpg "/>
I \string: indicates that a new line is added before the specified line and the contents are string,\n wrapped
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/70/30/wKiom1WzVuCwiDO5AADY2rRIW38512.jpg "title=" Picture 8.png "alt=" Wkiom1wzvucwido5aady2rriw38512.jpg "/>
R FileName: adds the contents of the specified file to the qualifying line
Add the contents of another file behind the second line,/ROOT/TT is another path to the text file
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/70/30/wKiom1WzVxnSbx5OAADL79r6QFQ133.jpg "title=" Picture 9.png "alt=" Wkiom1wzvxnsbx5oaadl79r6qfq133.jpg "/>
Multi-line processing
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/70/2D/wKioL1WzWR_xBAzbAACc-kvhGmc066.jpg "title=" Picture 10.png "alt=" Wkiol1wzwr_xbazbaacc-kvhgmc066.jpg "/>
W FileName: Saves content in the specified range to the specified file
In the example below,/tmp/t2 is the file path
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/70/30/wKiom1WzV4yhV9EkAACoIhp-vnQ008.jpg "title=" Picture 11.png "alt=" Wkiom1wzv4yhv9ekaacoihp-vnq008.jpg "/>
s : s/ match mode / What to replace /    delimiters are not limited to / s# match mode # What to replace #
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/70/2D/wKioL1WzWcCwK8AZAABjf6LRvB0191.jpg "title=" Picture 12.png "alt=" Wkiol1wzwccwk8azaabjf6lrvb0191.jpg "/>
The default is to replace only the first occurrence of the string in each line
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/70/30/wKiom1WzWAPQpmutAAB3Jw2CEG8315.jpg "title=" Picture 13.png "alt=" Wkiom1wzwapqpmutaab3jw2ceg8315.jpg "/>
If you want to replace all the strings that match in each row, you need to use the following two parameters:
G: Global substitution
I: ignoring character capitalization when looking up
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/70/2D/wKioL1WzWjSCOvGeAAB7AT5ngPw829.jpg "style=" float: none; "title=" Image 14.png "alt=" Wkiol1wzwjscovgeaab7at5ngpw829.jpg "/>
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/70/30/wKiom1WzWEvj6mYiAABpt08IRAE018.jpg "style=" float: none; "title=" Image 15.png "alt=" Wkiom1wzwevj6myiaabpt08irae018.jpg "/>
a back reference in SED:
& represents the entire string for a reference pattern match
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/70/2D/wKioL1WzWnXxEwQcAADYNkBEVYU713.jpg "title=" Picture 16.png "alt=" Wkiol1wzwnxxewqcaadynkbevyu713.jpg "/>
Back reference:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/70/30/wKiom1WzWK6TUgSkAACWngWYsP8089.jpg "title=" Picture 17.png "alt=" Wkiom1wzwk6tugskaacwngwysp8089.jpg "/>
You must use a back reference when you reference only part of the content that matches to
If you capitalize the l character of the matched word
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/70/2D/wKioL1WzWsfC_k8eAADiwqjsm-8579.jpg "title=" Picture 18.png "alt=" Wkiol1wzwsfc_k8eaadiwqjsm-8579.jpg "/>
Sed does not modify the source file by default, and if you need to modify the source file directly, use the-i option.
-r: sed only supports basic regularization by default ,-R enables sed to use extended regular expressions
For example, delete the white space character at the beginning of the line in the file, preceded by a white space character , and #
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/70/30/wKiom1WzWU2REiJ8AADkezxFapI192.jpg "title=" Picture 19.png "alt=" Wkiom1wzwu2reij8aadkezxfapi192.jpg "/>
This article is from the "Diannaowa" blog, make sure to keep this source http://diannaowa.blog.51cto.com/3219919/1678320
Linux-sed Command Detailed