Analytic sed-n '/^aaaa/{:a; N;${s/\ (. *bbbb[^\n]*\). */\1/p}; Ta} '

Source: Internet
Author: User

personal understanding, seeking the truth. Let 's take a look at this order.
Sed-n '/^aaaa/{:a; N;${s/\ (. *bbbb[^\n]*\). */\1/p}; Ta} ' A.txt
The result of his implementation is to find all the contents of the last line that the AAAA to bbbb string in a file can match to

sed-n '/^2012/{:a; N;${s/\ (. *2014[^\n]*\). */\1/p}; Ta} ' a.txt
A.txt as follows: AAAAbbbbCCCCCCCC1111222233335555xxxx
The decomposition is as follows:sed-n '/^2012/{: AN${s/\ (. *2014[^\n]*\). */\1/p}Ta}
1. First the first ^2012 is to match the line starting with 2012 (if the string is in the middle of the case ^ can be removed) I personally think that the effect is better, the first step of the test is to let us follow the command to know where we have started to execute, When we first found the first 2012, I was able to execute it later.
 2. The first step we obviously matched to the first line: After the aaaa matches, we skip: A, and finallyafter the match, we executed the n command. Now look at the meaning of the n commandN: Append the next input line to the end of the read line to add a \ n buffer (pattern apace)so now the content of our buffer is aaaa\n2012 bbbbthen the matching match is ${s/\ (. *2014[^\n]*\). */\1/p}now to parse this first ${} is to match the last line of content, that is, if the last line in the buffer is the contents of${s/\ (. *2014[^\n]*\). */\1/p} then the state of this command is yes otherwise no then this state to be used with the latter TA to use the last line is not the next matching contentnow again: A Ta this is a combination meaning:first set up a label a if the command status in front of TA is no, it is not matched to, return to the label A, if yes this command ends print out buffercontinue with the following command, so that you can achieve the purpose of the loop. now let's see, just our buffer is after aaaa\n2012 bbbb the match of course is the last line of data that matches our buffer, now the last act of BBBB ${s/\ (. *2014[^\n]*\). */\1/p} Well, apparently not, so his state is no.we jumped to a and then N, and now our buffer is aaaa\n2012 BBBB executes n and adds \ n and then the next line, aaaa\n2012 bbbb\n\2013 CCCCnow our buffer is aaaa\n2012 bbbb\n\2013 CCCC and then does the subsequent line match to ${s/\ (. *2014[^\n]*\). */\1/p} apparently not down.we finally reached 2014 1111 lines in this line our buffer is aaaa\n2012 bbbb\n\2013cccc\n .... 1111at this point the last line of our buffer is 1111in front of him is any number of any character--in accordance withfollowed by any number of non-newline characters--conforming toCommand Status Yesta loop end output buffer content empty bufferAAAAbbbbCCCCCCCC1111Although the loop ends but the match is not over, Seb still has to traverse the following contentnow we're up to 2014 2222 executed n now buffer is 2222\n2014 3333then Match ${s/\ (. *2014[^\n]*\). */\1/p} match status Yes Loop endOutput buffer 2222\n2014 3333Why did we jump 2014 1111 when we matched to CCCC? 2222because our 2014 1111 has been changed by n the structure becomes the content line number of the previous line also changes this is a little more abstractthen we then matched to 2015 5555 execute n buffer for 5555\n2015 xxxxMatch ${s/\ (. *2014[^\n]*\). */\1/p} does not hold state no Execute TA Loop to a continue to execute n ... There's no 2014 in the back, so the command status is no until you know the end will not output a buffer.
In fact, the ^aaaa of this command can be used as AAAA instead of a more useful test
This is basically the meaning of not knowing whether my understanding is correct, if there are errors, to correct. Sincerely admire the original person.

Analytic sed-n '/^aaaa/{:a; N;${s/\ (. *bbbb[^\n]*\). */\1/p}; Ta} '

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: 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.