awk: print only lines that come after specific guard line patternFind files with a specific 2-line pattern using awkAwk command to print all the lines except the last three linesdiffrence between two files of different row numbersAWK print from pattern to patternawk: match pattern that contain only forward slashgrepping patterns in a json fileHow to delete all the lines that match specific conditionDelete lines that come after a line with a specific pattern in ShellParsing Text File By Column PatternAWK split file on pattern and name new files after string in first line after pattern

How to participate in group conversations as an outsider?

One of my friends deposited £42 into my account that he had borrowed previously. Will it affect my UK visa application?

My boss copied all his files from his cell phone - with some inappropriate content - to a public folder on our company. Should I delete it?

Where is the Windows license key on win 10?

How to set up a "Forced choice" for players in a game?

Correct word for "the law does not allow for that"

Schemes/ Mechanisms that could provide one time decryption?

Why doesn't CMD inherit environment variables on this machine?

Resources to study Quantum Algorithms?

Sci-fi novel about robots which stopped humans from doing risky things

Best spot within a human to place redundant heart

Who are social elites?

What is the meaning of "wiped my face with a planet"?

Did Roger Rabbit exist prior to the film "Who Framed Roger Rabbit?"

"I did it this way, it worked, so what I did is correct"

Why only sine waves?

Fishy: An ASCII Programming Language

Can airpod with wrong spelling on the case be original?

How to get past the URL limit error of maximum 4096 characters?

Is this the correct pronunciation of "heinous" in any English-speaking country?

Fired for a policy I didn't know about, as well as another false reason

Cannot find module './src/data' with vue-cli

Computer power supplies usually have higher efficiency on 230V than on 115V. Why?

Block on an inclined plane. 1 solution or 3?



awk: print only lines that come after specific guard line pattern


Find files with a specific 2-line pattern using awkAwk command to print all the lines except the last three linesdiffrence between two files of different row numbersAWK print from pattern to patternawk: match pattern that contain only forward slashgrepping patterns in a json fileHow to delete all the lines that match specific conditionDelete lines that come after a line with a specific pattern in ShellParsing Text File By Column PatternAWK split file on pattern and name new files after string in first line after pattern






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









7


















Consider this file:



#!/usr/bin/env bash
cat > example_file.txt <<EOL
|dms,wew,qwqw|
|m|head1|
|3,4,6|
|3e,2,23|
|m|head2|
|xw,12,2123|
|23,d,213|
|23,22q,2skl|
|m|head1|
|dljs,wqpw,2;a|
|dllw,w1p,1q;a|
EOL


Every once in a while, there will be a
guard pattern: a line that starts with:



|m|


(yes, the first line of the file need not
be a guard pattern)
Now I only want to print those lines
for which the last guard pattern was:



|m|head1|


and ignore all other lines.
In other worst, I would like the output to be:



#!/usr/bin/env bash
cat > desired_result_file.txt <<EOL
|m|head1|
|3,4,6|
|3e,2,23|
|m|head1|
|dljs,wqpw,2;a|
|dllw,w1p,1q;a|
EOL









share|improve this question































    7


















    Consider this file:



    #!/usr/bin/env bash
    cat > example_file.txt <<EOL
    |dms,wew,qwqw|
    |m|head1|
    |3,4,6|
    |3e,2,23|
    |m|head2|
    |xw,12,2123|
    |23,d,213|
    |23,22q,2skl|
    |m|head1|
    |dljs,wqpw,2;a|
    |dllw,w1p,1q;a|
    EOL


    Every once in a while, there will be a
    guard pattern: a line that starts with:



    |m|


    (yes, the first line of the file need not
    be a guard pattern)
    Now I only want to print those lines
    for which the last guard pattern was:



    |m|head1|


    and ignore all other lines.
    In other worst, I would like the output to be:



    #!/usr/bin/env bash
    cat > desired_result_file.txt <<EOL
    |m|head1|
    |3,4,6|
    |3e,2,23|
    |m|head1|
    |dljs,wqpw,2;a|
    |dllw,w1p,1q;a|
    EOL









    share|improve this question



























      7













      7









      7


      1






      Consider this file:



      #!/usr/bin/env bash
      cat > example_file.txt <<EOL
      |dms,wew,qwqw|
      |m|head1|
      |3,4,6|
      |3e,2,23|
      |m|head2|
      |xw,12,2123|
      |23,d,213|
      |23,22q,2skl|
      |m|head1|
      |dljs,wqpw,2;a|
      |dllw,w1p,1q;a|
      EOL


      Every once in a while, there will be a
      guard pattern: a line that starts with:



      |m|


      (yes, the first line of the file need not
      be a guard pattern)
      Now I only want to print those lines
      for which the last guard pattern was:



      |m|head1|


      and ignore all other lines.
      In other worst, I would like the output to be:



      #!/usr/bin/env bash
      cat > desired_result_file.txt <<EOL
      |m|head1|
      |3,4,6|
      |3e,2,23|
      |m|head1|
      |dljs,wqpw,2;a|
      |dllw,w1p,1q;a|
      EOL









      share|improve this question














      Consider this file:



      #!/usr/bin/env bash
      cat > example_file.txt <<EOL
      |dms,wew,qwqw|
      |m|head1|
      |3,4,6|
      |3e,2,23|
      |m|head2|
      |xw,12,2123|
      |23,d,213|
      |23,22q,2skl|
      |m|head1|
      |dljs,wqpw,2;a|
      |dllw,w1p,1q;a|
      EOL


      Every once in a while, there will be a
      guard pattern: a line that starts with:



      |m|


      (yes, the first line of the file need not
      be a guard pattern)
      Now I only want to print those lines
      for which the last guard pattern was:



      |m|head1|


      and ignore all other lines.
      In other worst, I would like the output to be:



      #!/usr/bin/env bash
      cat > desired_result_file.txt <<EOL
      |m|head1|
      |3,4,6|
      |3e,2,23|
      |m|head1|
      |dljs,wqpw,2;a|
      |dllw,w1p,1q;a|
      EOL






      command-line awk






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 21 at 23:32









      user2413user2413

      9,10913 gold badges43 silver badges68 bronze badges




      9,10913 gold badges43 silver badges68 bronze badges























          1 Answer
          1






          active

          oldest

          votes


















          7



















          Regex-oriented



          $ awk '/^|m|/ head1 p' example_file.txt 
          |m|head1|
          |3,4,6|
          |3e,2,23|
          |m|head1|
          |dljs,wqpw,2;a|
          |dllw,w1p,1q;a|


          or field-oriented



          $ awk -F'|' '$2 == "m" $3 == "head1" ? p=1 : p=0 p' example_file.txt 
          |m|head1|
          |3,4,6|
          |3e,2,23|
          |m|head1|
          |dljs,wqpw,2;a|
          |dllw,w1p,1q;a|



          p is effectively a print flag.



          Awk programs consist of pattern action pairs, in which action is executed on a record if pattern evaluates true (non-zero). You can omit pattern - in which case action is evaluated for every record - or omit action in which case awk applies the default pattern, which is to print the record: the latter is what's happening here.






          share|improve this answer



























          • How does the variable p work to control printing? I see how it is set, but where is that explained in the awk manual, etc.?

            – Joe
            Sep 26 at 8:03











          • @Joe I have added some explanation of the p

            – steeldriver
            Sep 26 at 11:51












          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "89"
          ;
          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: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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/4.0/"u003ecc by-sa 4.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%2faskubuntu.com%2fquestions%2f1175764%2fawk-print-only-lines-that-come-after-specific-guard-line-pattern%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown


























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          7



















          Regex-oriented



          $ awk '/^|m|/ head1 p' example_file.txt 
          |m|head1|
          |3,4,6|
          |3e,2,23|
          |m|head1|
          |dljs,wqpw,2;a|
          |dllw,w1p,1q;a|


          or field-oriented



          $ awk -F'|' '$2 == "m" $3 == "head1" ? p=1 : p=0 p' example_file.txt 
          |m|head1|
          |3,4,6|
          |3e,2,23|
          |m|head1|
          |dljs,wqpw,2;a|
          |dllw,w1p,1q;a|



          p is effectively a print flag.



          Awk programs consist of pattern action pairs, in which action is executed on a record if pattern evaluates true (non-zero). You can omit pattern - in which case action is evaluated for every record - or omit action in which case awk applies the default pattern, which is to print the record: the latter is what's happening here.






          share|improve this answer



























          • How does the variable p work to control printing? I see how it is set, but where is that explained in the awk manual, etc.?

            – Joe
            Sep 26 at 8:03











          • @Joe I have added some explanation of the p

            – steeldriver
            Sep 26 at 11:51















          7



















          Regex-oriented



          $ awk '/^|m|/ head1 p' example_file.txt 
          |m|head1|
          |3,4,6|
          |3e,2,23|
          |m|head1|
          |dljs,wqpw,2;a|
          |dllw,w1p,1q;a|


          or field-oriented



          $ awk -F'|' '$2 == "m" $3 == "head1" ? p=1 : p=0 p' example_file.txt 
          |m|head1|
          |3,4,6|
          |3e,2,23|
          |m|head1|
          |dljs,wqpw,2;a|
          |dllw,w1p,1q;a|



          p is effectively a print flag.



          Awk programs consist of pattern action pairs, in which action is executed on a record if pattern evaluates true (non-zero). You can omit pattern - in which case action is evaluated for every record - or omit action in which case awk applies the default pattern, which is to print the record: the latter is what's happening here.






          share|improve this answer



























          • How does the variable p work to control printing? I see how it is set, but where is that explained in the awk manual, etc.?

            – Joe
            Sep 26 at 8:03











          • @Joe I have added some explanation of the p

            – steeldriver
            Sep 26 at 11:51













          7















          7











          7









          Regex-oriented



          $ awk '/^|m|/ head1 p' example_file.txt 
          |m|head1|
          |3,4,6|
          |3e,2,23|
          |m|head1|
          |dljs,wqpw,2;a|
          |dllw,w1p,1q;a|


          or field-oriented



          $ awk -F'|' '$2 == "m" $3 == "head1" ? p=1 : p=0 p' example_file.txt 
          |m|head1|
          |3,4,6|
          |3e,2,23|
          |m|head1|
          |dljs,wqpw,2;a|
          |dllw,w1p,1q;a|



          p is effectively a print flag.



          Awk programs consist of pattern action pairs, in which action is executed on a record if pattern evaluates true (non-zero). You can omit pattern - in which case action is evaluated for every record - or omit action in which case awk applies the default pattern, which is to print the record: the latter is what's happening here.






          share|improve this answer
















          Regex-oriented



          $ awk '/^|m|/ head1 p' example_file.txt 
          |m|head1|
          |3,4,6|
          |3e,2,23|
          |m|head1|
          |dljs,wqpw,2;a|
          |dllw,w1p,1q;a|


          or field-oriented



          $ awk -F'|' '$2 == "m" $3 == "head1" ? p=1 : p=0 p' example_file.txt 
          |m|head1|
          |3,4,6|
          |3e,2,23|
          |m|head1|
          |dljs,wqpw,2;a|
          |dllw,w1p,1q;a|



          p is effectively a print flag.



          Awk programs consist of pattern action pairs, in which action is executed on a record if pattern evaluates true (non-zero). You can omit pattern - in which case action is evaluated for every record - or omit action in which case awk applies the default pattern, which is to print the record: the latter is what's happening here.







          share|improve this answer















          share|improve this answer




          share|improve this answer








          edited Sep 26 at 11:50

























          answered Sep 21 at 23:57









          steeldriversteeldriver

          82.5k12 gold badges134 silver badges223 bronze badges




          82.5k12 gold badges134 silver badges223 bronze badges















          • How does the variable p work to control printing? I see how it is set, but where is that explained in the awk manual, etc.?

            – Joe
            Sep 26 at 8:03











          • @Joe I have added some explanation of the p

            – steeldriver
            Sep 26 at 11:51

















          • How does the variable p work to control printing? I see how it is set, but where is that explained in the awk manual, etc.?

            – Joe
            Sep 26 at 8:03











          • @Joe I have added some explanation of the p

            – steeldriver
            Sep 26 at 11:51
















          How does the variable p work to control printing? I see how it is set, but where is that explained in the awk manual, etc.?

          – Joe
          Sep 26 at 8:03





          How does the variable p work to control printing? I see how it is set, but where is that explained in the awk manual, etc.?

          – Joe
          Sep 26 at 8:03













          @Joe I have added some explanation of the p

          – steeldriver
          Sep 26 at 11:51





          @Joe I have added some explanation of the p

          – steeldriver
          Sep 26 at 11:51


















          draft saved

          draft discarded















































          Thanks for contributing an answer to Ask Ubuntu!


          • 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%2faskubuntu.com%2fquestions%2f1175764%2fawk-print-only-lines-that-come-after-specific-guard-line-pattern%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

          Tamil (spriik) Luke uk diar | Nawigatjuun

          Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

          Training a classifier when some of the features are unknownWhy does Gradient Boosting regression predict negative values when there are no negative y-values in my training set?How to improve an existing (trained) classifier?What is effect when I set up some self defined predisctor variables?Why Matlab neural network classification returns decimal values on prediction dataset?Fitting and transforming text data in training, testing, and validation setsHow to quantify the performance of the classifier (multi-class SVM) using the test data?How do I control for some patients providing multiple samples in my training data?Training and Test setTraining a convolutional neural network for image denoising in MatlabShouldn't an autoencoder with #(neurons in hidden layer) = #(neurons in input layer) be “perfect”?