Sed command details
Overview
Sed is short for stream editor, that is, stream editor. It processes a row of content at a time. During Processing, it stores the currently processed row in the temporary buffer, called the pattern space, and then uses the sed command to process the content in the buffer, after processing, the buffer content is sent to the screen. Next, process the next row, and repeat until the end of the file. The file content is not changed unless you use the redirection storage output.
Use syntax
The rules for using the sed command are as follows:
sed[option]'command' input_file
Option is optional. The following options are commonly used:
Common Commands include the following:
Command example
Assume that there is a file test.txt with the following content:
[qifuguang@winwill~]$ cattest.txt
thisis first line
thisis second line
thisis third line
thisis fourth line
this fifth line
happy everyday
end
This section describes how to use each command in detail.
A command (append line)
Example 1
[qifuguang@winwill~]$ sed'1a \add one'test.txt
thisis first line
add one
thisis second line
thisis third line
thisis fourth line
thisis fifth line
happy everyday
end
In the command section of this example, 1 indicates the first line, the same second line is written as 2, and the first line to the third line is written1,3
, Use$
Indicates the last line, such2,$
Indicates all rows from the second row to the last row (including the second row and the last row ).
The purpose of this example is to add the "add one" string after the first line, and you can see the specific effect from the output.
Example 2
[qifuguang@winwill~]$ sed'1,$a \add one'test.txt
thisis first line
add one
thisis second line
add one
thisis third line
add one
thisis fourth line
add one
thisis fifth line
add one
happy everyday
add one
end
add one
In this example, the "add one" string is added after all the rows in the first and last rows. The output shows the effect.
Example 3
[qifuguang@winwill~]$ sed'/first/a \add one'test.txt
thisis first line
add one
thisis second line
thisis third line
thisis fourth line
thisis fifth line
happy everyday
end
In this example, the string "add one" is added to the row containing the "first" string. The output shows that the first row contains the first, therefore, "add one" is added after the first line"
Example 4
[qifuguang@winwill~]$ sed'/^ha.*day$/a \add one'test.txt
thisis first line
thisis second line
thisis third line
thisis fourth line
thisis fifth line
happy everyday
add one
end
In this example, the regular expression is used to match rows,^ha.*day$
Indicates the row starting with ha and ending with day. The "happy everyday" of the file can be matched. Therefore, the "add one" string is added after the row.
I command (insert line)
The method of using the I command is the same as that of the command, except that a string is inserted before the matched line. Therefore, replace a in the example of the command with I, I won't be embarrassed here.
C command (replace line)
Example 5
[qifuguang@winwill~]$ sed'$c \add one'test.txt
thisis first line
thisis second line
thisis third line
thisis fourth line
thisis fifth line
happy everyday
add one
In this example, replace the last line with the string "add one". The output shows the effect.
Example 6
[qifuguang@winwill~]$ sed'4,$c \add one'test.txt
thisis first line
thisis second line
thisis third line
add one
In this example, replace the content from the fourth row to the last row with the string "add one ".
Example 7
[qifuguang@winwill~]$ sed'/^ha.*day$/c \replace line'test.txt
thisis first line
thisis second line
thisis third line
thisis fourth line
thisis fifth line
replace line
end
In this example, replace the rows starting with ha and ending with day with "replace line ".
D command (Delete line)
Example 8
[qifuguang@winwill~]$ sed'/^ha.*day$/d'test.txt
thisis first line
thisis second line
thisis third line
thisis fourth line
thisis fifth line
end
In this example, rows starting with "ha" and ending with "day" are deleted.
Example 9
[qifuguang@winwill~]$ sed'4,$d'test.txt
thisis first line
thisis second line
thisis third line
In this example, content from the fourth row to the last row is deleted.
P command (print line)
Example 10
[qifuguang@winwill~]$ sed-n '4,$p'test.txt
thisis fourth line
thisis fifth line
happy everyday
end
In this example, the content from the fourth line to the last line is printed on the screen. The p command is generally used together with the-n option.
Example 11
[qifuguang@winwill~]$ sed-n '/^ha.*day$/p'test.txt
happy everyday
In this example, the rows starting with ha and ending with day are printed.
S command (replace string)
In actual use, the s command is the most commonly used.
Example 12
[qifuguang@winwill~]$ sed's/line/text/g'test.txt
thisis first text
thisis second text
thisis third text
thisis fourth text
thisis fifth text
happy everyday
end
In this example, all lines in the file are replaced with text, and g indicates global, that is, global replacement. If g is not added, only the first line of the row is replaced.
Example 13
[qifuguang@winwill~]$ sed'/^ha.*day$/s/happy/very happy/g'test.txt
thisis first line
thisis second line
thisis third line
thisis fourth line
thisis fifth line
very happy everyday
end
In this example, match the rows starting with ha and ending with day. In this example, the matched rows are "happy everyday", and then replace "happy" in the row with "very happy.
Example 14
[qifuguang@winwill~]$ sed's/\(.*\)line$/\1/g'test.txt
thisis first
thisis second
thisis third
thisis fourth
thisis fifth
happy everyday
end
This example is a bit complicated. Let's break it down first. First, the s command mode iss/old/new/g
So the old part of this example is\(.*\)line$
, Sed command to use\(\)
The content of the package indicates the nth part of the regular expression. The sequence number starts from 1. In this example, there is only one\(\)
So\(.*\)
Indicates the first part of the regular expression. This part matches any string, so\(.*\)line$
Match any row ending with line. Then replace the matched rows with the first part of the regular expression (in this example, the line part is deleted ).\1
Indicates the first part of the match.\2
Indicates the second part,\3
Indicates the third part, which can be referenced in sequence. For example:
[qifuguang@winwill~]$ sed's/\(.*\)is\(.*\)line/\1\2/g'test.txt
this first
this second
this third
this fourth
this fifth
happy everyday
end
Regular Expressionis
The two parts can be used.\1
And\2
Indicates that the function of this example is to delete the is in the middle.
Introduce shell variable in sed command
Shell programming in Linux -- basic usage of sed command
Sed for Unix text processing tools
Sed advanced usage
Sed command details and examples
Linux Regular Expression sed details
Sed in Linux text processing tool
This article permanently updates the link address: