Grep starting from a fixed text, until the first blank line

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
8
down vote

favorite
2












I have a file prova.txt like this:



Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random2
random3
random4

extra1
extra2
bla

Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561

extra2
bla
bla

Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random22131


and I need to grep out from "Start to grab here" to the first blank line. The output should be like this:



Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random2
random3
random4

Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561

Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random22131


As you can see the lines after "Start to grab here" are random, so -A -B grep flag don't work:



cat prova.txt | grep "Start to grab from here" -A 15 | grep -B 15 "^$" > output.txt


Can you help me to find a way that catch the first line that will be grabbed (as "Start to grab from here"), until a blank line. I cannot predict how many random lines I will have after "Start to grab from here".



Any unix compatible solution is appreciate (grep, sed, awk is better than perl or similar).



EDITED: after brilliant response by @john1024, I would like to know if it's possible to:



1° sort the block (according to Start to grab from here: 1 then 1 then 2)



2° remove 4 (alphabetically random) lines fix1,fix2,fix3,fix4 but are always 4



3° eventually remove random dupes, like sort -u command



Final output shoul be like this:



# fix lines removed - match 1 first time
Start to grab from here: 1
random1
random2
random3
random4

#fix lines removed - match 1 second time
Start to grab from here: 1
#random1 removed cause is a dupe
random22131

#fix lines removed - match 2 that comes after 1
Start to grab from here: 2
random1546
random2561


or



# fix lines removed - match 1 first time and the second too
Start to grab from here: 1
random1
random2
random3
random4
#random1 removed cause is a dupe
random22131

#fix lines removed - match 2 that comes after 1
Start to grab from here: 2
random1546
random2561


The second output is better that the first one. Some other unix command magic is needed.










share|improve this question























  • This is really helpful for grabbing the stack trace for a particular thread from java jstack output. Glad I found this Q&A!
    – BenjaminBallard
    Sep 14 at 14:00














up vote
8
down vote

favorite
2












I have a file prova.txt like this:



Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random2
random3
random4

extra1
extra2
bla

Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561

extra2
bla
bla

Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random22131


and I need to grep out from "Start to grab here" to the first blank line. The output should be like this:



Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random2
random3
random4

Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561

Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random22131


As you can see the lines after "Start to grab here" are random, so -A -B grep flag don't work:



cat prova.txt | grep "Start to grab from here" -A 15 | grep -B 15 "^$" > output.txt


Can you help me to find a way that catch the first line that will be grabbed (as "Start to grab from here"), until a blank line. I cannot predict how many random lines I will have after "Start to grab from here".



Any unix compatible solution is appreciate (grep, sed, awk is better than perl or similar).



EDITED: after brilliant response by @john1024, I would like to know if it's possible to:



1° sort the block (according to Start to grab from here: 1 then 1 then 2)



2° remove 4 (alphabetically random) lines fix1,fix2,fix3,fix4 but are always 4



3° eventually remove random dupes, like sort -u command



Final output shoul be like this:



# fix lines removed - match 1 first time
Start to grab from here: 1
random1
random2
random3
random4

#fix lines removed - match 1 second time
Start to grab from here: 1
#random1 removed cause is a dupe
random22131

#fix lines removed - match 2 that comes after 1
Start to grab from here: 2
random1546
random2561


or



# fix lines removed - match 1 first time and the second too
Start to grab from here: 1
random1
random2
random3
random4
#random1 removed cause is a dupe
random22131

#fix lines removed - match 2 that comes after 1
Start to grab from here: 2
random1546
random2561


The second output is better that the first one. Some other unix command magic is needed.










share|improve this question























  • This is really helpful for grabbing the stack trace for a particular thread from java jstack output. Glad I found this Q&A!
    – BenjaminBallard
    Sep 14 at 14:00












up vote
8
down vote

favorite
2









up vote
8
down vote

favorite
2






2





I have a file prova.txt like this:



Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random2
random3
random4

extra1
extra2
bla

Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561

extra2
bla
bla

Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random22131


and I need to grep out from "Start to grab here" to the first blank line. The output should be like this:



Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random2
random3
random4

Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561

Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random22131


As you can see the lines after "Start to grab here" are random, so -A -B grep flag don't work:



cat prova.txt | grep "Start to grab from here" -A 15 | grep -B 15 "^$" > output.txt


Can you help me to find a way that catch the first line that will be grabbed (as "Start to grab from here"), until a blank line. I cannot predict how many random lines I will have after "Start to grab from here".



Any unix compatible solution is appreciate (grep, sed, awk is better than perl or similar).



EDITED: after brilliant response by @john1024, I would like to know if it's possible to:



1° sort the block (according to Start to grab from here: 1 then 1 then 2)



2° remove 4 (alphabetically random) lines fix1,fix2,fix3,fix4 but are always 4



3° eventually remove random dupes, like sort -u command



Final output shoul be like this:



# fix lines removed - match 1 first time
Start to grab from here: 1
random1
random2
random3
random4

#fix lines removed - match 1 second time
Start to grab from here: 1
#random1 removed cause is a dupe
random22131

#fix lines removed - match 2 that comes after 1
Start to grab from here: 2
random1546
random2561


or



# fix lines removed - match 1 first time and the second too
Start to grab from here: 1
random1
random2
random3
random4
#random1 removed cause is a dupe
random22131

#fix lines removed - match 2 that comes after 1
Start to grab from here: 2
random1546
random2561


The second output is better that the first one. Some other unix command magic is needed.










share|improve this question















I have a file prova.txt like this:



Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random2
random3
random4

extra1
extra2
bla

Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561

extra2
bla
bla

Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random22131


and I need to grep out from "Start to grab here" to the first blank line. The output should be like this:



Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random2
random3
random4

Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561

Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random22131


As you can see the lines after "Start to grab here" are random, so -A -B grep flag don't work:



cat prova.txt | grep "Start to grab from here" -A 15 | grep -B 15 "^$" > output.txt


Can you help me to find a way that catch the first line that will be grabbed (as "Start to grab from here"), until a blank line. I cannot predict how many random lines I will have after "Start to grab from here".



Any unix compatible solution is appreciate (grep, sed, awk is better than perl or similar).



EDITED: after brilliant response by @john1024, I would like to know if it's possible to:



1° sort the block (according to Start to grab from here: 1 then 1 then 2)



2° remove 4 (alphabetically random) lines fix1,fix2,fix3,fix4 but are always 4



3° eventually remove random dupes, like sort -u command



Final output shoul be like this:



# fix lines removed - match 1 first time
Start to grab from here: 1
random1
random2
random3
random4

#fix lines removed - match 1 second time
Start to grab from here: 1
#random1 removed cause is a dupe
random22131

#fix lines removed - match 2 that comes after 1
Start to grab from here: 2
random1546
random2561


or



# fix lines removed - match 1 first time and the second too
Start to grab from here: 1
random1
random2
random3
random4
#random1 removed cause is a dupe
random22131

#fix lines removed - match 2 that comes after 1
Start to grab from here: 2
random1546
random2561


The second output is better that the first one. Some other unix command magic is needed.







sed awk regular-expression sort






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 24 '16 at 19:29

























asked Oct 24 '16 at 18:40









heisen

6017




6017











  • This is really helpful for grabbing the stack trace for a particular thread from java jstack output. Glad I found this Q&A!
    – BenjaminBallard
    Sep 14 at 14:00
















  • This is really helpful for grabbing the stack trace for a particular thread from java jstack output. Glad I found this Q&A!
    – BenjaminBallard
    Sep 14 at 14:00















This is really helpful for grabbing the stack trace for a particular thread from java jstack output. Glad I found this Q&A!
– BenjaminBallard
Sep 14 at 14:00




This is really helpful for grabbing the stack trace for a particular thread from java jstack output. Glad I found this Q&A!
– BenjaminBallard
Sep 14 at 14:00










2 Answers
2






active

oldest

votes

















up vote
10
down vote













Using awk



Try:



$ awk '/Start to grab/,/^$/' prova.txt
Start to grab from here: 1
random1
random2
random3
random4

Start to grab from here: 2
random1546
random2561

Start to grab from here: 3
random45
random22131


/Start to grab/,/^$/ defines a range. It starts with any line that matches Start to grab and ends with the first empty line, ^$, that follows.



Using sed



With very similar logic:



$ sed -n '/Start to grab/,/^$/p' prova.txt
Start to grab from here: 1
random1
random2
random3
random4

Start to grab from here: 2
random1546
random2561

Start to grab from here: 3
random45
random22131


-n tells sed not to print anything unless we explicitly ask it to. /Start to grab/,/^$/p tells it to print any lines in the range defined by /Start to grab/,/^$/.






share|improve this answer




















  • Your solution is perfect, I have edited my ask to add something. Relly apreciate your help. Thank you
    – heisen
    Oct 24 '16 at 19:08

















up vote
0
down vote













I am posting an alternative solution as it may be useful to some peoples use cases. This solution does not comply exactly to the stated requirements, for the best solution see the answer from @John1024.



You can use awk with the Record Separator set to an empty string, awk will interpret these as blank newlines:



$ awk '/Start/' RS= prova.txt 
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random2
random3
random4
Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random22131


This version does not preserve the blank newlines in the output. It will also show context before the match if present. This behaviour can be very useful when grepping for something in a file and you want to see the newline delimited block it is a part of, for example:



$ awk '/random1546/' RS= prova.txt 
Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561


For example I find this useful when grepping for things in ini files.






share|improve this answer




















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f318595%2fgrep-starting-from-a-fixed-text-until-the-first-blank-line%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    10
    down vote













    Using awk



    Try:



    $ awk '/Start to grab/,/^$/' prova.txt
    Start to grab from here: 1
    random1
    random2
    random3
    random4

    Start to grab from here: 2
    random1546
    random2561

    Start to grab from here: 3
    random45
    random22131


    /Start to grab/,/^$/ defines a range. It starts with any line that matches Start to grab and ends with the first empty line, ^$, that follows.



    Using sed



    With very similar logic:



    $ sed -n '/Start to grab/,/^$/p' prova.txt
    Start to grab from here: 1
    random1
    random2
    random3
    random4

    Start to grab from here: 2
    random1546
    random2561

    Start to grab from here: 3
    random45
    random22131


    -n tells sed not to print anything unless we explicitly ask it to. /Start to grab/,/^$/p tells it to print any lines in the range defined by /Start to grab/,/^$/.






    share|improve this answer




















    • Your solution is perfect, I have edited my ask to add something. Relly apreciate your help. Thank you
      – heisen
      Oct 24 '16 at 19:08














    up vote
    10
    down vote













    Using awk



    Try:



    $ awk '/Start to grab/,/^$/' prova.txt
    Start to grab from here: 1
    random1
    random2
    random3
    random4

    Start to grab from here: 2
    random1546
    random2561

    Start to grab from here: 3
    random45
    random22131


    /Start to grab/,/^$/ defines a range. It starts with any line that matches Start to grab and ends with the first empty line, ^$, that follows.



    Using sed



    With very similar logic:



    $ sed -n '/Start to grab/,/^$/p' prova.txt
    Start to grab from here: 1
    random1
    random2
    random3
    random4

    Start to grab from here: 2
    random1546
    random2561

    Start to grab from here: 3
    random45
    random22131


    -n tells sed not to print anything unless we explicitly ask it to. /Start to grab/,/^$/p tells it to print any lines in the range defined by /Start to grab/,/^$/.






    share|improve this answer




















    • Your solution is perfect, I have edited my ask to add something. Relly apreciate your help. Thank you
      – heisen
      Oct 24 '16 at 19:08












    up vote
    10
    down vote










    up vote
    10
    down vote









    Using awk



    Try:



    $ awk '/Start to grab/,/^$/' prova.txt
    Start to grab from here: 1
    random1
    random2
    random3
    random4

    Start to grab from here: 2
    random1546
    random2561

    Start to grab from here: 3
    random45
    random22131


    /Start to grab/,/^$/ defines a range. It starts with any line that matches Start to grab and ends with the first empty line, ^$, that follows.



    Using sed



    With very similar logic:



    $ sed -n '/Start to grab/,/^$/p' prova.txt
    Start to grab from here: 1
    random1
    random2
    random3
    random4

    Start to grab from here: 2
    random1546
    random2561

    Start to grab from here: 3
    random45
    random22131


    -n tells sed not to print anything unless we explicitly ask it to. /Start to grab/,/^$/p tells it to print any lines in the range defined by /Start to grab/,/^$/.






    share|improve this answer












    Using awk



    Try:



    $ awk '/Start to grab/,/^$/' prova.txt
    Start to grab from here: 1
    random1
    random2
    random3
    random4

    Start to grab from here: 2
    random1546
    random2561

    Start to grab from here: 3
    random45
    random22131


    /Start to grab/,/^$/ defines a range. It starts with any line that matches Start to grab and ends with the first empty line, ^$, that follows.



    Using sed



    With very similar logic:



    $ sed -n '/Start to grab/,/^$/p' prova.txt
    Start to grab from here: 1
    random1
    random2
    random3
    random4

    Start to grab from here: 2
    random1546
    random2561

    Start to grab from here: 3
    random45
    random22131


    -n tells sed not to print anything unless we explicitly ask it to. /Start to grab/,/^$/p tells it to print any lines in the range defined by /Start to grab/,/^$/.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Oct 24 '16 at 18:43









    John1024

    45.1k4101118




    45.1k4101118











    • Your solution is perfect, I have edited my ask to add something. Relly apreciate your help. Thank you
      – heisen
      Oct 24 '16 at 19:08
















    • Your solution is perfect, I have edited my ask to add something. Relly apreciate your help. Thank you
      – heisen
      Oct 24 '16 at 19:08















    Your solution is perfect, I have edited my ask to add something. Relly apreciate your help. Thank you
    – heisen
    Oct 24 '16 at 19:08




    Your solution is perfect, I have edited my ask to add something. Relly apreciate your help. Thank you
    – heisen
    Oct 24 '16 at 19:08












    up vote
    0
    down vote













    I am posting an alternative solution as it may be useful to some peoples use cases. This solution does not comply exactly to the stated requirements, for the best solution see the answer from @John1024.



    You can use awk with the Record Separator set to an empty string, awk will interpret these as blank newlines:



    $ awk '/Start/' RS= prova.txt 
    Start to grab from here: 1
    fix1
    fix2
    fix3
    fix4
    random1
    random2
    random3
    random4
    Start to grab from here: 2
    fix1
    fix2
    fix3
    fix4
    random1546
    random2561
    Start to grab from here: 1
    fix1
    fix2
    fix3
    fix4
    random1
    random22131


    This version does not preserve the blank newlines in the output. It will also show context before the match if present. This behaviour can be very useful when grepping for something in a file and you want to see the newline delimited block it is a part of, for example:



    $ awk '/random1546/' RS= prova.txt 
    Start to grab from here: 2
    fix1
    fix2
    fix3
    fix4
    random1546
    random2561


    For example I find this useful when grepping for things in ini files.






    share|improve this answer
























      up vote
      0
      down vote













      I am posting an alternative solution as it may be useful to some peoples use cases. This solution does not comply exactly to the stated requirements, for the best solution see the answer from @John1024.



      You can use awk with the Record Separator set to an empty string, awk will interpret these as blank newlines:



      $ awk '/Start/' RS= prova.txt 
      Start to grab from here: 1
      fix1
      fix2
      fix3
      fix4
      random1
      random2
      random3
      random4
      Start to grab from here: 2
      fix1
      fix2
      fix3
      fix4
      random1546
      random2561
      Start to grab from here: 1
      fix1
      fix2
      fix3
      fix4
      random1
      random22131


      This version does not preserve the blank newlines in the output. It will also show context before the match if present. This behaviour can be very useful when grepping for something in a file and you want to see the newline delimited block it is a part of, for example:



      $ awk '/random1546/' RS= prova.txt 
      Start to grab from here: 2
      fix1
      fix2
      fix3
      fix4
      random1546
      random2561


      For example I find this useful when grepping for things in ini files.






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        I am posting an alternative solution as it may be useful to some peoples use cases. This solution does not comply exactly to the stated requirements, for the best solution see the answer from @John1024.



        You can use awk with the Record Separator set to an empty string, awk will interpret these as blank newlines:



        $ awk '/Start/' RS= prova.txt 
        Start to grab from here: 1
        fix1
        fix2
        fix3
        fix4
        random1
        random2
        random3
        random4
        Start to grab from here: 2
        fix1
        fix2
        fix3
        fix4
        random1546
        random2561
        Start to grab from here: 1
        fix1
        fix2
        fix3
        fix4
        random1
        random22131


        This version does not preserve the blank newlines in the output. It will also show context before the match if present. This behaviour can be very useful when grepping for something in a file and you want to see the newline delimited block it is a part of, for example:



        $ awk '/random1546/' RS= prova.txt 
        Start to grab from here: 2
        fix1
        fix2
        fix3
        fix4
        random1546
        random2561


        For example I find this useful when grepping for things in ini files.






        share|improve this answer












        I am posting an alternative solution as it may be useful to some peoples use cases. This solution does not comply exactly to the stated requirements, for the best solution see the answer from @John1024.



        You can use awk with the Record Separator set to an empty string, awk will interpret these as blank newlines:



        $ awk '/Start/' RS= prova.txt 
        Start to grab from here: 1
        fix1
        fix2
        fix3
        fix4
        random1
        random2
        random3
        random4
        Start to grab from here: 2
        fix1
        fix2
        fix3
        fix4
        random1546
        random2561
        Start to grab from here: 1
        fix1
        fix2
        fix3
        fix4
        random1
        random22131


        This version does not preserve the blank newlines in the output. It will also show context before the match if present. This behaviour can be very useful when grepping for something in a file and you want to see the newline delimited block it is a part of, for example:



        $ awk '/random1546/' RS= prova.txt 
        Start to grab from here: 2
        fix1
        fix2
        fix3
        fix4
        random1546
        random2561


        For example I find this useful when grepping for things in ini files.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 21 hours ago









        htaccess

        1,719145




        1,719145



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f318595%2fgrep-starting-from-a-fixed-text-until-the-first-blank-line%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)