Shell Script--sed Tool

Source: Internet
Author: User

Sed

SED: it is called a streaming editor, and its working scenario is typically in the editor, where a piece of text is read in the SED in a row and then output in the specified way, which is its most basic usage.


First, the basic operation of SED:


1,/pattern/action

Pattern is a regular expression that acts on the line that satisfies the pattern

For example

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/83/41/wKiom1duKazSiX3tAACGTNIcYbc267.png "title=" Untitled. png "Width=" "height=" 348 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:400px;height:348px; "alt=" Wkiom1dukazsix3taacgtnicybc267.png "/>

This command is basically the same as grep, where-n means that only the display of the following instructions will be executed, and if you do not add the-N option it will display all the information so that the match is displayed again, p means print, and if you change to D it is deleted. -I will modify the source file


2,/pattern/s/pattern1/pattern2/find the line corresponding to the first pattern, and then convert the PATTERN1 to pattern2

For example

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/83/41/wKioL1duK3zyZ5WvAABdYMNro4g272.png "title=" Untitled. png "Width=" "height=" "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:400px;height:240px; "alt=" Wkiol1duk3zyz5wvaabdymnro4g272.png "/>

The function of G is to replace all the elements in this line that satisfy the conditions.


3, RANGE control

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/83/41/wKioL1duLDmR8sqnAABC2DQDt_8086.png "title=" Untitled. png "Width=" "height=" 158 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:400px;height:158px; "alt=" Wkiol1duldmr8sqnaabc2dqdt_8086.png "/>



Ii. Advanced Operations for SED


The advanced operation of SED comes from its two spaces, one called the pattern space, the other is called the hold space, when does not display the command to use to maintain the space, it has been working in the pattern space, it works like this, reads by the line to the pattern space to operate after the operation, then outputs, and then wins a row.

command to keep space

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

G: After append the contents of hold space to patternspace\n

H: Copy the contents of the pattern space into hold space,

The contents of the original hold space are cleared.

H: After append the contents of the pattern space to holdspace\n

D: Delete all lines in pattern and read into the next line into pattern

D: Delete the first line in multiline pattern without reading the next line

x: Swap to preserve space and pattern space content


Example 1: Add a space to the end of each line in the file above

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/83/42/wKiom1duMVPDdgV8AAAu-rCXYpM086.png "title=" Untitled. png "alt=" Wkiom1dumvpddgv8aaau-rcxypm086.png "/>


Example two: row and column conversion

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/83/42/wKiom1duM8GwkGhlAAAzQftjqxM341.png "title=" Untitled. png "Width=" "height=" 215 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:500px;height:215px; "alt=" Wkiom1dum8gwkghlaaazqftjqxm341.png "/>


Note: The effect of using H is to append each row to the hold mode, and then at the end of the hold space the data is exchanged to

The mode space then performs the substitution operation, which is read by line when SED is read, and is not visible when reading a line, only the

There is data all H to keep space to save up, only to find.


Example three: Reverse print

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/83/41/wKioL1duNrGzu6hdAAA0TydRBGo108.png "title=" Untitled. png "alt=" Wkiol1dunrgzu6hdaaa0tydrbgo108.png "/>


Literally, the first line does not keep space G, the last line does not delete the pattern space

The other lines in the data are first G, then H, and then D.


Example four: Sum of 1 to 100

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/83/41/wKioL1duOiHBHdL3AABHqvXixB4106.png "title=" Untitled. png "Width=" "height=" "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:600px;height:66px; "alt=" Wkiol1duoihbhdl3aabhqvxixb4106.png "/>

This is similar to the one above.



Summary: The learning of SED tools is mostly practice, it is more work in the editor under the operation of the text

For example, the vim compiler bottom-line mode can be replaced with the s///command.

This article is from the "Traces" blog, be sure to keep this source http://wpfbcr.blog.51cto.com/10696766/1792849

Shell Script--sed Tool

Related Article

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.