An essay on the Pattern space and hold space in SED

Source: Internet
Author: User
Tags processing text

The first part is a subset of concepts and examples, which are transferred from: http://coolshell.cn/articles/9104.html
Pattern Space
The
0th one is about the-n parameter, we may not understand, it's okay, let's take a look at the pseudo-code of the SED processing text and learn about the concept of pattern space:
  1. foreach line in file {
  2. //放入把行Pattern_Space
  3. Pattern_Space <= line;
  4. // 对每个pattern space执行sed命令
  5. Pattern_Space <= EXEC(sed_cmd, Pattern_Space);
  6. // 如果没有指定 -n 则输出处理后的Pattern_Space
  7. if (sed option hasn‘t "-n") {
  8.   print Pattern_Space
  9. }
  10. }
Address
The first one is about address, almost all of the above commands are like this (note: one of them!) indicates whether a command is executed after a successful match
[Address[,address]] [!] {cmd}
Address can be a number, or it can be a pattern, you can use a comma to separate two address represents two address of the interval, to execute command cmd, pseudo-code as follows:
  1. bool bexec = false
  2. foreach line in file {
  3. if ( match(address1) ){
  4. bexec = true;
  5. }
  6. if ( bexec == true) {
  7. EXEC(sed_cmd);
  8. }
  9. if ( match (address2) ) {
  10. bexec = false;
  11. }
  12. }

About address you can use relative locations, such as:

  1. # 其中的+3表示后面连续3行
  2. $ sed ‘/dog/,+3s/^/# /g‘ pets.txt
  3. This is my cat
  4. my cat‘s name is betty
  5. # This is my dog
  6. # my dog‘s name is frank
  7. # This is my fish
  8. # my fish‘s name is george
  9. This is my goat
  10. my goat‘s name is adam
Command packaging
cmd can be multiple, they can be separated by semicolons, and can be enclosed in curly braces as nested commands. Here are a few examples:
  1. $ cat pets.txt
  2. This is my cat
  3. my cat‘s name is betty
  4. This is my dog
  5. my dog‘s name is frank
  6. This is my fish
  7. my fish‘s name is george
  8. This is my goat
  9. my goat‘s name is adam
  10. # 对3行到第6行,执行命令/This/d
  11. $ sed ‘3,6 {/This/d}‘ pets.txt
  12. This is my cat
  13. my cat‘s name is betty
  14. my dog‘s name is frank
  15. my fish‘s name is george
  16. This is my goat
  17. my goat‘s name is adam
  18. # 对3行到第6行,匹配/This/成功后,再匹配/fish/,成功后执行d命令
  19. $ sed ‘3,6 {/This/{/fish/d}}‘ pets.txt
  20. This is my cat
  21. my cat‘s name is betty
  22. This is my dog
  23. my dog‘s name is frank
  24. my fish‘s name is george
  25. This is my goat
  26. my goat‘s name is adam
  27. # 从第一行到最后一行,如果匹配到This,则删除之;如果前面有空格,则去除空格
  28. $ sed ‘1,${/This/d;s/^ *//g}‘ pets.txt
  29. my cat‘s name is betty
  30. my dog‘s name is frank
  31. my fish‘s name is george
  32. my goat‘s name is adam
Hold Space
Next, we need to understand the concept of hold space, let's take a look at four commands:

G: Copy contents of hold space to pattern space, the contents of the original pattern space are cleared
G: After append the contents of hold space to the pattern space\n
H: Copy the contents of the pattern space into hold space, and the contents of the original keep space are erased.
H: Append the contents of the pattern space to the hold space\n
X: Exchanging the contents of pattern space and hold space

What is the use of these commands? Let's take a look at two examples, and the sample files used are:

    1. $ cat t.txt
    2. one
    3. two
    4. three

A first example:

    1. $ sed ‘H;g‘ t.txt
    2. one
    3. one
    4. two
    5. one
    6. two
    7. three

is not a bit not understand, I make a diagram you can understand.

The second example, reverse the line of a file:

    1. $ sed ‘1!G;h;$!d‘ t.txt
    2. three
    3. two
    4. one

One of ' 1! G;h;$!d ' can be disassembled as three commands

1! g--only the first line does not execute the G command, append the contents of hold space back to the pattern space
h--the first line executes the H command to copy the contents of the pattern space into hold space
$!d--except the last line does not execute the D command, the other rows execute the d command, delete the forward

Attach your own understanding: SED has P-zone (Pattern space) and H-zone (hold space), each read a row will put the content into the P-zone, then if we want to do something about the content before, we need to use the H area for staging some data.

Take the last diagram above, read the first line, read one to P area, and then H command to the H zone;

After reading to the P-area, then append to the H-zone, delete the P-zone in order not to output the content;

Read the last line, append the data to the H-zone, then replace the P-zone with the contents of the H-zone, and finally output the content of P-zone.

An essay on the Pattern space and hold space in SED

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.