0

This question already has an answer here:

I have an example dataset where a section has fields beginning with "something". I would like to obtain all of the lines in each "something" section when matching a specific string of "1234".

I was thinking that I could search for "1234" and print all lines before and after until matching "something".

Desired output:

something like this one
 1234
 abcd

something like this one
 zyxw
 1234 

Example dataset:

otherthings
otherthings
otherthings

something like this one
 1234
 abcd 

something not like this one
 xxxx
 yyyy 

something not like this one
 xxxx
 yyyy

something like this one
 1234
 abcd

otherthings
otherthings
otherthings

marked as duplicate by don_crissti, jlliagre, roaima, Scott, garethTheRed Apr 22 '16 at 6:18

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    So; are some records starting with something and others with otherthings (like in your example), or are all records starting with a unique identifier? I.e.: otherthings is really something ++ ? (If you get what I mean.) – Runium Apr 21 '16 at 22:42
0

Using "awk":

#!/bin/sh

awk '
    function print_section() {
        # Only print section if "1234" was encountered
        if (valid == 1) print section;
    }
    {
        if (/something/) {
            # Start new section
            section = $0; 
        }
        else if (/^\s*$/) {
            # Empty line -> output previous section
            if (section ne "") {
                print_section();
                section = "";
                valid = 0;
            }
        }
        else if (section ne "") {
            # Add line to section if one has been started
            section = section "\n" $0;
            if (/1234/) valid = 1;
        }
    }
    END {
        # End of file, print current section if it exists
        print_section();
    }
' file

Not the answer you're looking for? Browse other questions tagged or ask your own question.