Application of sed (i.)

Source: Internet
Author: User

First, sed Use of Commands

Sed is an online editor that processes a single line of content at a time. When processing, the currently processed rows are stored in a temporary buffer called pattern space, followed by the SED command to process the contents of the buffer, and after processing is done, the contents of the buffer are sent to the screen. Then the next line is processed, so it repeats until the end of the file. The file content does not change unless you use redirection to store the output.

Basic commands for SED:

1. Replace: s command
1.1 Basic usage

such as: sed ' s/day/night/' <old >new
This example replaces the day of the first occurrence of each line in the file old with night, outputting the result to the file new

S "Replace" command
/.. /.. /Separator (Delimiter)
Day search string
Night Replacement string
In fact, the separator "/" can be replaced by other symbols, such as ",", "|" and so on.
such as: sed ' s/\/usr\/local\/bin/\/common\/bin/' <old >new
Equivalent to sed ' s_/usr/local/bin_/common/bin_ ' <old >new
Obviously, it's much better to use "_" as a separator than "/".

1.2 Using & to represent matching strings

Sometimes you might want to add some characters around or near the matched string.
such as: sed ' s/abc/(ABC)/' <old >new

This example adds parentheses around the found ABC.
This example can also be written as Sed ' s/abc/(&)/' <old >new

The following are more complex examples:
Sed ' s/[a-z]*/(&)/' <old >new

SED replaces the first occurrence of the search string by default, using/g to replace the search string with all

$ sed ' s/test/mytest/g ' example-----Replace test with mytest in the entire row range. If there is no G tag, only the first matching test of each row is replaced with mytest.

$ sed ' s/^192.168.0.1/&localhost/' example-----& symbol represents the part found in the replacement string. All lines starting with 192.168.0.1 will be replaced with a self-added localhost, which becomes 192.168.0.1localhost.

$ sed ' s#10#100#g ' example-----no matter what character, followed by the S command is considered a new delimiter, so, "#" Here is the delimiter, instead of the default "/" delimiter. means to replace all 10 with 100.

If you need to make multiple modifications to the same file or row, you can use the "-e" option

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7A/0B/wKiom1ahp2PDc2vXAAAGKjAh1uw876.png "title=" 1.png " alt= "Wkiom1ahp2pdc2vxaaagkjah1uw876.png"/>

Get eth0 network card IP address:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7A/0B/wKiom1ahp3LCI0o5AAAPaekDIxg372.png "title=" 2.png " alt= "Wkiom1ahp3lci0o5aaapaekdixg372.png"/>

2. Delete line: D command

Remove all rows that contain "how" from a file

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7A/0B/wKiom1ahp4ay3WtKAAADo6-Rl_M779.png "title=" 3.png " alt= "Wkiom1ahp4ay3wtkaaado6-rl_m779.png"/>

Displays the contents of the/etc/passwd and prints the line number while deleting the 2~5

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7A/0A/wKioL1ahp-Dit967AAAbkss-A5M381.png "title=" 4.png " alt= "Wkiol1ahp-dit967aaabkss-a5m381.png"/>

Attached: The NL command is used in the Linux system to calculate the line number of a file. NL can automatically add the output file content to the line number

If you want to delete line 2nd, you can use NL/ETC/PASSWD | Sed ' 2d ' to achieve, as if to delete the 3rd to the last line, it is NL/ETC/PASSWD | Sed ' 3, $d '.

3. Add row: A command (added after the specified line) or I command (added before the specified line)

A can be followed by strings, and the strings appear in a new line

Add a new line to the word "XXXXX" after the second line of/etc/passwd

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7A/0A/wKioL1ahqBTz-f0FAAAfZlSG88Y414.png "title=" 5.png " alt= "Wkiol1ahqbtz-f0faaafzlsg88y414.png"/>

Add a new line to the word "XXXXX" before the second line of/etc/passwd

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7A/0A/wKioL1ahqCXSGyYkAAAfSNUSyWs195.png "title=" 6.png " alt= "Wkiol1ahqcxsgyykaaafsnusyws195.png"/>

If you want to add more than one row at a time, use a backslash \ for adding new rows between each row

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7A/0B/wKioL1ahqObROiUkAAAmYnUFXkI080.png "title=" 7.png " alt= "Wkiol1ahqobroiukaaamynufxki080.png"/>

4. Replace line: C command

C can be followed by strings, which can replace rows between n1,n2

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7A/0B/wKioL1ahqQGDcEZnAAAfnV8rDhA792.png "title=" 8.png " alt= "Wkiol1ahqqgdceznaaafnv8rdha792.png"/>

5. Print: P command

Sed '/north/p ' datafile output all rows by default, find rows in north to repeat print

Sed–n '/north/p ' datafile suppresses default output, prints only rows found north

nl/etc/passwd | Sed-n ' 5,7p ' lists only the 5th to 7th line of content in the/etc/passwd file

Note: the-i option of SED can directly modify the contents of the file

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7A/0C/wKiom1ahqSiDUmloAAAJbDUvSvo766.png "title=" I.png " alt= "Wkiom1ahqsidumloaaajbduvsvo766.png"/>

6. Extended:

There are three ways of calling Sed:

L type commands at the command line

L INSERT the SED command into the script file and then call SED

L INSERT the SED command into the script file and make the SED script executable.

A, use the SED command-line format as:

sed [Options] sed command input file.

Remember to add single quotes to the actual command when using the SED command at the command line. SED also allows double quotes.

B. Use the SED script file in the following format:

sed Options - f sed script file input file

C, to use the SED script file with the SED command interpreter in the first line, in the following format:

sed script file [options] input file

Whether you use the shell command line or the script file, sed accepts input from standard input, typically a keyboard or redirection result, if no input file is specified.

The SED options are as follows:

-F,--filer=script-file boot sed script file name

General Examples:

The Test.txt is processed by the SED script, and the contents of the Test.txt file are as follows:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7A/0B/wKioL1ahqaOzy8i2AAAdEx0XA4w527.png "title=" 9.png " alt= "Wkiol1ahqaozy8i2aaadex0xa4w527.png"/>

Create sed script file append.sed, add content to test.txt via sed script, with the following script:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7A/0B/wKioL1ahqbGCXLsmAAAN71xq2Dw959.png "title=" 10.png "alt=" Wkiol1ahqbgcxlsmaaan71xq2dw959.png "/>

Save it and add executable permissions: chmod +x append.sed

Run Script append.sed

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7A/0B/wKioL1ahqbyhCSnzAAAJOVimCyM919.png "title=" 11.png "alt=" Wkiol1ahqbyhcsnzaaajovimcym919.png "/>

The results appear as follows:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7A/0C/wKiom1ahqYzS-gUTAAAlhlTyZYI696.png "title=" 12.png "alt=" Wkiom1ahqyzs-gutaaalhltyzyi696.png "/>

Now look at its specific features.

The first line is the SED command interpretation line. The script finds the SED in this line to run the command, which is located in/bin.

The second line starts with/company/, which is the starting position for the attach operation. A\ notify SED This is an additional operation, first insert two new rows.

The third to fourth line is the actual text of the attach operation to be added to the copy.



This article is from the "Small White blog" blog, please be sure to keep this source http://xiaobai1981.blog.51cto.com/9649602/1737575

Application of sed (i.)

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.