Practice set of SED commands

Source: Internet
Author: User

Sed is the flow editor, which is handy 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. It is a lightweight flow editor that automates editing without having to edit data directly.

SED works by reading the edited file by the standard input, reading a line or specifying the line into the pattern space, then editing the contents of the schema space one by one, then outputting the result to standard output, and clearing the pattern space, repeating the previous operation until the last line, the flow editor stops, The source file does not change (unless you use the-i option). SED is primarily used to edit one or more files, simplifying the repetitive operation of files.


Let's look at the workflow of SED in the following diagram:

650) this.width=650; "title=" Dp0a$byi2kjrp]3%zox4v2t.png "src=" http://s4.51cto.com/wyfs02/M01/83/48/ Wkiom1durryy8l1aaajrvpncbdw379.png "alt=" Wkiom1durryy8l1aaajrvpncbdw379.png "/>

Sed workflow: The line to be processed is read into the pattern space, then processed, the line is output after processing, and the pattern space is emptied. Then just repeat the operation and read the next line in the file until the file is processed.

We have to be clear because the file content in the pattern space does not change unless the source file is modified by using the redirected store output.

In general, the processing of data only uses pattern space, the main tasks can be completed. But we need to know that at some point, the use of space can also bring unexpected results.

Sed main commands:

H: Copy the contents of the schema space into the current hold buffer, erase the contents of the original staging buffer, and add new content.

H: Appends the contents of the pattern space behind the original contents of the hold buffer.

G: Copies the contents of the current hold buffer to the pattern space, replacing the contents of the current row in the pattern space.

G: Appends the contents of the current hold buffer to the pattern space, appended to the remaining buffer in the back of the original content.

D: Delete all rows of the pattern space and read into the next new line into the pattern space.

D: Deletes the first row in the pattern space and does not read into the next line.

X: Swaps the contents of the hold space and pattern space.

N: Reads the next input line and processes the new line with the next command.

N: Append the next input line to the pattern space.


Let's do a few exercises to practice practiced hand:

    1. A file contains many lines of text, adding a blank line to the end of each line:

650) this.width=650; "Title=" J (hx1ar6av{~g1]hi$v ' jt9.png "src=" http://s2.51cto.com/wyfs02/M00/83/47/ Wkiol1dutpqdogagaabtuaygvlm596.png "alt=" Wkiol1dutpqdogagaabtuaygvlm596.png "/>


2. There are 1-5 numbers in a file, each one in a row, and the number of digits in the file is reversed.

650) this.width=650; "title=" 7%%a8w%8nhztw$l ' WDG ' 2p2.png "src=" http://s3.51cto.com/wyfs02/M00/83/48/ Wkiom1duvuuydrnfaacflqzjcxs001.png "alt=" Wkiom1duvuuydrnfaacflqzjcxs001.png "/>


3. Append one or several matching rows from one file to the end of the file.

650) this.width=650; "title=" R96 ' ['}v33 (qlj[cv]19j%v.png "src=" http://s1.51cto.com/wyfs02/M02/83/48/ Wkiom1duwgmraeafaaczjkhtsb0652.png "alt=" Wkiom1duwgmraeafaaczjkhtsb0652.png "/>


4. Change the number in the original column of the source file to only one row.

650) this.width=650; "title=" Hsj88iu1pq0rzws (%)}0) ~j.png "src=" http://s2.51cto.com/wyfs02/M01/83/47/ Wkiol1duxbdg1mryaabornyp6oi953.png "alt=" Wkiol1duxbdg1mryaabornyp6oi953.png "/>


5. Ask for 1-100 's and.

The first method:

650) this.width=650; "Title=" g8837ywhv1mm (l@_%d$) ooe.png "src=" http://s4.51cto.com/wyfs02/M01/83/48/ Wkiom1duyngsjwcraabsgtpops4386.png "alt=" Wkiom1duyngsjwcraabsgtpops4386.png "/>

The second method:

650) this.width=650; "title=" __sp2q$scq3f) xcqfnzqr$9.png "src=" http://s2.51cto.com/wyfs02/M00/83/48/ Wkiom1duy-rwylgvaabgnarye14772.png "alt=" Wkiom1duy-rwylgvaabgnarye14772.png "/>


6. Print odd and even lines separately.

650) this.width=650; "Title=" ZE{@9L1]6Q@OEMTPN (21_xl.png "src=" Http://s1.51cto.com/wyfs02/M01/83/47/wKioL1duzp_ Aeu1yaabb7gzex20710.png "alt=" Wkiol1duzp_aeu1yaabb7gzex20710.png "/>


The above is their own in the study of the SED all the exercises, but still far from enough, but also more practice!











Practice set of SED commands

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.