Linux Three Musketeers – Sed command

Three Musketeers of Linux

At present, many companies require mastering some necessary Linux commands. At present, most of the company’s servers are applicable Linux systems, which requires us to learn some necessary Linux commands. We have already learned the basic commands of Linux. This article mainly studies the legendary Linux III Swordsman (grep, sed, awk)

This article introduces sed today, sed is similar to our windows editor. It takes time and a lot of practice to play the sed tool well. Here is just a list of some commonly used operations according to the needs of my work. Convenient for daily operation and maintenance and writing shell scripts for easy operation

Tips: If you are interested, you can learn more about regular expressions

Article directory

  • Linux Three Musketeers
  • foreword
  • what is sed
      • sed processing data flow
    • sed command
      • Common command options options:
      • Commonly used internal commands command
      • Common pattern space and hold space
        • sed add operation
        • sed delete operation
        • sed substitution operation
        • sed print operation

Foreword

Tip: Here you can add the general content to be recorded in this article:

For example: With the continuous development of artificial intelligence, the technology of machine learning is becoming more and more important. Many people have started to learn machine learning. This article introduces the basic content of machine learning.

Reminder: The following is the text of this article, the following case is for reference

What is sed

sed is an external command provided in linux. It is a line (stream) editor. It can add, delete, modify and check the content of the file non-interactively. The user can only enter the editing command on the command line, specify the file name, and then View the output on the screen. It is fundamentally different from a text editor.
That is, the former processes one text at a time, while the latter processes one line of a text at a time. This is what we should figure out and keep in mind, otherwise we may not be able to understand the operating principle and essence of sed.

sed processing data flow

image.png

sed command

Syntax:

sed [options] '{<!-- -->command}[flags]' [filename]
# Content in square brackets must be present, content in curly brackets is optional
sed # execute command
[options] # command options
{<!-- -->command}[flags] # sed internal options and parameters
[filename] # file

Common command options options:

-e script Add the commands specified in the script to the commands executed when processing input Multi-condition, there must be multiple operations in one line
-f script Add the command specified in the file to the commands executed when processing input
-n suppress automatic output
-i Edit the content of the file, modify the source file
-i.bak Create a .bak backup file at the same time as it is modified.
-r use extended regular expressions
! Negate (different from the shell after the pattern condition)

Common internal command command

a is added after the match
i is added before the match
p print
d delete
s find and replace
c change
y conversion N D P
n reads the next line of input and processes it from the next command instead of the first

flags
A number representing the pattern for new text replacement
g: Indicates to replace all instances of existing text with new text
p: Indicates to print the original content
w filename: write the result of the replacement to a file

Common pattern space and holding space

  • h: Copy the contents of pattern space to hold space
  • H: Add the contents of the pattern space to the hold space
  • g: take the contents of hold space and copy it to the pattern buffer
  • G: Take out the contents of the holding space,

image.png

sed add operation

  • a: add a record after the text
  • i: Insert a record in the text
[root@localhost sedTest]# cat demo
1abcdef first
2adbche test
3adbcef demo
4abcdef
5abcdef
# insert after3 in the third row
[root@localhost sedTest]# sed '3a\after3' demo
1abcdef first
2adbche test
3adbcef demo
after3
4abcdef
5abcdef
# Insert after this line after each line
[root@localhost sedTest]# sed 'a\after this line' demo
1abcdef first
after this line
2adbche test
after this line
3adbcef demo
after this line
4abcdef
after this line
5abcdef
after this line
# Match the line with ad and insert it after this line
[root@localhost sedTest]# sed '/ad/a\after this line' demo
1abcdef first
2adbche test
after this line
3adbcef demo
after this line
4abcdef
5abcdef
# Match all lines starting with 2ad, add after this line after it
[root@localhost sedTest]# sed '/^2ad/a\after this line' demo
1abcdef first
2adbche test
after this line
3adbcef demo
4abcdef
5abcdef

# Add before2 before the second line in the demo file
[root@localhost sedTest]# sed '2i\before2' demo
1abcdef first
before2
2adbche test
3adbcef demo
4abcdef
5abcdef
# If no line number is specified, it will be inserted before each line globally
[root@localhost sedTest]# sed 'i\before this line' demo
before this line
1abcdef first
before this line
2adbche test
before this line
3adbcef demo
before this line
4abcdef
before this line
5abcdef

sed delete operation

# delete line 3
[root@localhost sedTest]# sed '2d' demo
1abcdef first
3adbcef demo
4abcdef
5abcdef
# delete the line containing ad
[root@localhost sedTest]# sed '/ad/d' demo
1abcdef first
4abcdef
5abcdef
# delete all rows
[root@localhost sedTest]# sed 'd' demo
[root@localhost sedTest]#
# Delete the contents of the original file
[root@localhost sedTest]# cat demo
1abcdef first
2adbche test
3adbcef demo
4abcdef
5abcdef
[root@localhost sedTest]# sed -i 'd' demo
[root@localhost sedTest]# cat demo
[root@localhost sedTest]#

sed replacement operation

  • c: change the line: the operation of replacing a whole line. Format: sed ‘s\content to be replaced’ file
  • s: format: sed ‘s\replaced content\content to be replaced’ file
# c Replace the content of the second line with change the new line
[root@localhost sedTest]# sed '2c\change the new line' demo
1abcdef first
change the new line
3adbcef demo
4abcdef
5abcdef
# s matches all ad, replaced by ab
[root@localhost sedTest]# sed 's\ad\ab\' demo
1abcdef first
2 abbche test
3abbcef demo
4abcdef
5abcdef

sed print operation

[root@localhost sedTest]# sed 'p' demo
1abcdef first
1abcdef first
2adbche test
2adbche test
3adbcef demo
3adbcef demo
4abcdef
4abcdef
5abcdef
5abcdef
# Why is a duplicate line printed?
The P command is the print pattern space (print pattern space), which is an active buffer. When the sed editor executes the command,
It holds the text to be checked by the sed editor. In short, sed 'p' will add line by line to a temporary storage area, and output line by line
# Print the content of lines 2-4
[root@localhost sedTest]# sed -n '2,4p' demo
2adbche test
3adbcef demo
4abcdef
# Print the next line of the specified line n Multiple commands need to use {} at the same time, separated by a semicolon;
[root@localhost sedTest]# sed -n '2{n;p}' demo
3adbcef demo
# print the specified line and the next line N
[root@localhost sedTest]# sed -n '2{N;p}' demo
2adbche test
3adbcef demo

# print odd and even lines
# print odd lines
[root@localhost sedTest]# sed -n '{p;n}' demo meaning: skip the next line after printing the first line
1abcdef first
3adbcef demo
5abcdef
[root@localhost sedTest]# sed -n '{n;p}' demo meaning: skip the first line and print the next line
2adbche test
4abcdef
# Print two lines after printing the second line
[root@localhost sedTest]# sed -n '2, + 2p' demo
2adbche test
3adbcef demo
4abcdef