Home  >  Article  >  System Tutorial  >  Comprehensive summary: Detailed explanation of Linux sed multi-line processing

Comprehensive summary: Detailed explanation of Linux sed multi-line processing

WBOY
WBOYforward
2024-01-05 16:11:491103browse

Under normal circumstances, sed reads the line to be processed into the pattern space, and the commands in the script process the line one after another until the script is executed, and then the line is output, leaving the pattern space empty; Then repeat the previous action, and a new line in the file is read until the file is processed completely. However, various reasons, such as the user wanting a certain command in the script to be executed under certain conditions, or wanting the pattern space to be retained for next processing, may cause sed to not follow the instructions when processing files. Carry out the normal process. At this time, sed has set up some advanced commands to meet user requirements. If you want to learn the advanced commands of sed, you must first understand the following two buffer areas:

1. Definition of pattern space: Pattern space is a buffer area that saves what sed has just read from the input end.
2. Definition of hold space: Hold space is used to temporarily cache data when processing pattern space data.

There are also several command parameters:

g: Copy the contents of the hold space to the pattern space, and clear the contents of the original pattern space
G: Append the content in the hold space to the pattern space\n
h: Copy the contents of the pattern space to the hold space, and the contents of the original hold space are cleared
H: Append the content in pattern space to hold space\n
x: Exchange the contents of pattern space and hold space

For example, if we want to reverse the contents of a file, the file is as follows:

[qiu.li@l-tdata1.tkt.cn6 ~]$ cat tmp 
1-line
2-line
3-line

Execute the following command:

[qiu.li@l-tdata1.tkt.cn6 ~]$ sed '2,$G;h;$!d' tmp
3-line
2-line
1-line

Let’s gradually understand the above execution process

1. Let us analyze the following three commands:

  • 2,$G: Execute G command from the second line to the last line
  • h: Execute h command
  • $!d: Delete all lines except the last line

2. Specific operations

  1. Scan to the first line
    1. Put 1-line into the pattern space; the pattern space is still 1-line at this time;
    2. Directly execute the h command, the temporary storage space is 1-line at this time;
    3. Execute the d command to delete the only row of data in the pattern space. After deletion, the pattern space is empty
  2. Scan to the second line
    1. Put 2-line into pattern space
    2. Execute the G command to add the 1-line of the temporary storage space to the back of the 2-line of the pattern space. At this time, the pattern space is 2-line\n1-line;
    3. Execute h command, the content of the temporary storage space is 2-line\n1-line;
    4. Execute the d command and the pattern space will be cleared
  3. Scan to the third line
    1. Put 3-line into pattern space,
    2. Execute the G command to add the 2-line\n1-line of the temporary space to the back of the 3-line of the pattern space. At this time, the pattern space is 3-line\n2-line\n1-line;
    3. Execute h command, the content of the temporary storage space is 3-line\n2-line\n1-line;
    4. Do not execute $!d;
  4. Direct output 3-line\n2-line\n1-line

Of course, the command: sed '1!G;h;$!d' tmp can also have this effect.

The above is the detailed content of Comprehensive summary: Detailed explanation of Linux sed multi-line processing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete