Use of SED tools

Source: Internet
Author: User

SED: Streaming editor that introduces the output of the previous program to the input of SED and converts it to another format output after a series of editing commands

Basic format of the command:

SED option ' script ' file1 file2 ...

Sed option-f scriptfile file1 file2 ...

The processed files can be redirected by standard input, or they can be passed in by command-line arguments, and sed processes theincoming files sequentially and then processes them row by line

The edit command for SED can be passed directly to the command line parameter, or it can be written as a script file and then specified by the-f parameter, and the format of the edit command is/pattern/action

Pattern is a regular expression and action is the corresponding action to be performed by a regular expression that matches the condition

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 buffer

Zone, called the pattern space, and then uses the SED command to process the contents of the buffer, and after processing is done, the contents of the buffer are sent to the standard output. Then the next line is processed, so it repeats until the end of the file. This action makes the file content unchanged unless you use redirection to store the output . SED is primarily used to automatically edit one or more files

As shown, the operation process of SED

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/83/67/wKioL1dyZS_g3e-xAABjCrYC9Rw562.png-wh_500x0-wm_3 -wmp_4-s_3077913592.png "title=" Untitled. png "alt=" wkiol1dyzs_g3e-xaabjcryc9rw562.png-wh_50 "/>

Use of SED operations

1. Output lines that match the regular expression, only output lines ending in 2, plus the-n option

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/83/67/wKioL1dyZW3g1OUHAABmHMEmWH0263.png-wh_500x0-wm_3 -wmp_4-s_4088310752.png "title=" seq1 meet the conditions of a certain voyage. png "alt=" wkiol1dyzw3g1ouhaabmhmemwh0263.png-wh_50 "/>

2. Delete the line that matches the regular expression, and delete the line ending with 3

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/83/67/wKioL1dyZxyicfY-AAA3ayij1lE361.png-wh_500x0-wm_3 -wmp_4-s_2092610309.png "title=" 1.png "alt=" Wkiol1dyzxyicfy-aaa3ayij1le361.png-wh_50 "/>

3. Find the line that matches the pattern, replace the string with the first match pattern1 of the line with PATTERN2, need to add s instruction

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/83/68/wKiom1dyaBzwY-56AAAmcoRbOTY075.png-wh_500x0-wm_3 -wmp_4-s_2678063180.png "title=" 2.png "alt=" wkiom1dyabzwy-56aaamcorboty075.png-wh_50 "/>650) this.width=650;" Src= "http://s4.51cto.com/wyfs02/M02/83/67/wKioL1dyamezPqi4AAAp1V-w4tI032.png-wh_500x0-wm_3-wmp_4-s_2920898901. PNG "title=" 4.png "alt=" Wkiol1dyamezpqi4aaap1v-w4ti032.png-wh_50 "/>

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/83/68/wKiom1dyaguhcTxzAAAhVlcjepk300.png-wh_500x0-wm_3 -wmp_4-s_404748992.png "title=" 3.png "alt=" Wkiom1dyaguhctxzaaahvlcjepk300.png-wh_50 "/>

Which uses the-I option to change the contents of the file

4. Find the line that matches the pattern, replace all matching pattern1 strings in the line with pattern2, and add g for full substitution after editing the command

Find the line starting with 1, replacing each 1 of the row with ~1~, where the & symbol represents the contents of Pattern1

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/83/68/wKiom1dybFnRIQH2AAAePdAVYM8309.png-wh_500x0-wm_3 -wmp_4-s_881384864.png "title=" 5.png "alt=" Wkiom1dybfnriqh2aaaepdavym8309.png-wh_50 "/>

Processing of all numeric rows

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/83/68/wKiom1dybW_gLYNgAAAg-rbpkDE314.png-wh_500x0-wm_3 -wmp_4-s_2773547965.png "title=" 6.png "alt=" Wkiom1dybw_glyngaaag-rbpkde314.png-wh_50 "/>

Processing of numbers and alphabetic lines

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/83/68/wKiom1dybrLiX8_UAAAkj3q8gqs598.png-wh_500x0-wm_3 -wmp_4-s_3516126714.png "title=" 7.png "alt=" Wkiom1dybrlix8_uaaakj3q8gqs598.png-wh_50 "/>

Add line break after each line

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/83/68/wKiom1dyb0ihQ2VTAAAtdh2oqj4112.png-wh_500x0-wm_3 -wmp_4-s_1026816739.png "title=" 8.png "alt=" Wkiom1dyb0ihq2vtaaatdh2oqj4112.png-wh_50 "/>

4. Addressing: used to decide which rows to edit. The address can be in the form of a number, regular expression, or a combination of both, and if no address is specified, SED will process all rows of the input file

Print the specified line

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/83/68/wKiom1dycRzAeqbUAAAyZPMBtv0867.png-wh_500x0-wm_3 -wmp_4-s_2946484052.png "title=" 8.png "alt=" Wkiom1dycrzaeqbuaaayzpmbtv0867.png-wh_50 "/>

Print all rows in a range

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/83/67/wKioL1dycdzBkA_dAABCJQVRDBQ732.png-wh_500x0-wm_3 -wmp_4-s_1715435239.png "title=" 8.png "alt=" Wkiol1dycdzbka_daabcjqvrdbq732.png-wh_50 "/>

You can also find the first 15 rows and then the last 5 lines by pipeline, but using the SED command is easier.

If not addressed, SED will display all rows

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/83/68/wKiom1dycuzBmAIfAAAzVz_f3l4142.png-wh_500x0-wm_3 -wmp_4-s_1434468288.png "title=" 8.png "alt=" Wkiom1dycuzbmaifaaazvz_f3l4142.png-wh_50 "/>

Finds lines ending in 1 to 3 and displays

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M02/83/69/wKiom1dydMySZi39AAAz2BAr3eA760.png-wh_500x0-wm_3 -wmp_4-s_1530367569.png "title=" 28.png "alt=" Wkiom1dydmyszi39aaaz2bar3ea760.png-wh_50 "/>

5. Maintain space and mode space conversion related commands and options

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/83/68/wKioL1dydezweMreAAGLuJXXnrE988.png-wh_500x0-wm_3 -wmp_4-s_2315651632.png "title=" 8.png "alt=" Wkiol1dydezwemreaaglujxxnre988.png-wh_50 "/>

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/83/69/wKiom1dyds6S39nLAAB2TdRyiNs731.png-wh_500x0-wm_3 -wmp_4-s_1400702327.png "title=" 8.png "alt=" Wkiom1dyds6s39nlaab2tdryins731.png-wh_50 "/>

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/83/68/wKioL1dyjP2DHFgmAAAizuNBkj4649.png-wh_500x0-wm_3 -wmp_4-s_3068086323.png "title=" 8.png "alt=" Wkiol1dyjp2dhfgmaaaizunbkj4649.png-wh_50 "/>

1. Add a blank line to the end of each line, because the contents of the start of the space are empty, so the contents of the hold space are appended to the pattern space, and then a blank line is added to each line

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/83/68/wKioL1dydy_AIPv2AAArY793jmA665.png-wh_500x0-wm_3 -wmp_4-s_3139063882.png "title=" 8.png "alt=" Wkiol1dydy_aipv2aaary793jma665.png-wh_50 "/>

2. Reverse the contents of the output file

You can use the TAC tool to output

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/83/69/wKiom1dyd_-CMgHDAAApC_zKnMg130.png-wh_500x0-wm_3 -wmp_4-s_2841511937.png "title=" 8.png "alt=" Wkiom1dyd_-cmghdaaapc_zknmg130.png-wh_50 "/>

Use the SED command to implement

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/83/68/wKioL1dyeG3RBSpgAAAxH1pcts8324.png-wh_500x0-wm_3 -wmp_4-s_1848390728.png "title=" 8.png "alt=" Wkiol1dyeg3rbspgaaaxh1pcts8324.png-wh_50 "/>

The edit command is: If the 1th line is not done immediately after the G operation, but do H (copy the schema space content to maintain space, delete the original content of the space) operation, that is, the contents of the space is not appended to the mode space, if not the 1th row, then do g operation, if the last line, then do not do D, If it is not the last row, the contents of the schema space are deleted and the next new line is read into the pattern space

3. Append matching lines to end of file

Find ends with 9 and append to end of file

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/83/69/wKiom1dyesmincXiAABDu5lwecA221.png-wh_500x0-wm_3 -wmp_4-s_1016481551.png "title=" 8.png "alt=" Wkiom1dyesmincxiaabdu5lweca221.png-wh_50 "/>

If the first line is the H operation, then copy once

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/83/68/wKioL1dye3bDoHicAAA-lSk0fKk911.png-wh_500x0-wm_3 -wmp_4-s_214347333.png "title=" 8.png "alt=" Wkiol1dye3bdohicaaa-lsk0fkk911.png-wh_50 "/>

4. Row and column conversions

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/83/69/wKiom1dye8nDTc1iAABYyP3ZhuE377.png-wh_500x0-wm_3 -wmp_4-s_1947003275.png "title=" 8.png "alt=" Wkiom1dye8ndtc1iaabyyp3zhue377.png-wh_50 "/>

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/83/68/wKioL1dygWChSNUjAAAoTDtMMG8123.png-wh_500x0-wm_3 -wmp_4-s_1602432559.png "title=" 8.png "alt=" Wkiol1dygwchsnujaaaotdtmmg8123.png-wh_50 "/>

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/83/69/wKiom1dygb7CuINCAABZmamc7uk114.png-wh_500x0-wm_3 -wmp_4-s_1062598212.png "title=" 8.png "alt=" Wkiom1dygb7cuincaabzmamc7uk114.png-wh_50 "/>

X at the end of the pattern space line, with a space instead of the end of each line

Sum of 5.1 to 100

H means to append the contents of the pattern space to the hold space, H can take an address, such as $, to the end of the file, and then use X to take it to the pattern space, replace \ n with a space, and then print

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/83/68/wKioL1dyhFCy93qZAABVXsc_BUc373.png-wh_500x0-wm_3 -wmp_4-s_671516239.png "title=" 8.png "alt=" Wkiol1dyhfcy93qzaabvxsc_buc373.png-wh_50 "/>

You can also use the C-type goto statement

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/83/68/wKioL1dyhbWBWm62AABBqkURGW8418.png-wh_500x0-wm_3 -wmp_4-s_2892329191.png "title=" 8.png "alt=" Wkiol1dyhbwbwm62aabbqkurgw8418.png-wh_50 "/>

: A means that the label A,ba represents a jump (break) to a tag, $! Indicates that the last line does not follow,$!ba indicates that the last line does not jump to label A, end this operation

6. Print even rows or cardinality lines

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/83/69/wKiom1dyhqfxbszVAABFFsuv3L8965.png-wh_500x0-wm_3 -wmp_4-s_2843627259.png "title=" 8.png "alt=" Wkiom1dyhqfxbszvaabffsuv3l8965.png-wh_50 "/>

7. Annotation Conversions

Add comments to each line, then use the following command in the bottom row mode

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/83/68/wKioL1dyh9ewXxehAAA4zvfNCQA838.png-wh_500x0-wm_3 -wmp_4-s_3042729038.png "title=" 8.png "alt=" Wkiol1dyh9ewxxehaaa4zvfncqa838.png-wh_50 "/>

Comment on the line that satisfies the regular expression

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/83/68/wKioL1dyiMLyiZt3AAAubCGozaI516.png-wh_500x0-wm_3 -wmp_4-s_4196323242.png "title=" 8.png "alt=" Wkiol1dyimlyizt3aaaubcgozai516.png-wh_50 "/>

Add a comment to a line that satisfies a regular expression

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/83/68/wKioL1dyieCxnfsXAAA0oaI_Jzg694.png-wh_500x0-wm_3 -wmp_4-s_2501257678.png "title=" 8.png "alt=" Wkiol1dyiecxnfsxaaa0oai_jzg694.png-wh_50 "/>

Start with a line and add the second half comment

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/83/69/wKiom1dyjFWSPoE1AAA_jw87gnQ659.png-wh_500x0-wm_3 -wmp_4-s_3388248080.png "title=" 8.png "alt=" Wkiom1dyjfwspoe1aaa_jw87gnq659.png-wh_50 "/>

Annotation conversions

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/83/69/wKiom1dyimvAEwFxAABBzIfyjV4761.png-wh_500x0-wm_3 -wmp_4-s_595429841.png "title=" 8.png "alt=" Wkiom1dyimvaewfxaabbzifyjv4761.png-wh_50 "/>

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/83/68/wKioL1dyizyzAR7aAAA0SLmCFss398.png-wh_500x0-wm_3 -wmp_4-s_3679332828.png "title=" 8.png "alt=" Wkiol1dyizyzar7aaaa0slmcfss398.png-wh_50 "/>


Use of SED tools

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.