The SED command using Linux is detailed

Source: Internet
Author: User

SED is the short name of the stream editor, which is the stream editors. It handles a line of content at a time, and when processed, stores the currently processed rows 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.

Using syntax
The rules for using SED commands are:

sed [option] ' command ' input_file

option is optional and the usual options are as follows:

-N Use Quiet silent mode (wondering why not-s). In the usage of general sed, all content from stdin is generally listed on the screen. However, if the-n parameter is added, only the line (or action) that is specially processed by SED is listed.
-E performs the action editing of SED directly in the instruction-list mode;
-F writes the SED action directly in a file, and-f filename executes the sed command within the filename;
-R enables the SED command to support extended regular expressions (default is the underlying regular expression);
-I directly modifies the contents of the read file instead of the screen output.

The following commands are commonly used:

A \: Append line append, followed by string s after a \ (multiline string can be separated by \ n), then the string s is appended to the currently selected row;

C \: replace/replace line change,c \ followed by string s (a multiline string can be separated by \ n), the currently selected row is replaced with a string s;
i \: Insert line insert,i \ followed by string s (multiline string can be separated by \ n), the string s is inserted before the currently selected line;
D: Delete Row Delete, which deletes the currently selected row;
P: Prints print, which prints the currently selected line to the screen;
S: replace string subs, usually the S command is used like this: 1,2s/old/new/g, replacing the old string with the new string
command example
Suppose there is a local file Test.txt, the file contents are as follows:

[Email protected]~]$ cat Test.txt

This was first line
This was second line
This was third line
This was fourth line
This fifth line
Happy Everyday
End
This section will use this file to demonstrate in detail the usage of each command.

A command (append line)
Example One

[Email protected]~]$ sed ' 1a \add one ' test.txt

This was first line
Add one
This was second line
This was third line
This was fourth line
This was fifth line
Happy Everyday
End
The 1 in this command section represents the first row, the same second line is written in 2, the first line to the third line is written in 1, 3, and the last line is represented by $, for example, 2,$ represents the second row to the last row in the middle of all rows (containing the second row and the last row).

The purpose of this example is to add the string "add one" after the first line, and to see the concrete effect from the output.

Example Two

[[Email protected]~]$ sed ' 1, $a \add one ' test.txt

This was first line
Add one
This was second line
Add one
This was third line
Add one
This was fourth line
Add one
This was fifth line
Add one
Happy Everyday
Add one
End
Add one
This example indicates that the "add one" string is appended to all rows in the first and last row, and the effect is visible from the output.

Example Three

[Email protected]~]$ sed '/first/a \add one ' test.txt

This was first line
Add one
This was second line
This was third line
This was fourth line
This was fifth line
Happy Everyday
End
This example adds the string "add one" after the line that contains the "first" string, and from the output you can see that the top row contains the primary, so the first line is added after the "add one"

Example Four

[Email protected]~]$ sed '/^ha.*day$/a \add one ' test.txt

This was first line
This was second line
This was third line
This was fourth line
This was fifth line
Happy Everyday
Add one
End
This example uses a regular expression to match a row, ^ha.*day$ represents a line that begins with Ha, and ends with day, so you can match the "happy everyday" of the file so that the "add one" string is added after the line.

The I command (insert row) I command uses the same method as the a command, except that the string is inserted in front of the matching line, so the example of the a command above can be replaced by an I, which is not verbose.

C command (replace line)
Example Five

[Email protected]~]$ sed ' $c \add one ' test.txt

This was first line
This was second line
This was third line
This was fourth line
This was fifth line
Happy Everyday
Add one
This example replaces the last line with the string "Add one", which shows the effect from the output.

Example VI

[[Email protected]~]$ sed ' 4, $c \add one ' test.txt

This was first line
This was second line
This was third line
Add one
This example replaces the contents of line fourth to the last line with the string "Add one".

Example VII

[Email protected]~]$ sed '/^ha.*day$/c \replace line ' test.txt

This was first line
This was second line
This was third line
This was fourth line
This was fifth line
Replace Line
End
This example starts with HA and replaces the line with the end of day with "replace lines".

d command (delete row)
Example VIII

[Email protected]~]$ sed '/^ha.*day$/d ' test.txt

This was first line
This was second line
This was third line
This was fourth line
This was fifth line
End
This example deletes a line that begins with Ha and ends with day.

Example Nine

[[Email protected]~]$ sed ' 4, $d ' Test.txt

This was first line
This was second line
This was third line
This example deletes the contents of line fourth to the last row.

P Command (print line)
Example Ten

[Email protected]~]$ sed-n ' 4, $p ' Test.txt

This was fourth line
This was 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 generally used with the-n option.

Example Xi.

[Email protected]~]$ sed-n '/^ha.*day$/p ' test.txt

Happy Everyday
This example prints a line that starts with HA and ends with day.

The s command (replacement string) is actually used most often in the S command.

Example 12

[Email protected]~]$ sed ' s/line/text/g ' test.txt

This is first text
This is second text
This is third text
This is fourth text
This is fifth text
Happy Everyday
End
This example replaces all lines in the file with text, and the final G is the meaning of global, which is the replacement of the whole, and if no g is added, only the first line of our line will be replaced.

Example 13

[Email protected]~]$ sed '/^ha.*day$/s/happy/very happy/g ' test.txt

This was first line
This was second line
This was third line
This was fourth line
This was fifth line
Very happy Everyday
End
This example first matches a line starting with Ha, ending with day, in this case the line that matches is "happy everyday", and then replaces the happy in that row with very happy.

Example 14

[[Email protected]~]$ sed ' s/\ (. *\) line$/\1/g ' Test.txt

This is first
This is second
This is third
This is fourth
This is fifth
Happy Everyday
End
This example is a little bit more complicated, first break it down. The mode of the S command is s/old/new/g, so the old part of this example, the \ (. *\) line$,sed command, uses the contents of the \ (\) package to represent the nth part of the regular expression, and the sequence number is calculated from 1, in this case there is only one \ (\) so \ (. *\) Represents the first part of a regular expression, which partially matches any string, so \ (. *\) line$ matches any line that ends with lines. The matched rows are then replaced with the first part of the regular expression (in this case the line section is deleted), the first part of the match is represented by \1, the same \2 represents the second part, and the \3 represents the third part, which can be referred to in turn. For example, the following:

[[Email protected]~]$ sed ' s/\ (. *\) is\ (. *\) line/\1\2/g ' Test.txt

This first
This second
This third
This fourth
This fifth
Happy Everyday
End
The part of the is on both sides of the regular expression can be expressed in \1 and \2, which is actually to delete the middle part of IS.

The SED command using Linux is detailed

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.