Use of the SED command in the shell

Source: Internet
Author: User

The SED stream editor is also a command in Linux that is often used in the shell to modify the contents of a file in a non-interactive way. The SED handles the text in rows, that is, one row at a time.
The basic format of the SED command is:
sed option ' edit directive ' file path
You can also use the pipe (|) Transfer parameters to SED processing Pre-command | sed option ' edit Command '

The common parameters of sed are:
-N Masks the default output, and if the-n option is not added, all output text satisfies the criteria of the row and then repeats the output
-R If you use extended regular, you need to add the-r option, the default does not support extended regular, only the standard regular
-I directly modifies the source file, without the-I will only be on the screen temporary output will not modify the source file, the General Test command is correct to use the-I option in the script
Editing instructions for SED:

P output
Command Complete Example Description
P Sed-n ' P ' 1.txt Output all lines of 1.txt
2p Sed-n ' 2p ' 1.txt Second line of output 1.txt
2,5p Sed-n ' 2,5p ' 1.txt The second line of output 1.txt to line fifth
2,+5p Sed-n ' 2,+5p ' 1.txt Output 1.txt second row and 5 rows after the second row
1~2p Sed-n ' 1~2p ' 1.txt Outputs the first line, one row per two lines, which is the output odd line
2~2p Sed-n ' 1~2p ' 1.txt Outputs the second line, one row per two lines, that is, the output even number of rows
/Regular/P Sed-n '/^[0-9]/p ' 1.txt Output 1.txt lines starting with a number
$= Sed-n ' $= ' 1.txt Number of rows for output 1.txt
d Delete
Command Complete Example Description
2d Sed ' 2d ' 1.txt Delete the second line of 1.txt
2,5d Sed ' 2,5d ' 1.txt Delete the second line of 1.txt to line fifth
2,+5d Sed ' 2,+5d ' 1.txt Delete the second row of 1.txt and the next 5 lines after the second row
$d Sed ' $d ' 1.txt Delete the last line of 1.txt
/Regular/ Sed '/^[0-9]/d ' 1.txt Delete a line that starts with a number
Sed '/^[0-9]/!d ' 1.txt Delete rows that do not start with a number
^$ Sed ' ^$ ' 1.txt Delete empty lines of 1.txt
S replacement sed ' s/old/new/' 1.txt
command Full example description
s/old/new/ sed ' s/old/new/' 1.txt Delete the first old of each row in 1.txt Replace with new
s/old/new/2 Delete the second old of each row in 1.txt Replace with new
s/old/new/g sed ' s/ol D/new/g ' 1.txt remove every old in 1.txt for each row replaced by new
s/old// sed ' s/old//' 1.txt Delete the first old of each line in 1.txt is replaced with an empty, that is, delete the old
s/old/&s/ sed ' s/old/&s/' 1.txt Delete the first old of each line in 1.txt replaced by Olds & the string for the previous lookup
4,7s/^/#/ sed ' 4,7s/^/#/' 1.txt Remove 4-7 lines at the beginning of 1.txt with # that is, bulk add comments
4,7s/^ #an/an/ sed ' 4,7s/^ #an/an/' 1.txt Delete 4-7 lines in 1.txt with the line beginning with #an remove #

The substitution of s///can be replaced with three other identical symbols, such as s### s&&& s999 can be used to replace the path is often

I/A/C Insertion
Command Complete Example Description
Line number I Sed ' 2i123 ' 1.txt Insert 123 in front of the second line of 1.txt
Regular I Sed '/^[0-9]/i123 ' 1.txt Insert 123 in front of all lines in 1.txt
A Insert after row command format and I are the same
C Replace the row command format and I are the same
SED Advanced Applications
Command Complete Example Description
Line number R Sed-i ' 2r 2.txt ' 1.txt Insert the contents of 2.txt after the second line in 1.txt
Line number, line number r Sed-i ' 2,5r 2.txt ' 1.txt Insert the contents of 2.txt in the second row to the fifth row after each line in 1.txt
/Regular/R Sed-i '/^[0-9]/r 2.txt ' 1.txt Inserts 2.txt of content into each row of rows after the line begins with a number in 1.txt
W Sed ' 2w 3.txt ' 1.txt Export the contents of 3.txt to the second row in 1.txt
W Sed ' 2,5w 3.txt ' 1.txt Export the contents of 3.txt in the second row to the fifth row in 1.txt
/Regular/R Sed-i '/^[0-9]/w 2.txt ' 1.txt A line with a number beginning in 1.txt is exported as a 3.txt content

Use of the SED command in the shell

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.