Detailed explanation of shell script (ten)-how to use sed editor

@ TOC

One, sed editor

  • Sed is a stream editor, the stream editor will edit the data stream based on a set of rules provided in advance before the editor processes the data.
  • The sed editor can process the data in the data stream according to commands, which are either entered from the command line or stored in a command text file.

Two, sed editor workflow

1. Read:

  • sed reads a line of content from the input stream (file, pipe, standard input) and stores it in a temporary buffer (also known as pattern space)

2. Implementation:

  • By default, all sed commands are executed sequentially in the pattern space. Unless the address of the line is specified, the sed command will be executed sequentially on all lines.

3. Display:

  • Send the modified content to the output stream. After sending the data, the pattern space will be emptied. Before all the file contents are processed, the above process will be repeated until all the contents are processed.

4. Note:

  • Before all the file contents are processed, the above process will be repeated until all the contents are processed.
  • By default, all sed commands are executed in the pattern space, so the input file will not change in any way, unless the output is stored by redirection.

Three, command format

Insert picture description here

Four, commonly used options

Options Description
-e or--expression= Indicates that the specified command is used to process the input text file. When there is only one operation command, it can be omitted. Generally, it is used when executing multiple operation commands.
f or--file= Indicates that the specified script file is used to process the input text file
h or--help Show help
-n,--quiet or silent Sed editor output is prohibited, but it can be used with the p command to complete the output
-i Modify the target text file directly

Insert picture description here

Insert picture description here

Five, common operations

operating Description
s Replace, replace the specified character
d Delete, delete the selected row
a Increase, add a line of specified content below the current line
i Insert, in the selected row, insert a row of specified content above
c Replace, replace the selected line with the specified content
Y Character conversion, the length of characters before and after conversion must be the same
p Print, if you specify a line at the same time, it means to print the specified line; if you do not specify a line, it means to print all the content; if there are non-printing characters, it will be output in ASCII code. It is usually used with the "-n" option
= Print line number
l (lowercase L) Print the text in the data stream and non-printable ASCII characters (such as the terminator $, the tab character \t)

6. Use address

The sed editor has 2 addressing modes:

  • Represent the row interval in numeric form
  • Use text mode to filter out lines

Insert picture description here

Insert picture description here

Seven, delete rows

Insert picture description here

Insert picture description here

8. Replace

1. Format

Insert picture description here

2, 4 kinds of replacement marks

  • Number: Indicates the number of matches that the new string will replace
  • g: Indicates that the new character will replace all matching places
  • p: Print the line matching the replace command, used with -n
  • w file: write the result of the replacement to the file

Insert picture description here
Insert picture description here

Nine, insert

Insert picture description here

Guess you like

Origin blog.csdn.net/Lucien010230/article/details/114817995