Linux self-study note--sed command

Source: Internet
Author: User
Tags line editor


sed Line Editor:

sed is a row editor that processes a single line of content at a time. When processing, the currently processed row is stored in the temporary storage area, called the "pattern Space", and then the SED command to process the contents of the buffer, after processing, the contents of the buffer to the screen. Proceed to the next line, so repeat and know the end of the file. The contents of the file do not change unless you use the redirect store output or with the edit option. SED is mainly used to automatically edit one or more files, to simplify the repeated operation of the file, to write the conversion program and so on.

sed Usage:

Sed-stream Editor for filtering andtransforming text ;

Sed-stream Editor for filtering andtransforming text ;

Common options:

- N : Does not output the contents of the mode to the screen;

650) this.width=650; "title=" 1.png "src=" https://s5.51cto.com/wyfs02/M00/06/91/wKiom1m6NEGQ2fgiAABZtN_ Z4ni344.png-wh_500x0-wm_3-wmp_4-s_3441608599.png "alt=" Wkiom1m6negq2fgiaabztn_z4ni344.png-wh_50 "/>

- e : Multi-point editing;

650) this.width=650; "title=" 2.png "src=" https://s1.51cto.com/wyfs02/M00/A5/42/ Wkiol1m6nb-alniwaabmm7q-e1k596.png-wh_500x0-wm_3-wmp_4-s_1064428809.png "alt=" Wkiol1m6nb-alniwaabmm7q-e1k596.png-wh_50 "/>

-f/path/to/script_file : Reads the edit script from the specified file;

650) this.width=650; "title=" 3.png "src=" https://s3.51cto.com/wyfs02/M02/06/92/ Wkiom1m6nflwx8v9aabiw7amdqw966.png-wh_500x0-wm_3-wmp_4-s_3333250241.png "alt=" Wkiom1m6nflwx8v9aabiw7amdqw966.png-wh_50 "/>

- R : Supports the use of extended regular expressions;

- I. : in situ editing;

650) this.width=650; "title=" 4.png "src=" https://s1.51cto.com/wyfs02/M02/06/92/ Wkiom1m6nfqjjbsdaab5vnppxhy147.png-wh_500x0-wm_3-wmp_4-s_1459647526.png "alt=" Wkiom1m6nfqjjbsdaab5vnppxhy147.png-wh_50 "/>

Address delimitation:

s) do not give the address, the full text to deal with;

2) single address:

# : The specified row;

/pattern/ : Each row to which the pattern can be matched;

3) address range

# , #:

# , +#:

/pat1/,/pat2/ :

# ,/pat1/:

4) ~: Step forward

A :

Edit command:

D : delete;

P : Displays the contents of the mode space;

a \text : Append text after line, support using \ n to implement multiline append;

I \text : Inserting text in front of the line, support for multi-row insertion using \ n;

C \text : Replaces the line of behavior or multiline text;

W/path/to/somefile : Saves the line of pattern space to the specified file;

R/path/to/somefile : reads the text stream of the specified file after the line of the line that matches to the pattern;

      = : Prints line numbers for lines in the pattern space;

! : Take the reverse condition;

s/// : Supports the use of other separators, [email protected]@@,s###

replace tag:

g : In-line global substitution;

P : Displays the row where the replacement was successful;

W/path/to/somefile : Saves the result of the replacement success to the specified file;

Exercise 1: Delete all whitespace characters in the/boot/grub/grub.cong file that begin with whitespace at the beginning of a line

650) this.width=650; "title=" 5.png "src=" https://s5.51cto.com/wyfs02/m00/06/92/wkiom1m6ngwy5r_- Aacxsu10cby183.png-wh_500x0-wm_3-wmp_4-s_3080104705.png "alt=" Wkiom1m6ngwy5r_-aacxsu10cby183.png-wh_50 "/>

Exercise 2: Remove all # and white space characters from the beginning of the line at the beginning of the/etc/fstab file with #, followed by at least one white space character;

650) this.width=650; "title=" 6.png "src=" https://s1.51cto.com/wyfs02/M02/A5/42/ Wkiol1m6nephtkm2aabqdqdpwsy945.png-wh_500x0-wm_3-wmp_4-s_1761693227.png "alt=" Wkiol1m6nephtkm2aabqdqdpwsy945.png-wh_50 "/>

Practice 3:echo an absolute path to the SED command, take out its base name, and take out its directory name

650) this.width=650; "title=" 7.png "src=" https://s5.51cto.com/wyfs02/M01/06/92/ Wkiom1m6nhwjepbmaaamooxajzy830.png-wh_500x0-wm_3-wmp_4-s_2999574534.png "alt=" Wkiom1m6nhwjepbmaaamooxajzy830.png-wh_50 "/>

Advanced Editing Commands:

h : The contents of the pattern space are covered in the holding space;

H : Append the contents of the pattern space to the holding space;

g : Data from the holding space is overwritten to the mode space;

G : Append data from Hold space to mode space;

x : To interchange the contents of the pattern space with the contents of the keeping space;

N : Reads the next line from the matching line to the mode space;

N : Appends the next row of rows to the mode space;

D : Delete rows in the pattern space;

D : Delete all rows in multiline mode space;

Example:

1. in the/tmp directory to create a new file Test.text, edit the contents of the file as follows;

650) this.width=650; "title=" 8.png "src=" https://s2.51cto.com/wyfs02/M01/06/92/ Wkiom1m6njsbjiosaaa9regm57a346.png-wh_500x0-wm_3-wmp_4-s_1870603005.png "alt=" Wkiom1m6njsbjiosaaa9regm57a346.png-wh_50 "/>

2. Print even lines:]# sed-n ' n;p ' Test.text

650) this.width=650; "title=" 9.png "src=" https://s2.51cto.com/wyfs02/M02/06/92/wKiom1m6NJ2wiQm0AAAKqH_ Ulte425.png-wh_500x0-wm_3-wmp_4-s_3720377099.png "alt=" Wkiom1m6nj2wiqm0aaakqh_ulte425.png-wh_50 "/>

3. reverse display of file contents:]# sed ' 1! G;h;$!d ' Test.text

650) this.width=650; "title=" 10.png "src=" https://s4.51cto.com/wyfs02/M02/A5/42/ Wkiol1m6nhvjtk7jaaaompmbbhi002.png-wh_500x0-wm_3-wmp_4-s_2201311708.png "alt=" Wkiol1m6nhvjtk7jaaaompmbbhi002.png-wh_50 "/>

4. two lines after removing the file:]# sed ' $! n;$! D ' Test.text

650) this.width=650; "title=" 11.png "src=" https://s4.51cto.com/wyfs02/M00/06/92/ Wkiom1m6nk2qttsraaamk0vnipy557.png-wh_500x0-wm_3-wmp_4-s_2845262012.png "alt=" Wkiom1m6nk2qttsraaamk0vnipy557.png-wh_50 "/>

5. Take out the last line of the file:]# sed ' $!d ' test.text

650) this.width=650; "title=" 12.png "src=" https://s3.51cto.com/wyfs02/M02/06/92/ Wkiom1m6nlwy364kaaakpiyztje380.png-wh_500x0-wm_3-wmp_4-s_2602822566.png "alt=" Wkiom1m6nlwy364kaaakpiyztje380.png-wh_50 "/>

6. Add a blank line below each line of the contents of the file:]# sed ' G ' test.text

650) this.width=650; "title=" 13.png "src=" https://s1.51cto.com/wyfs02/M01/06/92/wKiom1m6NL7hE_ 4faaajz7d2uuw027.png-wh_500x0-wm_3-wmp_4-s_3892856727.png "alt=" Wkiom1m6nl7he_4faaajz7d2uuw027.png-wh_50 "/>

7. combine blank lines in a file into one, with no blank lines between them:]# sed '/^$/d; G ' Test.text

650) this.width=650; "title=" 14.png "src=" https://s3.51cto.com/wyfs02/M01/A5/42/ Wkiol1m6njyjk777aaaxeg-xj0m652.png-wh_500x0-wm_3-wmp_4-s_4259150067.png "alt=" Wkiol1m6njyjk777aaaxeg-xj0m652.png-wh_50 "/>

8. Show Odd lines:]# sed ' n;d ' test.text

650) this.width=650; "title=" 15.png "src=" https://s4.51cto.com/wyfs02/M01/A5/42/ Wkiol1m6nkbiddn5aaapkwqawlq256.png-wh_500x0-wm_3-wmp_4-s_41822645.png "alt=" Wkiol1m6nkbiddn5aaapkwqawlq256.png-wh_50 "/>

9. reverse displays each line in the file:

650) this.width=650; "title=" 16.png "src=" https://s4.51cto.com/wyfs02/M02/A5/42/ Wkiol1m6nldqnxpyaaaejn5p4bm886.png-wh_500x0-wm_3-wmp_4-s_4235371985.png "alt=" Wkiol1m6nldqnxpyaaaejn5p4bm886.png-wh_50 "/>


Linux self-study note--sed command

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.