SED advanced usage: pattern spaces and retention spaces (hold space)

Source: Internet
Author: User

Environment: CentOS 6.4

SED version: sed-4.2.1-10.el6.x86_64


The previous basic usage already meets the needs of 90%, and people who think they want to really master sed also need to learn the advanced usage of SED, and this section explains the mode space and space of sed in detail.


The pattern space is also mentioned in the previous basic usage, that is, a temporary buffer for processing a line of content in a file. After a row is processed, the contents of the pattern space are printed to standard output, and then the cache is automatically emptied.

And here the space to maintain is another buffer in sed, which, like its name, does not automatically empty, but does not actively print the contents of this buffer into standard output. Instead, the following SED command is required for processing:

D Delete pattern spaces.    Start next cycle.       Delete the contents of pattern spaces and start the next loop.   H, h copy/append pattern spaces to hold spaces.       Copy/Append the contents of pattern spaces to hold space.   G, G copy/append hold space.       Copy/Append the contents of hold space to pattern spaces.    X Exchange the contents of the hold and pattern spaces. Exchange the contents of hold space and pattern spaces.

Here is an example to illustrate the use of the above command, for example, there is a file, we can use the TAC command reverse order to print out the contents of the file, in fact, with the SED can also be achieved, here first the results listed, and then detailed explanation


Yes, just one line of commands can be achieved:

Sed ' 1! G;h;$!d ' File

You can learn about the processing mechanism and process of SED through the following explanations:

Description

The first line is the contents of the file, the arrow refers to the row currently processed by SED, the sed command on the right, and the next three lines to the left green to the mode space content, and the right blue to preserve the space content. Each time sed reads only one row to the pattern space in the file, that is, the current processing of the rows in the schema space only in the file before each sed command is performed, which is not shown in the diagram. Each line represents the contents of the last two spaces after a command is processed

The trap is clear for each step, so only the results of the execution are graphically expressed without explanation, and can be contrasted with the previous instructions to the SED command to deepen the understanding

1, read the file line "" of the Row "", the last command to delete the mode space content, so will not print the content on the screen


2, read the second line "Sencond" for processing, the last command to remove the mode space content, so will not print the content on the screen


3, read the third line "three" for processing, note that the SED will be processed after processing, will be the mode space to print content to the screen and automatically empty the mode space (this is not shown in the diagram)



In fact, this feature can also be implemented with the following command:

Sed-n ' 1! G;h $p ' file
Description

-N: Indicates that the processing result of the SED is not exported, but want to output, you need to display the "P" command


In fact, there are several advanced commands for SED, which can be divided into three groups, followed by the opportunity to learn the other two groups of commands:

Handles multiple-line mode space (N, D, P).   Maintain space to preserve the contents of the pattern space and make it available for subsequent commands (H, H, G, G, X). Write scripts that use branching and conditional directives to change the control flow (:, B, t).


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.