Match blank lines before a word awkHow can I “grep” patterns across multiple lines?Multiline pattern...

Is "survival" paracord with fire starter strand dangerous

Why is the Ellipsoid Method of polynomial complexity?

What checks exist against overuse of presidential pardons in the USA?

Is this homebrew "Faerie Fire Grenade" unbalanced?

Is it possible for a person to be tricked into becoming a lich?

Can this planet in a binary star system exist?

Is it recommended to point out a professor's mistake during their lecture?

I feel cheated by my new employer, does this sound right?

Fixing a blind bolt hole when the first 2-3 threads are ruined?

Storing milk for long periods of time

RAID0 instead of RAID1 or 5, is this crazy?

What should be done with the carbon when using magic to get oxygen from carbon dioxide?

Isometric Heyacrazy - Now In 3D!

Journal published a paper, ignoring my objections as a referee

Printing a list as "a, b, c." using Python

Get contents before a colon

What is the practical impact of using System.Random which is not cryptographically random?

Spicing up a moment of peace

How to investigate an unknown 1.5GB file named "sudo" in my Linux home directory?

Can a network vulnerability be exploited locally?

Create a list of snaking numbers under 50,000

How to differentiate between two people with the same name in a story?

How to understand payment due date for credit card?

Why doesn't Starship have four landing legs?



Match blank lines before a word awk


How can I “grep” patterns across multiple lines?Multiline pattern match using sed, awk or grepHow to perform a multi line grepHow to perform a multiline grep across multiple files?Pass colors from ls through pipe to awk 'print' statementHow to add “wrappers” around methods in source code based on a pattern, using sed, awk, grep and friendsCommand line method to find repeat-word typos, with line numbersPrint some lines before and after pattern match excluding lines matching another patternsMultiline Regexp (grep, sed, awk, perl)Change single blank lines to double blank lines using awkMatching columns of different csv files, not working when column value is different lengthPrint text before and after match, from a specific beginning and to an ending stringawk + append lines before captured word only if lines are not defined in the fileAnd operation and case insensitivity in awk regular expression?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















I have a long file (showing only a piece):



145 27262253 2093226 747883433 76303046 2.74331
146 27992017 2188217 747883433 76303046 2.8678
147 30385435 2433407 747883433 76303046 3.18913
148 31218703 2514902 747883433 76303046 3.29594
149 33852828 2660530 747883433 76303046 3.48679
150 36161756 2836045 747883433 76303046 3.71682

Alignments 747883433 76303046
Bases 111613795461 11392665612

1 40000373 2754292 838333186 82982133 3.31914
2 35955786 2451917 838333186 82982133 2.95475
3 33056935 2241392 838333186 82982133 2.70105
4 32241895 2172229 838333186 82982133 2.61771
145 29490370 2184347 838333186 82982133 2.63231
146 30252912 2282821 838333186 82982133 2.75098
147 32862262 2544600 838333186 82982133 3.06644
148 33769718 2631164 838333186 82982133 3.17076
149 36673113 2787718 838333186 82982133 3.35942
150 39222287 2975755 838333186 82982133 3.58602

Alignments 838333186 82982133
Bases 125129342261 12391027833

1 35736929 2509527 741319423 80995147 3.09837
2 32185143 2238927 741319423 80995147 2.76427
3 29595482 2043259 741319423 80995147 2.52269
4 28861157 1978254 741319423 80995147 2.44244


I want to match the blank line before Alignments word and the word itself. Expecting:



Alignments      747883433       76303046   

Alignments 838333186 82982133


Is it possible? I have many others blank lines and Alignments words.
My try: | awk '{if($1 ~ /^[[:space:]]*Alignments/) {print $0}}'. However, I get:



Alignments      747883433       76303046
Alignments 838333186 82982133









share|improve this question







New contributor



giannkas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






















  • You are after a multiline grep/awk. There are several questions that ask the same question.

    – Sparhawk
    1 hour ago


















0















I have a long file (showing only a piece):



145 27262253 2093226 747883433 76303046 2.74331
146 27992017 2188217 747883433 76303046 2.8678
147 30385435 2433407 747883433 76303046 3.18913
148 31218703 2514902 747883433 76303046 3.29594
149 33852828 2660530 747883433 76303046 3.48679
150 36161756 2836045 747883433 76303046 3.71682

Alignments 747883433 76303046
Bases 111613795461 11392665612

1 40000373 2754292 838333186 82982133 3.31914
2 35955786 2451917 838333186 82982133 2.95475
3 33056935 2241392 838333186 82982133 2.70105
4 32241895 2172229 838333186 82982133 2.61771
145 29490370 2184347 838333186 82982133 2.63231
146 30252912 2282821 838333186 82982133 2.75098
147 32862262 2544600 838333186 82982133 3.06644
148 33769718 2631164 838333186 82982133 3.17076
149 36673113 2787718 838333186 82982133 3.35942
150 39222287 2975755 838333186 82982133 3.58602

Alignments 838333186 82982133
Bases 125129342261 12391027833

1 35736929 2509527 741319423 80995147 3.09837
2 32185143 2238927 741319423 80995147 2.76427
3 29595482 2043259 741319423 80995147 2.52269
4 28861157 1978254 741319423 80995147 2.44244


I want to match the blank line before Alignments word and the word itself. Expecting:



Alignments      747883433       76303046   

Alignments 838333186 82982133


Is it possible? I have many others blank lines and Alignments words.
My try: | awk '{if($1 ~ /^[[:space:]]*Alignments/) {print $0}}'. However, I get:



Alignments      747883433       76303046
Alignments 838333186 82982133









share|improve this question







New contributor



giannkas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






















  • You are after a multiline grep/awk. There are several questions that ask the same question.

    – Sparhawk
    1 hour ago














0












0








0








I have a long file (showing only a piece):



145 27262253 2093226 747883433 76303046 2.74331
146 27992017 2188217 747883433 76303046 2.8678
147 30385435 2433407 747883433 76303046 3.18913
148 31218703 2514902 747883433 76303046 3.29594
149 33852828 2660530 747883433 76303046 3.48679
150 36161756 2836045 747883433 76303046 3.71682

Alignments 747883433 76303046
Bases 111613795461 11392665612

1 40000373 2754292 838333186 82982133 3.31914
2 35955786 2451917 838333186 82982133 2.95475
3 33056935 2241392 838333186 82982133 2.70105
4 32241895 2172229 838333186 82982133 2.61771
145 29490370 2184347 838333186 82982133 2.63231
146 30252912 2282821 838333186 82982133 2.75098
147 32862262 2544600 838333186 82982133 3.06644
148 33769718 2631164 838333186 82982133 3.17076
149 36673113 2787718 838333186 82982133 3.35942
150 39222287 2975755 838333186 82982133 3.58602

Alignments 838333186 82982133
Bases 125129342261 12391027833

1 35736929 2509527 741319423 80995147 3.09837
2 32185143 2238927 741319423 80995147 2.76427
3 29595482 2043259 741319423 80995147 2.52269
4 28861157 1978254 741319423 80995147 2.44244


I want to match the blank line before Alignments word and the word itself. Expecting:



Alignments      747883433       76303046   

Alignments 838333186 82982133


Is it possible? I have many others blank lines and Alignments words.
My try: | awk '{if($1 ~ /^[[:space:]]*Alignments/) {print $0}}'. However, I get:



Alignments      747883433       76303046
Alignments 838333186 82982133









share|improve this question







New contributor



giannkas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have a long file (showing only a piece):



145 27262253 2093226 747883433 76303046 2.74331
146 27992017 2188217 747883433 76303046 2.8678
147 30385435 2433407 747883433 76303046 3.18913
148 31218703 2514902 747883433 76303046 3.29594
149 33852828 2660530 747883433 76303046 3.48679
150 36161756 2836045 747883433 76303046 3.71682

Alignments 747883433 76303046
Bases 111613795461 11392665612

1 40000373 2754292 838333186 82982133 3.31914
2 35955786 2451917 838333186 82982133 2.95475
3 33056935 2241392 838333186 82982133 2.70105
4 32241895 2172229 838333186 82982133 2.61771
145 29490370 2184347 838333186 82982133 2.63231
146 30252912 2282821 838333186 82982133 2.75098
147 32862262 2544600 838333186 82982133 3.06644
148 33769718 2631164 838333186 82982133 3.17076
149 36673113 2787718 838333186 82982133 3.35942
150 39222287 2975755 838333186 82982133 3.58602

Alignments 838333186 82982133
Bases 125129342261 12391027833

1 35736929 2509527 741319423 80995147 3.09837
2 32185143 2238927 741319423 80995147 2.76427
3 29595482 2043259 741319423 80995147 2.52269
4 28861157 1978254 741319423 80995147 2.44244


I want to match the blank line before Alignments word and the word itself. Expecting:



Alignments      747883433       76303046   

Alignments 838333186 82982133


Is it possible? I have many others blank lines and Alignments words.
My try: | awk '{if($1 ~ /^[[:space:]]*Alignments/) {print $0}}'. However, I get:



Alignments      747883433       76303046
Alignments 838333186 82982133






text-processing awk






share|improve this question







New contributor



giannkas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question







New contributor



giannkas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question






New contributor



giannkas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked 1 hour ago









giannkasgiannkas

12 bronze badges




12 bronze badges




New contributor



giannkas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




giannkas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • You are after a multiline grep/awk. There are several questions that ask the same question.

    – Sparhawk
    1 hour ago



















  • You are after a multiline grep/awk. There are several questions that ask the same question.

    – Sparhawk
    1 hour ago

















You are after a multiline grep/awk. There are several questions that ask the same question.

– Sparhawk
1 hour ago





You are after a multiline grep/awk. There are several questions that ask the same question.

– Sparhawk
1 hour ago










5 Answers
5






active

oldest

votes


















0















You can use the below Awk for your problem. Match a empty line and see if the line starting with Alignments immediately starts after that



awk '!NF { line = NR; next } (NR = line + 1 ) && /^Alignments/{ printf "n%sn",$0; }' file





share|improve this answer


























  • we are hardcoring the newline in output... it would print the same if we don't have an empty line...

    – msp9011
    1 hour ago











  • @msp9011 : No only if the previous line is empty!

    – Inian
    59 mins ago











  • just check by removing empty line in input file...

    – msp9011
    52 mins ago



















0















Why don't we use grep? :



grep -A1 "^$" file | grep -B1 'Alignments' | grep -v -- "^--$"





share|improve this answer

































    0















    Sed excels in such tasks. First we stick the next line to the current provided the current is empty. Then interrogate and print upon meeting the criterion set.



    $ sed -ne '
    /./!N
    /^nAlignments/p
    ' file.txt





    share|improve this answer

































      0















      $ awk '/^$|^Alignments/' input.txt | uniq

      Alignments 747883433 76303046

      Alignments 838333186 82982133


      the uniq makes sure there will be no more than one blank line before, after, or between any Alignments lines.



      grep could be used instead. or sed -n. or perl -n. e.g.



      $ grep -E '^$|Alignments' input.txt | uniq





      share|improve this answer

































        0















        Using GNU awk:



        awk -v RS='nAlignments[ 0-9]*' '{print RT}' file


        The record separtor RS is set to the expected match and is printed for every record using RT (record terminator).






        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',
          autoActivateHeartbeat: false,
          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
          });


          }
          });






          giannkas is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f538190%2fmatch-blank-lines-before-a-word-awk%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0















          You can use the below Awk for your problem. Match a empty line and see if the line starting with Alignments immediately starts after that



          awk '!NF { line = NR; next } (NR = line + 1 ) && /^Alignments/{ printf "n%sn",$0; }' file





          share|improve this answer


























          • we are hardcoring the newline in output... it would print the same if we don't have an empty line...

            – msp9011
            1 hour ago











          • @msp9011 : No only if the previous line is empty!

            – Inian
            59 mins ago











          • just check by removing empty line in input file...

            – msp9011
            52 mins ago
















          0















          You can use the below Awk for your problem. Match a empty line and see if the line starting with Alignments immediately starts after that



          awk '!NF { line = NR; next } (NR = line + 1 ) && /^Alignments/{ printf "n%sn",$0; }' file





          share|improve this answer


























          • we are hardcoring the newline in output... it would print the same if we don't have an empty line...

            – msp9011
            1 hour ago











          • @msp9011 : No only if the previous line is empty!

            – Inian
            59 mins ago











          • just check by removing empty line in input file...

            – msp9011
            52 mins ago














          0














          0










          0









          You can use the below Awk for your problem. Match a empty line and see if the line starting with Alignments immediately starts after that



          awk '!NF { line = NR; next } (NR = line + 1 ) && /^Alignments/{ printf "n%sn",$0; }' file





          share|improve this answer













          You can use the below Awk for your problem. Match a empty line and see if the line starting with Alignments immediately starts after that



          awk '!NF { line = NR; next } (NR = line + 1 ) && /^Alignments/{ printf "n%sn",$0; }' file






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 1 hour ago









          InianInian

          7,11017 silver badges36 bronze badges




          7,11017 silver badges36 bronze badges
















          • we are hardcoring the newline in output... it would print the same if we don't have an empty line...

            – msp9011
            1 hour ago











          • @msp9011 : No only if the previous line is empty!

            – Inian
            59 mins ago











          • just check by removing empty line in input file...

            – msp9011
            52 mins ago



















          • we are hardcoring the newline in output... it would print the same if we don't have an empty line...

            – msp9011
            1 hour ago











          • @msp9011 : No only if the previous line is empty!

            – Inian
            59 mins ago











          • just check by removing empty line in input file...

            – msp9011
            52 mins ago

















          we are hardcoring the newline in output... it would print the same if we don't have an empty line...

          – msp9011
          1 hour ago





          we are hardcoring the newline in output... it would print the same if we don't have an empty line...

          – msp9011
          1 hour ago













          @msp9011 : No only if the previous line is empty!

          – Inian
          59 mins ago





          @msp9011 : No only if the previous line is empty!

          – Inian
          59 mins ago













          just check by removing empty line in input file...

          – msp9011
          52 mins ago





          just check by removing empty line in input file...

          – msp9011
          52 mins ago













          0















          Why don't we use grep? :



          grep -A1 "^$" file | grep -B1 'Alignments' | grep -v -- "^--$"





          share|improve this answer






























            0















            Why don't we use grep? :



            grep -A1 "^$" file | grep -B1 'Alignments' | grep -v -- "^--$"





            share|improve this answer




























              0














              0










              0









              Why don't we use grep? :



              grep -A1 "^$" file | grep -B1 'Alignments' | grep -v -- "^--$"





              share|improve this answer













              Why don't we use grep? :



              grep -A1 "^$" file | grep -B1 'Alignments' | grep -v -- "^--$"






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 1 hour ago









              msp9011msp9011

              5,6795 gold badges43 silver badges69 bronze badges




              5,6795 gold badges43 silver badges69 bronze badges


























                  0















                  Sed excels in such tasks. First we stick the next line to the current provided the current is empty. Then interrogate and print upon meeting the criterion set.



                  $ sed -ne '
                  /./!N
                  /^nAlignments/p
                  ' file.txt





                  share|improve this answer






























                    0















                    Sed excels in such tasks. First we stick the next line to the current provided the current is empty. Then interrogate and print upon meeting the criterion set.



                    $ sed -ne '
                    /./!N
                    /^nAlignments/p
                    ' file.txt





                    share|improve this answer




























                      0














                      0










                      0









                      Sed excels in such tasks. First we stick the next line to the current provided the current is empty. Then interrogate and print upon meeting the criterion set.



                      $ sed -ne '
                      /./!N
                      /^nAlignments/p
                      ' file.txt





                      share|improve this answer













                      Sed excels in such tasks. First we stick the next line to the current provided the current is empty. Then interrogate and print upon meeting the criterion set.



                      $ sed -ne '
                      /./!N
                      /^nAlignments/p
                      ' file.txt






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 36 mins ago









                      Rakesh SharmaRakesh Sharma

                      1761 silver badge2 bronze badges




                      1761 silver badge2 bronze badges


























                          0















                          $ awk '/^$|^Alignments/' input.txt | uniq

                          Alignments 747883433 76303046

                          Alignments 838333186 82982133


                          the uniq makes sure there will be no more than one blank line before, after, or between any Alignments lines.



                          grep could be used instead. or sed -n. or perl -n. e.g.



                          $ grep -E '^$|Alignments' input.txt | uniq





                          share|improve this answer






























                            0















                            $ awk '/^$|^Alignments/' input.txt | uniq

                            Alignments 747883433 76303046

                            Alignments 838333186 82982133


                            the uniq makes sure there will be no more than one blank line before, after, or between any Alignments lines.



                            grep could be used instead. or sed -n. or perl -n. e.g.



                            $ grep -E '^$|Alignments' input.txt | uniq





                            share|improve this answer




























                              0














                              0










                              0









                              $ awk '/^$|^Alignments/' input.txt | uniq

                              Alignments 747883433 76303046

                              Alignments 838333186 82982133


                              the uniq makes sure there will be no more than one blank line before, after, or between any Alignments lines.



                              grep could be used instead. or sed -n. or perl -n. e.g.



                              $ grep -E '^$|Alignments' input.txt | uniq





                              share|improve this answer













                              $ awk '/^$|^Alignments/' input.txt | uniq

                              Alignments 747883433 76303046

                              Alignments 838333186 82982133


                              the uniq makes sure there will be no more than one blank line before, after, or between any Alignments lines.



                              grep could be used instead. or sed -n. or perl -n. e.g.



                              $ grep -E '^$|Alignments' input.txt | uniq






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 22 mins ago









                              cascas

                              42.3k4 gold badges62 silver badges112 bronze badges




                              42.3k4 gold badges62 silver badges112 bronze badges


























                                  0















                                  Using GNU awk:



                                  awk -v RS='nAlignments[ 0-9]*' '{print RT}' file


                                  The record separtor RS is set to the expected match and is printed for every record using RT (record terminator).






                                  share|improve this answer






























                                    0















                                    Using GNU awk:



                                    awk -v RS='nAlignments[ 0-9]*' '{print RT}' file


                                    The record separtor RS is set to the expected match and is printed for every record using RT (record terminator).






                                    share|improve this answer




























                                      0














                                      0










                                      0









                                      Using GNU awk:



                                      awk -v RS='nAlignments[ 0-9]*' '{print RT}' file


                                      The record separtor RS is set to the expected match and is printed for every record using RT (record terminator).






                                      share|improve this answer













                                      Using GNU awk:



                                      awk -v RS='nAlignments[ 0-9]*' '{print RT}' file


                                      The record separtor RS is set to the expected match and is printed for every record using RT (record terminator).







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered 13 mins ago









                                      olivoliv

                                      1,9465 silver badges13 bronze badges




                                      1,9465 silver badges13 bronze badges

























                                          giannkas is a new contributor. Be nice, and check out our Code of Conduct.










                                          draft saved

                                          draft discarded


















                                          giannkas is a new contributor. Be nice, and check out our Code of Conduct.













                                          giannkas is a new contributor. Be nice, and check out our Code of Conduct.












                                          giannkas is a new contributor. Be nice, and check out our Code of Conduct.
















                                          Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f538190%2fmatch-blank-lines-before-a-word-awk%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                                          Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                                          Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...