Use of SED command in Linux

Time:2020-11-25

Sed is the abbreviation of stream editor, that is, stream editor. It processes one line of content at a time. When processing, it stores the currently processed line in the temporary buffer, which is called “pattern space”. Then it uses the SED command to process the contents of the buffer. After processing, it sends the contents of the buffer to the screen. The next line is then processed, which is repeated until the end of the file. The contents of the file do not change unless you use redirection to store output.

Using grammar
The rules for using the SED command are as follows:

Copy code

The code is as follows:

sed [option] ‘command’ input_file

Option is optional. The common options are as follows:

-N use silent mode (I don’t know why it’s not – s). In normal sed usage, all content from stdin is listed on the screen. However, if the – n parameter is added, only the row (or action) that has been specially processed by sed will be listed;
-E. edit sed action directly in instruction line mode;
-F writes sed actions in a file directly, and – f filename can execute sed command in filename;
-R let sed command support extended regular expression (the default is base regular expression);
-I modify the contents of the read file directly, rather than output from the screen.

The following commands are commonly used:

A: append the line append, and the string s will be added after the string s (multi line strings can be separated by the string);

C: replace / replace line change, C \ \ is followed by the string s (multiple line strings can be separated with the string s), then the currently selected line will be replaced with the string s;
If I \ \ insert a line, and I \ \ is followed by the string s (multiple line strings can be separated by a string), the string s will be inserted in front of the currently selected line;
d: Delete line delete, which will delete the currently selected line;
p: Print print, which will print the currently selected line to the screen;
s: Replace the string subs. Usually, the usage of the s command is as follows: 1,2s / old / new / g to replace the old string with the new string
Command example
Suppose you have a local file test.txt The contents of the document are as follows:

Copy code

The code is as follows:

[[email protected]~]$ cat test.txt

this is first line
this is second line
this is third line
this is fourth line
this fifth line
happy everyday
end
This section uses this file to demonstrate in detail the usage of each command.

A command (append line)
Example 1

Copy code

The code is as follows:

[[email protected]~]$ sed ‘1a \add one’ test.txt

this is first line
add one
this is second line
this is third line
this is fourth line
this is fifth line
happy everyday
end
In this example, in the command part, 1 represents the first line, the same second line is written as 2, the first line to the third line is written as 1,3, and the last line is represented by $, for example, 2, $represents all the lines between the second line and the last line (including the second line and the last line).

In this example, we can see the effect of adding “one” after the first line.

Example 2

Copy code

The code is as follows:

[[email protected]~]$ sed ‘1,$a \add one’ test.txt

this is first line
add one
this is second line
add one
this is third line
add one
this is fourth line
add one
this is fifth line
add one
happy everyday
add one
end
add one
This example shows that the string “add one” is added after all the lines in the first and last lines, and the effect can be seen from the output.

Example 3

Copy code

The code is as follows:

[[email protected]~]$ sed ‘/first/a \add one’ test.txt

this is first line
add one
this is second line
this is third line
this is fourth line
this is fifth line
happy everyday
end
In this example, the string “add one” is added after the line containing the “first” string. From the output, we can see that the first line contains first, so “add one” is added after the first line

Example 4

Copy code

The code is as follows:

[[email protected]~]$ sed ‘/^ha.*day$/a \add one’ test.txt

this is first line
this is second line
this is third line
this is fourth line
this is fifth line
happy everyday
add one
end
In this example, regular expression is used to match the line, ^ ha. * day $means that the line beginning with HA and ending with day can be matched to “happy every day” of the file, so the “add one” string is added after the line.

I command (insert line)
The I command uses the same method as the a command, except that a string is inserted in front of the matching line. Therefore, the a of the example of a command above can be directly replaced with I. there is no wordiness here.

C command (replace line)
Example 5

Copy code

The code is as follows:

[[email protected]~]$ sed ‘$c \add one’ test.txt

this is first line
this is second line
this is third line
this is fourth line
this is     fifth line
happy everyday
add one
This example shows replacing the last line with the string “add one”. You can see the effect from the output.

Example 6

Copy code

The code is as follows:

[[email protected]~]$ sed ‘4,$c \add one’ test.txt

this is first line
this is second line
this is third line
add one
This example replaces the contents of the fourth line to the last line with the string “add one”.

Example 7

Copy code

The code is as follows:

[[email protected]~]$ sed ‘/^ha.*day$/c \replace line’ test.txt

this is first line
this is second line
this is third line
this is fourth line
this is fifth line
replace line
end
In this example, replace the line beginning with HA and ending with day with “replace line”.

D command (delete line)
Example 8

Copy code

The code is as follows:

[[email protected]~]$ sed ‘/^ha.*day$/d’ test.txt

this is first line
this is second line
this is third line
this is fourth line
this is fifth line
end
In this example, delete lines that start with HA and end with day.

Example 9

Copy code

The code is as follows:

[[email protected]~]$ sed ‘4,$d’ test.txt

this is first line
this is second line
this is third line
In this example, delete the contents from the fourth line to the last line.

P command (print line)
Example 10

Copy code

The code is as follows:

[[email protected]~]$ sed -n ‘4,$p’ test.txt

this is fourth line
this is fifth line
happy everyday
end
This example prints the fourth line to the last line on the screen. The P command is usually used with the – N option.

Example 11

Copy code

The code is as follows:

[[email protected]~]$ sed -n ‘/^ha.*day$/p’ test.txt

happy everyday
This example prints lines that start with HA and end with day.

S command (replace string)
In practical application, s-command is the most commonly used.

Example 12

Copy code

The code is as follows:

[[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
In this example, all lines in the file are replaced with text. The last g means global, that is, global replacement. If G is not added, only the first line of the line will be replaced.

Example 13

Copy code

The code is as follows:

[[email protected]~]$ sed ‘/^ha.*day$/s/happy/very happy/g’ test.txt

this is first line
this is second line
this is third line
this is fourth line
this is fifth line
very happy everyday
end
This example first matches the line beginning with HA and ending with day. In this case, the line matched is “happy every day”, and then replace the happy in the row with very happy.

Example 14

Copy code

The code is as follows:

[[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 complicated. Let’s break it down. First of all, the mode of s command is s / old / new / g, so the old part of this example is \ (. * \) line $. The content wrapped by \ (\) in sed command represents the N part of the regular expression, and the ordinal number is calculated from 1. In this example, there is only one \ (\) so \ (. * \) represents the first part of the regular expression, which matches any string. Therefore, (. * \) line $matches any string ending with line What can I do. Then replace the matched line with the first part of the regular expression (in this case, it is equivalent to deleting the line part). The first part of the regular expression is represented by 1, the second part is the second part, and the third part is the third part. For example, the following example:

Copy code

The code is as follows:

[[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 parts on both sides of is in a regular expression can be represented by either 1 or 2. The purpose of this example is to delete is in the middle part.