SED and regular expressions

Source: Internet
Author: User

I. Basic concepts of SED

Sed, the streaming editor, which introduces the previous program output to the SED input, is converted to another format output after a series of editing commands. The basic regular expression specification is used by default, and the extended specification is used if the-r option is added.

In the basic specification, the +,? , {}, (), | are common meanings, and if you want to use their special meanings, you need to use \ Escape, which is the opposite in the extended specification.

Sed in the processing of a line of content, the current processing of the navigation in the temporary buffer (memory), called the pattern space, and then use the SED command to process the contents of the pattern space, after processing is completed, the content to the screen (but at some point, can use to hold space to stage the data), and then repeatedly processing the next row, Until the end of the file.


Ii. Basic use of SED

(1)/pattern/p: Print matching lines

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s2.51cto.com/wyfs02/m02/83/87/wkiom1d1djktnnteaaazmhgymw0217.png "title=" P.png "alt=" Wkiom1d1djktnnteaaazmhgymw0217.png "/>


(2)/pattern/d: Delete matching rows

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s1.51cto.com/wyfs02/m00/83/87/wkiom1d1dlkrc7ngaabd8cl5gb4632.png "title=" D.png "alt=" Wkiom1d1dlkrc7ngaabd8cl5gb4632.png "/>

Note: The SED command does not modify the original file, only indicates that some lines do not print output, and if the plus-i option modifies the original file

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/83/87/wKiom1d1EUCjQP4dAAAm4RGe5IY843.png "title="-i.png "alt=" Wkiom1d1eucjqp4daaam4rge5iy843.png "/>


(3)/pattern/s/pattern1/pattern2/: Find the line that matches the pattern and replace the string with the first match pattern1 of the line with PATTERN2

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s2.51cto.com/wyfs02/m01/83/87/wkiom1d1doeiayn3aaberads6wu868.png "title=" replaces. png "alt=" Wkiom1d1doeiayn3aaberads6wu868.png "/>


(4)/pattern/s/pattern1/pattern2/g: Find the line that matches the pattern and replace the string with PATTERN1 for each line matching the pattern2

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/83/85/wKioL1d1DqyjCBNNAABf8mC2cVY636.png "style=" float: none; "title=" G replaces 1.png "alt=" Wkiol1d1dqyjcbnnaabf8mc2cvy636.png "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/83/85/wKioL1d1Dqyy_KZ6AAAuQjybb-w494.png "style=" float: none; "title=" G replaces 2.png "alt=" Wkiol1d1dqyy_kz6aaauqjybb-w494.png "/>


(5) addressing

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/83/87/wKiom1d1DuyyKi06AABnAXbNESA067.png "style=" float: none; "title=" Addressing 1.png "alt=" Wkiom1d1duyyki06aabnaxbnesa067.png "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/83/85/wKioL1d1Du2ha8tVAAAvhyIMqp8368.png "style=" float: none; "title=" Addressing 2.png "alt=" Wkiol1d1du2ha8tvaaavhyimqp8368.png "/>

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>

Third, regular expression

(1) ^: Beginning of Line locator

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m00/83/85/wkiol1d1dwybznimaaa-lcldh2g152.png "title=" ^.png "alt=" Wkiol1d1dwybznimaaa-lcldh2g152.png "/>


(2) $: Line End Locator

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s1.51cto.com/wyfs02/m00/83/87/wkiom1d1dyarz48uaaazotb5kpo736.png "title=" $.png "alt=" Wkiom1d1dyarz48uaaazotb5kpo736.png "/>


(3).: Matches a single character other than the line break

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m01/83/85/wkiol1d1dz-gpufcaaa29tk8_gi583.png "title=" M.. Y.png "alt=" Wkiol1d1dz-gpufcaaa29tk8_gi583.png "/>


(4) *: Match 0 or more preamble units

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s5.51cto.com/wyfs02/m01/83/87/wkiom1d1d1ur_yptaabh42svo2e635.png "title=" asterisk. png "alt=" wKiom1d1D1uR_ Yptaabh42svo2e635.png "/>


(5) []: matches any character within a specified character group

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s2.51cto.com/wyfs02/m02/83/85/wkiol1d1d4ts8o66aaaug_myggw036.png "title=" "". png "alt=" WKioL1d1D4TS8O66AAAug_ Myggw036.png "/>


(6) [^]: matches any character not in the specified character group

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s1.51cto.com/wyfs02/m02/83/87/wkiom1d1d5bzwucoaaat8kycuu4118.png "title=" "^". png "alt=" Wkiom1d1d5bzwucoaaat8kycuu4118.png "/>


(7) (): Save the matched character, save as label 1, you can use \1 to refer to the

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s1.51cto.com/wyfs02/m01/83/85/wkiol1d1er2yyzg1aaa4zyz7mrs560.png "title=" back slash 1.png "alt=" Wkiol1d1er2yyzg1aaa4zyz7mrs560.png "/>


(8) &: Save the searched string so that it is referenced in the replacement string

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s4.51cto.com/wyfs02/m00/83/85/wkiol1d1eiuzwcfcaaa0mlimqok105.png "title=" &.png "alt=" Wkiol1d1eiuzwcfcaaa0mlimqok105.png "/>


(9) \<: Word head Locator

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m00/83/87/wkiom1d1ekmxkm5paaaw16x3n7u198.png "title=", ". png" alt= " Wkiom1d1ekmxkm5paaaw16x3n7u198.png "/>


(ten) \>: Ending locator

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s1.51cto.com/wyfs02/m01/83/85/wkiol1d1el2xcrzkaaax1_bh-ms843.png "title=", ". png" alt= "wKioL1d1EL2xCrZKAAAx1_ Bh-ms843.png "/>


(one) v\{m\}: Continuous M V

V\{m,\}: At least M v

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m00/83/87/wkiom1d1em_ho5xwaabmewa9o5w022.png "title=" at least. png "alt=" wKiom1d1EM_ Ho5xwaabmewa9o5w022.png "/>


Iv. sed command

G: Copy The contents of the hold space into the pattern space

G: Append The contents of the hold space to the pattern space

H: copy The contents of the pattern space into the hold space

H: Append The contents of the pattern space to the hold space

D: Delete the first row in the pattern space and read in the next line

D: Delete the first row in the pattern space without reading the next line

x: swap mode space and preserve space content


V. The exercises of the SED

(1) Add a blank line to the end of each line

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m00/83/85/wkiol1d1d-la4-bbaaa5nyvgzkw865.png "title=" 1.png "alt=" Wkiol1d1d-la4-bbaaa5nyvgzkw865.png "/>


(2) Simulate TAC with sed (reverse output)

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s1.51cto.com/wyfs02/m02/83/87/wkiom1d1d_grwrggaabmwoezqo4377.png "title=" 2.png "alt=" wKiom1d1D_ Grwrggaabmwoezqo4377.png "/>


(3) Append match line to end of file

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s2.51cto.com/wyfs02/m00/83/87/wkiom1d1eahz6fvtaabfwxntcmc368.png "title=" 3.png "alt=" Wkiom1d1eahz6fvtaabfwxntcmc368.png "/>


(4) Line Column Conversions

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s4.51cto.com/wyfs02/m01/83/85/wkiol1d1ea-zqgajaabcwjm1_da772.png "title=" 4.png "alt=" wKioL1d1EA-zqgAJAABCwjM1 _da772.png "/>


(5) 1+...+100

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m01/83/87/wkiom1d1eb3zlfugaabwgrpr2jg563.png "title=" 5.png "alt=" Wkiom1d1eb3zlfugaabwgrpr2jg563.png "/>


(6) Output odd and even lines

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s2.51cto.com/wyfs02/m02/83/85/wkiol1d1ec7s2l2gaabamn9lceg890.png "title=" 6.png "alt=" Wkiol1d1ec7s2l2gaabamn9lceg890.png "/>


(7) 1+...+100 (Label method)

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s5.51cto.com/wyfs02/m02/83/87/wkiom1d1edzrc_oqaabwqxws16m483.png "title=" 7.png "alt=" wKiom1d1EDzRc_ Oqaabwqxws16m483.png "/>

Note: A means that the label A,ba means jump to the label A,$!ba means that the last line does not jump to label a.


This article is from "GREEN" blog, please make sure to keep this source http://green906.blog.51cto.com/10697569/1794711

SED and regular expressions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.