SED command detailed examples

Source: Internet
Author: User

Reprint please indicate the source, this article link: http://qifuguang.me/2015/09/21/sed command detailed/have the deletion

1
sed [option] ' command ' input_file

Where option is optional, commonly used options are as follows:- n Use Quiet (silent) mode (not understand why not-s). In general sed usage, all content from stdin is generally listed on the screen. However, if you add the-n parameter, only the line (or action) that is specially handled by SED will be listed;- e to edit the action of SED directly on the instruction column mode;- f directly writes the action of SED in a file,-f filename You can execute the SED command within filename;- r let sed command support extended regular expressions (default is the underlying regular expression);

-I directly modify the contents of the read file, not the screen output.

The following commands are commonly used:

a \: append is the append string, a \ followed by the string s (multiline string can be separated by \ n), then the currently selected row will be followed by the string s. c \: replace/replace string, c \ followed by string s ( Multiple-line strings can be separated by \ n, the currently selected row is replaced with a string s : delete is deleted, and the command deletes the currently selected row; i \: Insert Inserts a string, i \ follows string s (multiple lines can be \ n-delimited), the string s is printed before the currently selected line, which prints the currently selected line to the screen; s: Replace, usually the use of the S command is this: 1,2s/old/new/g, Replace old string with new string command example

Suppose there is a local file Test.txt, the contents of the file are as follows:

[qifuguang@winwill~]$ Cat Test.txt
This is the
This is second line
This is third line
This is fourth line
This is fifth line
Happy Everyday
End

This section uses this file to demonstrate the use of each command in detail. a command

1
2
3
4
5
6 7 8 9
[qifuguang@winwill~]$ sed ' 1a \add one ' test.txt This is-is-is-is-is-second-line-this
I s third line this are fourth line this is
fifth line
Happy everyday
End

In this example, 1 in the command section is the first line, the same second line is 2, the first line to the third line is 1, 3, and the last line is $, for example, 2,$ represents all the rows in the middle of the second line to the last line (including the second and last lines).
The effect of this example is to add the string "add one" after the first line and see the concrete effect from the output.

1
2
3
4
5
6 7 8 9 (15)
[qifuguang@winwill~]$ sed ' 1, $a \add One ' test.txt This is-is-a-is-is-is-is-this-
One is third line add one, this is fourth line add one, this is
fifth line add one
H Appy Everyday
Add one
end
add one

This example shows that you can see the effect from the output by adding the "add one" string after all the lines in the first and last lines.

1
2
3
4
5
6 7 8 9
[qifuguang@winwill~]$ sed '/first/a \add one ' test.txt
This is in This is third the line this is fourth the line this is
fifth line
Happy everyday
End

This example indicates that after the line containing the "first" string is appended with the string "Add one", you can see from the output that the top row contains a, so the first row adds "add one"

1
2
3
4
5
6 7 8 9
[qifuguang@winwill~]$ sed '/^ha.*day$/a \add One ' test.txt This is-is-is-is-is-second-the-is-
t Hird line This is fourth line this is
fifth line
Happy everyday
add
one end

This example uses a regular expression to match a row, and ^ha.*day$ represents a row that begins with Ha and ends with day, so it can be matched to the "happy everyday" of the file, so the "add one" string is added after the line.

1
2
3
4
5
6 7 8
[qifuguang@winwill~]$ sed ' $c \add One ' test.txt This is-is-is-is-second-is-
third-
line
     this is fourth line this are     fifth line
Happy everyday
Add one

This example means that the last line is replaced with the string "Add one", and the effect can be seen from the output.

1
2
3
4
5
[qifuguang@winwill~]$ sed ' 4, $c \add One ' test.txt This is-is-is-a-is-second-is-
Third line
     add One

This example replaces the contents of line fourth through the last line with the string "Add one."

1
2
3
4
5
6 7 8
[qifuguang@winwill~]$ sed '/^ha.*day$/c \replace line ' Test.txt The This is a-is-a-second-line this
Is third line This is fourth the line this is
fifth line
replace
line end

This example replaces the line at the end of day with the beginning of ha with "replace lines." d Command

1
2
3
4
5
6 7
[qifuguang@winwill~]$ sed '/^ha.*day$/d ' test.txt This is the IS-a-is-is-is-second-is-
Third line
     this is fourth line this is fifth line end

This example deletes a row that starts with HA and ends with day.

1
2
3
4
[qifuguang@winwill~]$ sed ' 4, $d ' Test.txt This is the IS-a-is-a-is-second-the-is-third-line

This example deletes the contents of line fourth to the last line. P Command

1
2
3
4
5
[qifuguang@winwill~]$ sed-n ' 4, $p ' Test.txt This is fourth the line this is
fifth line
Happy everyday
End

This example prints the contents of line fourth to the last line on the screen, and the P command is used with the-n option in general.

1
2
[qifuguang@winwill~]$ sed-n '/^ha.*day$/p ' test.txt
Happy Everyday

This example prints a row that starts with HA and ends with day. s Command

In the actual use of the S-command most commonly used.

1 2 3 4 5 6 7 8 
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.