Full analysis of SED command in Linux

Time:2021-3-6

1、 About sed
Sed: stream editor stream editor is also known as line editor, which only edit one line at a time. Sed works in “schema space” and does not operate on source files. No harm to source files.

2、 Sed usage format
The common formats of the SED command are as follows:
(1)Sed [options] ‘script’ input_file…
(2)Sed [options] –f script_file input_file…
(3)Sed [options] ‘ADDR1,ADDR2command’ input_file…
For example, # sed’1,2d’fstab can delete the first two lines of fstab and display them, but it will not change the source file.
#Sed ‘1,2! D’ fstab means to delete all rows except the first and second.
(4)Sed [options] ‘/PATTERN/command’ input_file……
&: S / L.. E / & Er /: refers to all the characters matched before the reference.
For example: sed ‘/ [[: Upper:]] / D’ binary.sh Indicates that all uppercase lines are deleted.
(5)Sed ‘/PATTERN1/,/PATTERN2/command’ input_file……
Note: this means that the command is executed from the first line matched by pattern1 to the middle line matched by pattern2.

3、 Common options for sed:
– N: only the rows matched by sed are displayed. The remaining rows are not displayed. Examples are given below.
-i: The original file can be operated directly. By default, sed will not change the original file, but the – I option can modify the original file, which should be used with caution.
-r: You can use standard regular expressions. By default, sed only supports basic regular expressions, but with the – R option, it supports extended regular expressions
-e: Multi script sharing. Multiple commands can be executed at the same time. For example:

Copy code

The code is as follows:

He love His lover.
She like Her liker.
Dove love Her liker.
It can also be used like this, separated by semicolons:

Copy code

The code is as follows:

4、 Use examples

Copy code

The code is as follows:

#Sed – e command line processing – a new
> sed -e ‘$ a widuu’ test.txt #In test.txt Add widuu at the end of

20151026113212056.png (426×100)

Copy code

The code is as follows:

#Replace – e with – n
> sed -n ‘$ a widuu’ test.txt #Only the processed line is displayed

20151026113238876.png (402×48)

Copy code

The code is as follows:

#- C replaces how many behavior strings you set here mine test.txt The content is shown in the figure
> sed -e ‘2,5c xiaowei’ test.txt #Replace lines 2-5 with the string Xiaowei

20151026113258767.png (465×267)

Copy code

The code is as follows:

#Multi command execution-e
> sed -e ‘2,5c xiaowei’ -e ‘s/cccc/widuu/’ test.txt

20151026113315497.png (599×134)

Copy code

The code is as follows:

#- I do not output – s substitution
>sed -i ‘s/111111/widuu/g’ test.txt

20151026113350275.png (535×319)

Copy code

The code is as follows:

#I insert a custom string in test.txt Insert Xiaowei in the second line of
> sed -e ‘2i xiaowei’ test.txt

20151026113425957.png (508×321)

Copy code

The code is as follows:

#D delete any input string
> sed -e ‘2d’ test.txt #Delete the second line

 
20151026113448647.png (432×290) 

Recommended Today

Rust and python: why rust can replace Python

In this guide, we compare the rust and python programming languages. We will discuss the applicable use cases in each case, review the advantages and disadvantages of using rust and python, and explain why rust might replace python. I will introduce the following: What is rust? What is Python? When to use rust When to […]