how to remove first N-1 lines up to a given input string












2















I'm trying to remove the first N-1 lines up to the first match of a specified string



I am very new to scripting, but from my googling I believe I have the two commands I need, but don't know how to use them together:



grep -n -m 1 'STRING' file.txt |sed  's/([0-9]*).*/1/'


will give me the line N



sed -e '1,Nd' file.txt > file2.txt


will remove the first N lines from file.txt and place them in file2.txt



am I able to use '1,N-1d'?



Further, how can I get all of the above into one line, as I am executing this from ANSYS and it doesn't carry parameters from line to line



any help would be greatly appreciated



Thank you!










share|improve this question





























    2















    I'm trying to remove the first N-1 lines up to the first match of a specified string



    I am very new to scripting, but from my googling I believe I have the two commands I need, but don't know how to use them together:



    grep -n -m 1 'STRING' file.txt |sed  's/([0-9]*).*/1/'


    will give me the line N



    sed -e '1,Nd' file.txt > file2.txt


    will remove the first N lines from file.txt and place them in file2.txt



    am I able to use '1,N-1d'?



    Further, how can I get all of the above into one line, as I am executing this from ANSYS and it doesn't carry parameters from line to line



    any help would be greatly appreciated



    Thank you!










    share|improve this question



























      2












      2








      2








      I'm trying to remove the first N-1 lines up to the first match of a specified string



      I am very new to scripting, but from my googling I believe I have the two commands I need, but don't know how to use them together:



      grep -n -m 1 'STRING' file.txt |sed  's/([0-9]*).*/1/'


      will give me the line N



      sed -e '1,Nd' file.txt > file2.txt


      will remove the first N lines from file.txt and place them in file2.txt



      am I able to use '1,N-1d'?



      Further, how can I get all of the above into one line, as I am executing this from ANSYS and it doesn't carry parameters from line to line



      any help would be greatly appreciated



      Thank you!










      share|improve this question
















      I'm trying to remove the first N-1 lines up to the first match of a specified string



      I am very new to scripting, but from my googling I believe I have the two commands I need, but don't know how to use them together:



      grep -n -m 1 'STRING' file.txt |sed  's/([0-9]*).*/1/'


      will give me the line N



      sed -e '1,Nd' file.txt > file2.txt


      will remove the first N lines from file.txt and place them in file2.txt



      am I able to use '1,N-1d'?



      Further, how can I get all of the above into one line, as I am executing this from ANSYS and it doesn't carry parameters from line to line



      any help would be greatly appreciated



      Thank you!







      grep sed






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 3 at 15:50









      George Udosen

      20.6k94467




      20.6k94467










      asked Jan 3 at 15:46









      tricha122tricha122

      111




      111






















          2 Answers
          2






          active

          oldest

          votes


















          1














          Sure you can use grep and sed and bash arithmetic expansion, e.g.:



          N=$(grep -n -m1 'STRING' file1.txt | cut -d: -f1)
          sed "1,$(( N - 1 ))d" file1.txt > file2.txt


          You can get the same effect with sed, e.g.:



          sed '/STRING/!d; :a; n; ba' file1.txt > file2.txt





          share|improve this answer































            1














            You could use awk (both the default mawk and the GNU gawk version work for this) to filter your file with a single command:



            awk '/STRING/{y=1} y' file1.txt > file2.txt


            Here, /STRING/{y=1} scans each input line for the regular expression pattern STRING and if it matches, set a variable y to 1. Both the name and value are chosen arbitrarily here, we just use this as a boolean flag to remember if we have seen the pattern you search already or not.



            Then, also for each line, after performing the first check for that line, it evaluates the expression y. If it's a truthy value (like e.g. 1 here; while the variable being not defined yet or if it were 0 that would be falsey), awk performs the default action for that line, because we didn't specify any after the expression, which is to simply print that line.



            That way, the variable gets set when our STRING pattern matches for the first time, and all lines after that, including the matched line itself get printed, those before that not.






            share|improve this answer

























              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/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%2faskubuntu.com%2fquestions%2f1106621%2fhow-to-remove-first-n-1-lines-up-to-a-given-input-string%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              Sure you can use grep and sed and bash arithmetic expansion, e.g.:



              N=$(grep -n -m1 'STRING' file1.txt | cut -d: -f1)
              sed "1,$(( N - 1 ))d" file1.txt > file2.txt


              You can get the same effect with sed, e.g.:



              sed '/STRING/!d; :a; n; ba' file1.txt > file2.txt





              share|improve this answer




























                1














                Sure you can use grep and sed and bash arithmetic expansion, e.g.:



                N=$(grep -n -m1 'STRING' file1.txt | cut -d: -f1)
                sed "1,$(( N - 1 ))d" file1.txt > file2.txt


                You can get the same effect with sed, e.g.:



                sed '/STRING/!d; :a; n; ba' file1.txt > file2.txt





                share|improve this answer


























                  1












                  1








                  1







                  Sure you can use grep and sed and bash arithmetic expansion, e.g.:



                  N=$(grep -n -m1 'STRING' file1.txt | cut -d: -f1)
                  sed "1,$(( N - 1 ))d" file1.txt > file2.txt


                  You can get the same effect with sed, e.g.:



                  sed '/STRING/!d; :a; n; ba' file1.txt > file2.txt





                  share|improve this answer













                  Sure you can use grep and sed and bash arithmetic expansion, e.g.:



                  N=$(grep -n -m1 'STRING' file1.txt | cut -d: -f1)
                  sed "1,$(( N - 1 ))d" file1.txt > file2.txt


                  You can get the same effect with sed, e.g.:



                  sed '/STRING/!d; :a; n; ba' file1.txt > file2.txt






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 3 at 16:14









                  ThorThor

                  2,5231620




                  2,5231620

























                      1














                      You could use awk (both the default mawk and the GNU gawk version work for this) to filter your file with a single command:



                      awk '/STRING/{y=1} y' file1.txt > file2.txt


                      Here, /STRING/{y=1} scans each input line for the regular expression pattern STRING and if it matches, set a variable y to 1. Both the name and value are chosen arbitrarily here, we just use this as a boolean flag to remember if we have seen the pattern you search already or not.



                      Then, also for each line, after performing the first check for that line, it evaluates the expression y. If it's a truthy value (like e.g. 1 here; while the variable being not defined yet or if it were 0 that would be falsey), awk performs the default action for that line, because we didn't specify any after the expression, which is to simply print that line.



                      That way, the variable gets set when our STRING pattern matches for the first time, and all lines after that, including the matched line itself get printed, those before that not.






                      share|improve this answer






























                        1














                        You could use awk (both the default mawk and the GNU gawk version work for this) to filter your file with a single command:



                        awk '/STRING/{y=1} y' file1.txt > file2.txt


                        Here, /STRING/{y=1} scans each input line for the regular expression pattern STRING and if it matches, set a variable y to 1. Both the name and value are chosen arbitrarily here, we just use this as a boolean flag to remember if we have seen the pattern you search already or not.



                        Then, also for each line, after performing the first check for that line, it evaluates the expression y. If it's a truthy value (like e.g. 1 here; while the variable being not defined yet or if it were 0 that would be falsey), awk performs the default action for that line, because we didn't specify any after the expression, which is to simply print that line.



                        That way, the variable gets set when our STRING pattern matches for the first time, and all lines after that, including the matched line itself get printed, those before that not.






                        share|improve this answer




























                          1












                          1








                          1







                          You could use awk (both the default mawk and the GNU gawk version work for this) to filter your file with a single command:



                          awk '/STRING/{y=1} y' file1.txt > file2.txt


                          Here, /STRING/{y=1} scans each input line for the regular expression pattern STRING and if it matches, set a variable y to 1. Both the name and value are chosen arbitrarily here, we just use this as a boolean flag to remember if we have seen the pattern you search already or not.



                          Then, also for each line, after performing the first check for that line, it evaluates the expression y. If it's a truthy value (like e.g. 1 here; while the variable being not defined yet or if it were 0 that would be falsey), awk performs the default action for that line, because we didn't specify any after the expression, which is to simply print that line.



                          That way, the variable gets set when our STRING pattern matches for the first time, and all lines after that, including the matched line itself get printed, those before that not.






                          share|improve this answer















                          You could use awk (both the default mawk and the GNU gawk version work for this) to filter your file with a single command:



                          awk '/STRING/{y=1} y' file1.txt > file2.txt


                          Here, /STRING/{y=1} scans each input line for the regular expression pattern STRING and if it matches, set a variable y to 1. Both the name and value are chosen arbitrarily here, we just use this as a boolean flag to remember if we have seen the pattern you search already or not.



                          Then, also for each line, after performing the first check for that line, it evaluates the expression y. If it's a truthy value (like e.g. 1 here; while the variable being not defined yet or if it were 0 that would be falsey), awk performs the default action for that line, because we didn't specify any after the expression, which is to simply print that line.



                          That way, the variable gets set when our STRING pattern matches for the first time, and all lines after that, including the matched line itself get printed, those before that not.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jan 3 at 19:14

























                          answered Jan 3 at 16:37









                          Byte CommanderByte Commander

                          63.7k26173292




                          63.7k26173292






























                              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%2f1106621%2fhow-to-remove-first-n-1-lines-up-to-a-given-input-string%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

                              Ellipse (mathématiques)

                              Quarter-circle Tiles

                              Mont Emei