ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
You are merely merging each two lines. Simplest might be to save "Name" as well as key (presumably "Section") records, then print when an "Age" record is found.
Look carefully at the data. One section can contain many names.
You need 2 variables, KEY and TEXT
When "Section" pattern is encountered, assign $0 to KEY.
When "Name" pattern is encountered, concatenate, KEY (contains SECTION) and this $o (NAME) to TEXT.
When "Age" pattern is encountered, append $0 to the variable TEXT and print ti out and initialize TEXT.
Finally (on END), if TEXT variable contains a value, then print it out.
If you hard-code "Name" and "Age", then it is like this.
There is still two techniques, one with getline, and one with variables.
I always prefer the variables.
Here is an advanced solution that uses many variables.
It only depends on "Section" but does not need to know what follows.
If "Section" is met it sets state variable first and jumps to the next input cycle.
If first is set it must be on the next line; then it learns the first following key ffkey, "Name" in this case.
Because it does not know the ending key, it has to delay the printing of the final EOL ("\n" or ORS in awk). This is done when the next "Section" is met (I put in the first following printing), or in the END section.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.