Shell Script sed command

Source: Internet
Author: User

from:http://blog.csdn.net/engledb/article/details/19623087 Six, common examples

1. P command

The command p is used to display the contents of the pattern space. By default, sed prints the input lines on the screen, and option-n is used to cancel the default print operation. When the Select-N and command p appear simultaneously, sed prints the selected content.

Example:

[Plain] view plaincopy

    1. (1) Sed '/my/p ' datafile
    2. #默认情况下, sed prints all input lines on the standard output. If a row matches the pattern, the my,p command will print the line again.
    3. (2) sed-n '/my/p ' datafile
    4. #选项-N Cancels the default print for SED, and the p command prints the line of the matching mode my.

2, D command

Command d is used to delete input rows. Sed first copies the input line from the file into the pattern space, then executes the SED command on the line, and finally displays the contents of the pattern space on the screen. If command d is emitted, the input lines in the current mode space are deleted and not displayed.

Example:

[Plain] view plaincopy

    1. (1) Sed ' $d ' datafile
    2. #删除最后一行, the rest is displayed.
    3. (2) Sed '/my/d ' datafile
    4. #删除包含my的行, the rest is displayed.

3, S command

Example:

[Plain] view plaincopy

    1. (1) Sed ' s/^my/you/g ' datafile
    2. #命令末端的g表示在行内进行全局替换, that is, if a row appears with multiple my, all my my is replaced with you.
    3. (2) sed-n ' 1,20S/MY$/YOU/GP ' datafile
    4. #取消默认输出, Process 1 to 20 lines matching the line ending with my, replace all of my in-line I with you and print to the screen.
    5. (3) Sed ' s#my#your#g ' datafile
    6. #紧跟在s命令后的字符就是查找串和替换串之间的分隔符. Separating defaults is considered a forward slash, but can be changed. No matter what character (except newline, backslash), as soon as the S command is followed, a new string delimiter is formed.


4. E option

-E is an edit command that is used in cases where SED performs multiple editing tasks. All edit actions are applied to the line in the pattern buffer before the next line begins editing.

Example:

[Plain] view plaincopy

    1. Sed-e ' 1,10d '-e ' s/my/your/g ' datafile
    2. #选项-E is used for multiple edits. The first edit deletes the 第1-3 line. The second editor replaces all occurrences of my with your. Because these two edits are performed line by row (that is, both commands are executed on the current line in the pattern space), the order in which the commands are edited affects the result.

5. R command

The r command is a read command. SED uses this command to add the contents of a text file to a specific location in the current file.

For example:

[Plain] view plaincopy

    1. Sed '/my/r introduce.txt ' datafile
    2. #如果在文件datafile的某一行匹配到模式My, the contents of the file Introduce.txt are read after the line. If there is more than one row of my rows, the contents of the Introduce.txt file are read after the lines of my are present.

6, W command

Example:

[Plain] view plaincopy

    1. Sed-n '/hrwang/w me.txt ' datafile

7. A\ command

The A\ command is an append command, appended with the addition of new text to the current line in the file (that is, the line in the read-in mode buffer). The appended line of text is placed below the SED command. If more than one row is to be appended, each row must end with a backslash, except for the last line. The last line ends with a quotation mark and a file name.

Example:

[Plain] view plaincopy

    1. Sed '/^hrwang/a\
    2. >hrwang and Mjfan are husband\
    3. >and Wife ' DataFile
    4. #如果在datafile文件中发现匹配以hrwang开头的行, append Hrwang and Mjfan are husband and wife below the line

8. I\ command

The I\ command inserts a new text in front of the current line.

9. c\ command

SED uses this command to modify an existing text to a new text.

10, N command

SED uses this command to get the next line of input files and read them into the pattern buffer, and any sed command will be applied to the next line immediately following the matching line.

For example:

[Plain] view plaincopy

    1. Sed '/hrwang/{n;s/my/your/;} ' datafile

Note: If you need to use multiple commands, or if you need to nest addresses in an address range, you must enclose the command in curly braces, write only one command per line, or use semicolons to split multiple commands in the same row.

11, y command

This command is similar to the TR command in Unix/linux, where characters are converted from left to right in a one-to-one fashion. For example, y/abc/abc/will convert all lowercase a to a, lowercase B to B, and lowercase C to c.

For example:

[Plain] view plaincopy

    1. Sed ' 1,20y/hrwang12/hrwang^$/' datafile
    2. #将1到20行内, convert all lowercase hrwang to uppercase, convert 1 to ^, convert 2 to $.
    3. #正则表达式元字符对y命令不起作用. As with the S command delimiter, the slash can be replaced with other characters.

12. Q Command

The Q command will cause the SED program to exit without further processing.

[Plain] view plaincopy

    1. Sed '/hrwang/{s/hrwang/hrwang/;q;} ' datafile

13, h command and G command

To better illustrate these two commands, we first create the following text file:

[Plain] view plaincopy

    1. #cat datafile
    2. My name is Hrwang.
    3. Your name is Mjfan.
    4. Hrwang is Mjfan ' s husband.
    5. Mjfan is Hrwang ' s wife.

[Plain] view plaincopy

      1. Sed-e '/hrwang/h '-e ' $G ' datafile
      2. Sed-e '/hrwang/h '-e ' $G ' datafile
      3. #通过上面两条命令, you will find that H clears the contents of the original staging buffer, saving only the schema space that was saved in the last time the H was executed. The h command appends the rows of each match Hrwnag to the staging buffer.
      4. Sed-e '/hrwang/h '-e ' $g ' datafile
      5. Sed-e '/hrwang/h '-e ' $G ' datafile
      6. #通过上面两条命令, you'll find that G replaces the contents of the current row in the schema space with the content of the staging buffer, replacing the last row. The G command appends the contents of the staging buffer to the current line of the schema space. This is appended to the end.

Shell Script sed command

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.