Shell Scripting Learning Notes (advanced usage of SED----mode space and hold space)

Source: Internet
Author: User
Tags first row

Some time ago in the study shell script, the last mention of the SED mode space and maintain the concept of space, but has not studied well, these two days studied, so it sent, not very comprehensive, just for your reference.

Keep the space sed under normal circumstances, the processed rows are read into the pattern space, and the "sed command" in the script is processed one after the other until the script finishes executing. The line is then output, the mode is emptied, and then the action is repeated, and a new line in the file is read in until the file is processed.

Pattern space can be likened to a production line, while maintaining space can be likened to a warehouse, a metaphor that hopes to help you understand the relationship between the two.

Sed commands are used to operate to maintain space and pattern space common to have a bit of

For convenience, use p to represent the pattern space, m to represent the holding space


H: Copy the contents of the pattern space into the staging buffer (hold space)

H: Append the contents of the pattern space to the staging buffer (hold space)

G: Copy the contents of the staging buffer to the schema space, overwriting the original content

G: Append the contents of the staging buffer to the schema space, appended to the original content

D: Delete all ⾏ lines in the pattern and read into the next new line to P

D:D Delete the first line in M, p, and do not read into the next line

x: Swap to preserve space and pattern space content


Here I will use specific examples to explain how to use it:

1. Implementing the TAC function


1 of them. G means that the first row does not use G,$!d to indicate that the last row does not use D, and that the execution works are like this:


The above figure is the data in two space conversion process, do not know that we understand no, where the row is marked D for the D operation (delete the contents of the mode space), the G is marked G operation (the content is appended to the pattern space, this operation will not overwrite the original content), h is denoted by H operation (copy the schema space content to the hold space, which overwrites the original content). The final result is consistent with the results we see.


2. Transformation of Ranks


Let me explain the above statement, h means that the content of the pattern space is appended to the hold space, ${...} means the last execution, meaning that the contents of {} are finally executed, so the final time is to keep the contents of the space consistent with the contents of the cat, and x to swap the contents of the space and pattern space. Then the content in the mode space is the cat content, then use "s/p1/p2/g" replace the command, the newline character \ n, replaced by a space, so that the column becomes a row, and vice versa.


3.1~10 and (1~10 less, more convenient to observe, 1~100 and is also very simple)



The function of SEQ is to list the number of 1~n:


First: H---append to hold space

Second: ${x;s/\n/+/g;s/^+//;p}----The last line to execute (because ${}), swap to keep the contents of space and pattern space, replace \ n with +, and finally use the BC calculator to find out the 1~10.


The above is my understanding of maintaining space and pattern space and the use of some instructions to operate two space, may use the above instructions can achieve more functions, but I do not introduce here at home, this part is really a test of brainpower. If you are interested, you can do more research and realize more functions.


Related Article

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.