Use of sed command tool

Keywords: Programming vim

Understanding the basic options of sed tools

The common options for sed commands are as follows:
- n (Shield default output, default sed will output all the contents of the read document)
- r (Let sed support extended regularity)
- i (sed modifies source files directly, default sed modifies files temporarily only through memory, source files have no effect)

Common instructions for sed commands are as follows:
sed instruction P (display), d (deletion), s (replacement) and other common operations

sed usage

Usage 1.
Pre-command | sed option instruction
Usage two,
sed option instruction file

p uses line numbers as addressors:

[root@server0 ~]#vim  text.txt
[root@server0 ~]# Sed-n'p'text.txt//-n masks the default output and displays all rows only once
[root@server0 ~]#Sed-n'1p'text.txt//Print only the first line on the screen
[root@server0 ~]#Sed-n'2,4p'text.txt//Print the second to fourth lines on the screen
[root@server0 ~]#Sed-n'2p; 4p'text.txt//Print the second and fourth lines on the screen
[root@server0 ~]#Sed-n'1~2p'text.txt//Print odd lines on the screen
[root@server0 ~]#Sed-n'1, +2p'text.txt//Print starts with the first line and adds two lines

p uses regularization as the addressor:

[root@server0 ~]#Sed-n'/root/p'/etc/passwd//Search for rows containing root
[root@server0 ~]#Sed-n'/^root/p'/etc/passwd//Search for rows starting with root
[root@server0 ~]#Sed-n'/root$/p'/etc/passwd//Search for rows ending with root
[root@server0 ~]#Sed-n'/ro{1,}/p'/etc/passwd// Search for strings that begin with RO and appear one or more lines

- r supports extended regularity

sed -n -r '/ro{1,}/p' /ect/passwd   //Use extended regular search for strings that begin with ro and appear once and more rows

= Display line number

[root@server0 ~]#Sed-n'='text.txt// / Displays line numbers for all lines
[root@server0 ~]#Sed-n'$='text.txt// / Displays the line number of the last line
[root@server0 ~]#Sed-n'/root/='text.txt// Displays line numbers with root strings

d as an addressor is similar to p

[root@server0 ~]#Sed'd'text.txt//Delete all lines in text
[root@server0 ~]#Sed'1d'text.txt//Delete the first line of text
[root@server0 ~]#Sed'1, 2d'text. TXT // / Delete the first to second lines of text
[root@server0 ~]#Sed'1, +2d'text.txt// / Delete text by adding two lines from the first line
[root@server0 ~]#Sed'1-2d'text.txt//Delete odd rows
[root@server0 ~]#Sed'/root/d'text.txt//Delete rows with root

- i Modify the file

[root@server0 ~]#Sed-i'p'text.txt// Save the results of the source text displayed on the screen into text
[root@server0 ~]#Save the file after deleting the first line
[root@server0 ~]#Sed-i'$d'text.txt// / Delete the last line and save the file

s replacement function

[root@server0 ~]#Sed's/2017/**/'test.txt// Replace the first 2017 for each line of text with **** using the s substitution instruction
[root@server0 ~]#Sed's/2017/**/g'test.txt// Replace all 2017 lines in the text with **** using the s substitution instruction
[root@server0 ~]#Sed's/2017/**/2'test.txt// Replace the second 2017 in each line of the text with **** using the s substitution instruction
[root@server0 ~]#Sed's/2017//2'test.txt// Replace the second 2017 with blank for each line of text that meets the requirements, equivalent to deleting 2017

Posted by Scottiebabes on Fri, 12 Apr 2019 21:24:33 -0700