Use of Linux sed Command

  • 2021-08-21 22:07:28
  • OfStack

1. Feature profile

sed (Stream EDitor) is a stream file editor that processes one line at a time. When processing, store the currently processed line in a temporary buffer called "pattern space" (Pattern Space), then use the sed command to process the contents of the buffer. After processing, send the contents of the buffer to the screen, and then process the next line until the end of the file. The file contents have not changed unless the-i option is used. sed is mainly used to edit one or more files, simplify the repeated operation of files or write conversion programs.

The function of sed is similar to that of awk, but the difference is that sed is simple and the function of column processing is worse than 1, while awk is complex and powerful.

2. Command format


sed [OPTION]... {script-only-if-no-other-script} [input-file]...

Among them, OPTION is a command option, script-only-if-no-other-script is a processing action, which can be specified by-e, and input-file is an input file, which can be specified by multiple.

3. Description of options


 Options: 
-n,--quiet,--silent Use quiet mode. sed Adj. 1 In general usage, all from STDIN Data of 1 Will be printed to the terminal, if you add -n After, only after passing sed The specially handled one 1 Rows will be listed. 
-e <script>,--expression=<script> : Specify sed Action, which can be composed of multiple -e Specify multiple actions. 
-f <script-file>,--file=<script-file> : Directly set the sed The action is written in 1 Within 10 files, -f filename You can run the filename  Inside sed Action; 
-r,--regexp-extended : sed Support extended regular expressions ( The default is the underlying regular expression ) . 
-i  Modify the contents of the read file directly, instead of outputting it to the terminal. 
--help Displays Help. 
--version Display version. 

 Action description: [n1[,n2]]function
n1, n2  : It doesn't necessarily exist, 1 For example, if my action is required in the  10  To  20  If you proceed between lines, write " 10,20 Action behavior ". 

function : 
a  : New,  a  You can connect strings after, and these strings will be listed in the new 1 Row appearance ( Current lower 1 Row ) ~ 
c  : Replace,  c  Can be followed by strings, which can replace the  n1,n2  Between the lines! 
d  Delete, because it is deleted, so  d  Usually nothing is followed; 
i  : Insert,  i  You can connect strings after, and these strings will be listed in the new 1 Row appearance ( Present 1 Row ) ; 
p  Print, that is, print out a selected data. Usually  p  Will be associated with parameters  sed -n 1 Start running ~ 
s  : Replace, usually this s The actions of can be matched with formal notation! For example  1,20s/old/new/g . 

4. Typical examples

4.1 Delete Row Action

(1) List the contents of/etc/passwd and print the line number, while deleting lines 2-5.


[b3335@MIC ~]$ nl -n ln /etc/passwd | sed '2,5d'
 root:x:0:0:root:/root:/bin/bash
 sync:x:5:0:sync:/sbin:/bin/sync
 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
.....( Omitted later ).....

Note: sed-e should have been issued. When there is only one action, it is fine without-e, but when there is more than one action, the-e option must be used to specify the action. Also note that the actions followed by sed must be enclosed in two single quotation marks.

(2) Just delete line 2.


nl /etc/passwd | sed '2d'

(3) To delete lines 3 through the last 1


nl /etc/passwd | sed '3,$d' 

4.2 Add Row Action

(1) Add "I like drinking tea" after line 2.


[b3335@MIC ~]$ nl -n ln /etc/passwd | sed '2a I like drinking tea'
 root:x:0:0:root:/root:/bin/bash
 bin:x:1:1:bin:/bin:/sbin/nologin
I like drinking tea
 daemon:x:2:2:daemon:/sbin:/sbin/nologin
.....( Omitted later ).....

(2) If you want to add it before line 2.


nl /etc/passwd | sed '2i drink tea'
// Or 
nl /etc/passwd | sed '1a drink tea'

(3) Add two lines after line 2, "I like drinking tea" and "I like drinking beer".


[b3335@MIC ~]$ nl -n ln /etc/passwd | sed '2a I like drinking tea\nI like drinking beer'
  root:x:0:0:root:/root:/bin/bash
  bin:x:1:1:bin:/bin:/sbin/nologin
I like drinking tea
I like drinking beer
  daemon:x:2:2:daemon:/sbin:/sbin/nologin
 … (omitted) … 

Or use a backslash\ to separate every 1 line, you can type a command on multiple lines at the command line, as follows:


[b3335@MIC ~]$ nl -n ln /etc/passwd | sed '2a I like drinking tea\
> I like drinking beer'

4.3 Replace Row Actions

(1) Replace lines 2-5 with "No 2-5 number".


[b3335@MIC ~]$ nl -nln /etc/passwd | sed '2,5c No 2-5 number'
  root:x:0:0:root:/root:/bin/bash
No 2-5 number
  sync:x:5:0:sync:/sbin:/bin/sync
.....( Omitted later ).....

4.4 Select Line Print

(1) Only lines 5-7 in the/etc/passwd file are listed.


 Options: 
-n,--quiet,--silent Use quiet mode. sed Adj. 1 In general usage, all from STDIN Data of 1 Will be printed to the terminal, if you add -n After, only after passing sed The specially handled one 1 Rows will be listed. 
-e <script>,--expression=<script> : Specify sed Action, which can be composed of multiple -e Specify multiple actions. 
-f <script-file>,--file=<script-file> : Directly set the sed The action is written in 1 Within 10 files, -f filename You can run the filename  Inside sed Action; 
-r,--regexp-extended : sed Support extended regular expressions ( The default is the underlying regular expression ) . 
-i  Modify the contents of the read file directly, instead of outputting it to the terminal. 
--help Displays Help. 
--version Display version. 

 Action description: [n1[,n2]]function
n1, n2  : It doesn't necessarily exist, 1 For example, if my action is required in the  10  To  20  If you proceed between lines, write " 10,20 Action behavior ". 

function : 
a  : New,  a  You can connect strings after, and these strings will be listed in the new 1 Row appearance ( Current lower 1 Row ) ~ 
c  : Replace,  c  Can be followed by strings, which can replace the  n1,n2  Between the lines! 
d  Delete, because it is deleted, so  d  Usually nothing is followed; 
i  : Insert,  i  You can connect strings after, and these strings will be listed in the new 1 Row appearance ( Present 1 Row ) ; 
p  Print, that is, print out a selected data. Usually  p  Will be associated with parameters  sed -n 1 Start running ~ 
s  : Replace, usually this s The actions of can be matched with formal notation! For example  1,20s/old/new/g . 
0

4.5 Data search and related operations

(1) Data search and display

Search for/etc/passwd rows with the root keyword and output.


 Options: 
-n,--quiet,--silent Use quiet mode. sed Adj. 1 In general usage, all from STDIN Data of 1 Will be printed to the terminal, if you add -n After, only after passing sed The specially handled one 1 Rows will be listed. 
-e <script>,--expression=<script> : Specify sed Action, which can be composed of multiple -e Specify multiple actions. 
-f <script-file>,--file=<script-file> : Directly set the sed The action is written in 1 Within 10 files, -f filename You can run the filename  Inside sed Action; 
-r,--regexp-extended : sed Support extended regular expressions ( The default is the underlying regular expression ) . 
-i  Modify the contents of the read file directly, instead of outputting it to the terminal. 
--help Displays Help. 
--version Display version. 

 Action description: [n1[,n2]]function
n1, n2  : It doesn't necessarily exist, 1 For example, if my action is required in the  10  To  20  If you proceed between lines, write " 10,20 Action behavior ". 

function : 
a  : New,  a  You can connect strings after, and these strings will be listed in the new 1 Row appearance ( Current lower 1 Row ) ~ 
c  : Replace,  c  Can be followed by strings, which can replace the  n1,n2  Between the lines! 
d  Delete, because it is deleted, so  d  Usually nothing is followed; 
i  : Insert,  i  You can connect strings after, and these strings will be listed in the new 1 Row appearance ( Present 1 Row ) ; 
p  Print, that is, print out a selected data. Usually  p  Will be associated with parameters  sed -n 1 Start running ~ 
s  : Replace, usually this s The actions of can be matched with formal notation! For example  1,20s/old/new/g . 
1

(2) Data search and deletion
Delete/etc/passwd all rows containing root, and output the other rows.


 Options: 
-n,--quiet,--silent Use quiet mode. sed Adj. 1 In general usage, all from STDIN Data of 1 Will be printed to the terminal, if you add -n After, only after passing sed The specially handled one 1 Rows will be listed. 
-e <script>,--expression=<script> : Specify sed Action, which can be composed of multiple -e Specify multiple actions. 
-f <script-file>,--file=<script-file> : Directly set the sed The action is written in 1 Within 10 files, -f filename You can run the filename  Inside sed Action; 
-r,--regexp-extended : sed Support extended regular expressions ( The default is the underlying regular expression ) . 
-i  Modify the contents of the read file directly, instead of outputting it to the terminal. 
--help Displays Help. 
--version Display version. 

 Action description: [n1[,n2]]function
n1, n2  : It doesn't necessarily exist, 1 For example, if my action is required in the  10  To  20  If you proceed between lines, write " 10,20 Action behavior ". 

function : 
a  : New,  a  You can connect strings after, and these strings will be listed in the new 1 Row appearance ( Current lower 1 Row ) ~ 
c  : Replace,  c  Can be followed by strings, which can replace the  n1,n2  Between the lines! 
d  Delete, because it is deleted, so  d  Usually nothing is followed; 
i  : Insert,  i  You can connect strings after, and these strings will be listed in the new 1 Row appearance ( Present 1 Row ) ; 
p  Print, that is, print out a selected data. Usually  p  Will be associated with parameters  sed -n 1 Start running ~ 
s  : Replace, usually this s The actions of can be matched with formal notation! For example  1,20s/old/new/g . 
2

If you want to delete the matching string, use the following command:


[b3335@MIC ~]$ nl /etc/passwd | sed 's/root//g'

(3) Data search and replacement
In addition to the whole row processing mode, sed can also search and replace partial data with row units. Basically, the search and replacement of sed is quite similar to that of vi! He's kind of like this:


 Options: 
-n,--quiet,--silent Use quiet mode. sed Adj. 1 In general usage, all from STDIN Data of 1 Will be printed to the terminal, if you add -n After, only after passing sed The specially handled one 1 Rows will be listed. 
-e <script>,--expression=<script> : Specify sed Action, which can be composed of multiple -e Specify multiple actions. 
-f <script-file>,--file=<script-file> : Directly set the sed The action is written in 1 Within 10 files, -f filename You can run the filename  Inside sed Action; 
-r,--regexp-extended : sed Support extended regular expressions ( The default is the underlying regular expression ) . 
-i  Modify the contents of the read file directly, instead of outputting it to the terminal. 
--help Displays Help. 
--version Display version. 

 Action description: [n1[,n2]]function
n1, n2  : It doesn't necessarily exist, 1 For example, if my action is required in the  10  To  20  If you proceed between lines, write " 10,20 Action behavior ". 

function : 
a  : New,  a  You can connect strings after, and these strings will be listed in the new 1 Row appearance ( Current lower 1 Row ) ~ 
c  : Replace,  c  Can be followed by strings, which can replace the  n1,n2  Between the lines! 
d  Delete, because it is deleted, so  d  Usually nothing is followed; 
i  : Insert,  i  You can connect strings after, and these strings will be listed in the new 1 Row appearance ( Present 1 Row ) ; 
p  Print, that is, print out a selected data. Usually  p  Will be associated with parameters  sed -n 1 Start running ~ 
s  : Replace, usually this s The actions of can be matched with formal notation! For example  1,20s/old/new/g . 
4

(4) Search for data and execute commands
Search the/etc/passwd, find the line corresponding to root, execute the following set of commands in curly braces, each separated by a semicolon, replace bash with blueshell, and output this line:


 Options: 
-n,--quiet,--silent Use quiet mode. sed Adj. 1 In general usage, all from STDIN Data of 1 Will be printed to the terminal, if you add -n After, only after passing sed The specially handled one 1 Rows will be listed. 
-e <script>,--expression=<script> : Specify sed Action, which can be composed of multiple -e Specify multiple actions. 
-f <script-file>,--file=<script-file> : Directly set the sed The action is written in 1 Within 10 files, -f filename You can run the filename  Inside sed Action; 
-r,--regexp-extended : sed Support extended regular expressions ( The default is the underlying regular expression ) . 
-i  Modify the contents of the read file directly, instead of outputting it to the terminal. 
--help Displays Help. 
--version Display version. 

 Action description: [n1[,n2]]function
n1, n2  : It doesn't necessarily exist, 1 For example, if my action is required in the  10  To  20  If you proceed between lines, write " 10,20 Action behavior ". 

function : 
a  : New,  a  You can connect strings after, and these strings will be listed in the new 1 Row appearance ( Current lower 1 Row ) ~ 
c  : Replace,  c  Can be followed by strings, which can replace the  n1,n2  Between the lines! 
d  Delete, because it is deleted, so  d  Usually nothing is followed; 
i  : Insert,  i  You can connect strings after, and these strings will be listed in the new 1 Row appearance ( Present 1 Row ) ; 
p  Print, that is, print out a selected data. Usually  p  Will be associated with parameters  sed -n 1 Start running ~ 
s  : Replace, usually this s The actions of can be matched with formal notation! For example  1,20s/old/new/g . 
5

Exit if only the first bash keyword replacing the/etc/passwd is blueshell


 Options: 
-n,--quiet,--silent Use quiet mode. sed Adj. 1 In general usage, all from STDIN Data of 1 Will be printed to the terminal, if you add -n After, only after passing sed The specially handled one 1 Rows will be listed. 
-e <script>,--expression=<script> : Specify sed Action, which can be composed of multiple -e Specify multiple actions. 
-f <script-file>,--file=<script-file> : Directly set the sed The action is written in 1 Within 10 files, -f filename You can run the filename  Inside sed Action; 
-r,--regexp-extended : sed Support extended regular expressions ( The default is the underlying regular expression ) . 
-i  Modify the contents of the read file directly, instead of outputting it to the terminal. 
--help Displays Help. 
--version Display version. 

 Action description: [n1[,n2]]function
n1, n2  : It doesn't necessarily exist, 1 For example, if my action is required in the  10  To  20  If you proceed between lines, write " 10,20 Action behavior ". 

function : 
a  : New,  a  You can connect strings after, and these strings will be listed in the new 1 Row appearance ( Current lower 1 Row ) ~ 
c  : Replace,  c  Can be followed by strings, which can replace the  n1,n2  Between the lines! 
d  Delete, because it is deleted, so  d  Usually nothing is followed; 
i  : Insert,  i  You can connect strings after, and these strings will be listed in the new 1 Row appearance ( Present 1 Row ) ; 
p  Print, that is, print out a selected data. Usually  p  Will be associated with parameters  sed -n 1 Start running ~ 
s  : Replace, usually this s The actions of can be matched with formal notation! For example  1,20s/old/new/g . 
6

4.6 Multi-point editing
1 sed command to delete the data from line 3 to the end of/etc/passwd and replace bash with blueshell


 Options: 
-n,--quiet,--silent Use quiet mode. sed Adj. 1 In general usage, all from STDIN Data of 1 Will be printed to the terminal, if you add -n After, only after passing sed The specially handled one 1 Rows will be listed. 
-e <script>,--expression=<script> : Specify sed Action, which can be composed of multiple -e Specify multiple actions. 
-f <script-file>,--file=<script-file> : Directly set the sed The action is written in 1 Within 10 files, -f filename You can run the filename  Inside sed Action; 
-r,--regexp-extended : sed Support extended regular expressions ( The default is the underlying regular expression ) . 
-i  Modify the contents of the read file directly, instead of outputting it to the terminal. 
--help Displays Help. 
--version Display version. 

 Action description: [n1[,n2]]function
n1, n2  : It doesn't necessarily exist, 1 For example, if my action is required in the  10  To  20  If you proceed between lines, write " 10,20 Action behavior ". 

function : 
a  : New,  a  You can connect strings after, and these strings will be listed in the new 1 Row appearance ( Current lower 1 Row ) ~ 
c  : Replace,  c  Can be followed by strings, which can replace the  n1,n2  Between the lines! 
d  Delete, because it is deleted, so  d  Usually nothing is followed; 
i  : Insert,  i  You can connect strings after, and these strings will be listed in the new 1 Row appearance ( Present 1 Row ) ; 
p  Print, that is, print out a selected data. Usually  p  Will be associated with parameters  sed -n 1 Start running ~ 
s  : Replace, usually this s The actions of can be matched with formal notation! For example  1,20s/old/new/g . 
7

-e means multi-point editing, the first edit command deletes the data from line 3 to the end of/etc/passwd, and the second command searches for bash and replaces it with blueshell.

4.7 Modify files directly

sed can modify the contents of files directly without using pipe commands or data flow redirection! However, since this action will be directly modified to the original file, so please don't take the system configuration to test casually, and be careful when using it. Let's use the downloaded regular_express. txt file to test it!

Use sed to replace every line in regular_express. txt with.


[root@www ~]# sed -i 's/\.$/!/g' regular_express.txt

Add "# This is a test" directly in the last line of regular_express. txt using sed.


 Options: 
-n,--quiet,--silent Use quiet mode. sed Adj. 1 In general usage, all from STDIN Data of 1 Will be printed to the terminal, if you add -n After, only after passing sed The specially handled one 1 Rows will be listed. 
-e <script>,--expression=<script> : Specify sed Action, which can be composed of multiple -e Specify multiple actions. 
-f <script-file>,--file=<script-file> : Directly set the sed The action is written in 1 Within 10 files, -f filename You can run the filename  Inside sed Action; 
-r,--regexp-extended : sed Support extended regular expressions ( The default is the underlying regular expression ) . 
-i  Modify the contents of the read file directly, instead of outputting it to the terminal. 
--help Displays Help. 
--version Display version. 

 Action description: [n1[,n2]]function
n1, n2  : It doesn't necessarily exist, 1 For example, if my action is required in the  10  To  20  If you proceed between lines, write " 10,20 Action behavior ". 

function : 
a  : New,  a  You can connect strings after, and these strings will be listed in the new 1 Row appearance ( Current lower 1 Row ) ~ 
c  : Replace,  c  Can be followed by strings, which can replace the  n1,n2  Between the lines! 
d  Delete, because it is deleted, so  d  Usually nothing is followed; 
i  : Insert,  i  You can connect strings after, and these strings will be listed in the new 1 Row appearance ( Present 1 Row ) ; 
p  Print, that is, print out a selected data. Usually  p  Will be associated with parameters  sed -n 1 Start running ~ 
s  : Replace, usually this s The actions of can be matched with formal notation! For example  1,20s/old/new/g . 
9

Since $represents the last line and the a action is added, the file adds "# This is a test" at the end.

sed -i Option can directly modify the contents of the file, which is very helpful! For example, if you have a 1 million-line file and you want to add some text to line 100, using vim at this point can be insane! Because the file is too big! Then what should I do? Just use sed! You don't even need to use vim to modify it with sed's direct modification/replacement feature!

The above is the use of Linux sed command details, more about Linux sed command information please pay attention to other related articles on this site!


Related articles: