sed delete all the words before a matchHow to count the number of words and print the lines that match exactly a given pattern?sed lines after match and before next matchUsing sed to replace wordsHow to delete all occurrences of a list of words from a text file?How to match a pattern in lines before another pattern matchRemove all words before a specific pattern, after another patternDelete n lines after pattern and m lines before patternhow to add words before line on all the scripts in current foldersed: delete all lines before matching one, including this one

Getting data from Seagate ST-238R drive

What does Ȝecyndbēc mean?

What are pitfalls I should be aware of with NPC/PC romantic relationships?

How do people create difficult, recreational problems (e.g. like those found in competitions such as the IMO)?

Find difference between dates in Excel

Doing chemistry under water?

Why didn't Abraham ask the single best question?

What color is a wolf's coat?

How to quantify Code Quality

Numeric example to understand the effect of option gamma

Paint Exterior Door that is in Sun

What to do if caught in a physical pentest?

Does the German President's apology for WWII reflect the views of the people of Germany?

What makes an airport "international"?

Does every truth have to be provable based on evidence?

When "this" is captured by a lambda, does it have to be used explicitly?

Are the expansion number tokens in 5-6 players expansion different from the basic Catan?

LACPDU packets to pass through unmanaged switch

Get verbatim output of 'curl wttr.in' in org src code

Expand a recursive pattern

Are all LTI systems invertible? If not, what is a good counterexample?

Does a lich die if its phylactery is destroyed, or can it simply not rejuvenate anymore?

How should I say "where", as in a mathematical definitions?

Is camera at risk of condensation in cold temperatures if never removed from bag?



sed delete all the words before a match


How to count the number of words and print the lines that match exactly a given pattern?sed lines after match and before next matchUsing sed to replace wordsHow to delete all occurrences of a list of words from a text file?How to match a pattern in lines before another pattern matchRemove all words before a specific pattern, after another patternDelete n lines after pattern and m lines before patternhow to add words before line on all the scripts in current foldersed: delete all lines before matching one, including this one






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









6

















I want to delete all the words before a pattern for example: I want to delete all the words before STAC.



Input:



asd
asdd
asddd
STAC
asd
as


Output:



STAC
asd
as


I have this code sed -ni "s/^.*STAC//d" myfile










share|improve this question




























  • If a line has that word it doesn't matter, you just have to eliminate everything that is before that precise word, that is, STAC no HAYSTACK or any combination. Just STAC

    – nicg
    Aug 9 at 14:48











  • thanks for the comment. I just modified the input

    – nicg
    Aug 9 at 14:52











  • awk '/^STAC$/,0', awk '$0=="STAC",0', grep -A100000 '^STAC$'

    – mosvy
    Aug 10 at 0:20


















6

















I want to delete all the words before a pattern for example: I want to delete all the words before STAC.



Input:



asd
asdd
asddd
STAC
asd
as


Output:



STAC
asd
as


I have this code sed -ni "s/^.*STAC//d" myfile










share|improve this question




























  • If a line has that word it doesn't matter, you just have to eliminate everything that is before that precise word, that is, STAC no HAYSTACK or any combination. Just STAC

    – nicg
    Aug 9 at 14:48











  • thanks for the comment. I just modified the input

    – nicg
    Aug 9 at 14:52











  • awk '/^STAC$/,0', awk '$0=="STAC",0', grep -A100000 '^STAC$'

    – mosvy
    Aug 10 at 0:20














6












6








6


1






I want to delete all the words before a pattern for example: I want to delete all the words before STAC.



Input:



asd
asdd
asddd
STAC
asd
as


Output:



STAC
asd
as


I have this code sed -ni "s/^.*STAC//d" myfile










share|improve this question

















I want to delete all the words before a pattern for example: I want to delete all the words before STAC.



Input:



asd
asdd
asddd
STAC
asd
as


Output:



STAC
asd
as


I have this code sed -ni "s/^.*STAC//d" myfile







text-processing sed






share|improve this question
















share|improve this question













share|improve this question




share|improve this question








edited Aug 9 at 14:51







nicg

















asked Aug 9 at 14:41









nicgnicg

313 bronze badges




313 bronze badges















  • If a line has that word it doesn't matter, you just have to eliminate everything that is before that precise word, that is, STAC no HAYSTACK or any combination. Just STAC

    – nicg
    Aug 9 at 14:48











  • thanks for the comment. I just modified the input

    – nicg
    Aug 9 at 14:52











  • awk '/^STAC$/,0', awk '$0=="STAC",0', grep -A100000 '^STAC$'

    – mosvy
    Aug 10 at 0:20


















  • If a line has that word it doesn't matter, you just have to eliminate everything that is before that precise word, that is, STAC no HAYSTACK or any combination. Just STAC

    – nicg
    Aug 9 at 14:48











  • thanks for the comment. I just modified the input

    – nicg
    Aug 9 at 14:52











  • awk '/^STAC$/,0', awk '$0=="STAC",0', grep -A100000 '^STAC$'

    – mosvy
    Aug 10 at 0:20

















If a line has that word it doesn't matter, you just have to eliminate everything that is before that precise word, that is, STAC no HAYSTACK or any combination. Just STAC

– nicg
Aug 9 at 14:48





If a line has that word it doesn't matter, you just have to eliminate everything that is before that precise word, that is, STAC no HAYSTACK or any combination. Just STAC

– nicg
Aug 9 at 14:48













thanks for the comment. I just modified the input

– nicg
Aug 9 at 14:52





thanks for the comment. I just modified the input

– nicg
Aug 9 at 14:52













awk '/^STAC$/,0', awk '$0=="STAC",0', grep -A100000 '^STAC$'

– mosvy
Aug 10 at 0:20






awk '/^STAC$/,0', awk '$0=="STAC",0', grep -A100000 '^STAC$'

– mosvy
Aug 10 at 0:20











5 Answers
5






active

oldest

votes


















13


















sed works linewise, that's why your try will not work.



So how to do it with sed? Define an address range, starting from the STAC line (/^STAC$/) to the end of the file ($). Those should be printed, so everything else (!) should get deleted:



sed -i '/^STAC$/,$!d' myfile





share|improve this answer





















  • 2





    Also possible: sed -ni '/^STAC$/,$p' myfile

    – filbranden
    Aug 9 at 16:17






  • 1





    This is for GNU sed only.

    – Christopher
    Aug 9 at 17:03












  • @Christopher Indeed, the -i option requires an argument (extension) for BSD sed and is not part of the standard. While the OP obviously uses GNU sed, your remark is valuable for future readers, so thank you!

    – Philippos
    Aug 9 at 17:16


















9


















An awk variant which prints all lines after the match (including the match):



$ awk '/^STAC$/ out=1 out' file
STAC
asd
as


This matches the line that only contains the string STAC and sets out to a non-zero value. For each line, if out is non-zero, print it.



Use $0 == "STAC" instead of /^STAC$/ to do a string comparison instead of a regular expression match.




Slightly more obfuscated but shorter, using the boolean result of the match with the regular expression as an integer (will be 0 for a non-match, and 1 for a match):



awk 'p += /^STAC$/' file


If the result in p is non-zero, which it will be from the point where the regular expression first matches, the current line will be printed.



Use p += ($0 == "STAC") instead of p += /^STAC$/ to do a string comparison instead of a regular expression match.






share|improve this answer



































    7


















    Another option would be to use a scriptable editor like ed:



    printf '%sn' '1,/^STAC/-1 d' 'wq' | ed -s myfile


    This prints two commands to ed:



    • delete lines from 1 through (the line before the one that starts with STAC)

    • write the file back to disk and quit

    The -s option inhibits ed's default printing of the number of bytes read & written.






    share|improve this answer

































      3


















      Using awk:



      awk '/^STAC$/,/$ /' input


      This will print all lines between STAC and anything (including the matching lines)




      Or using a grep that supports the -z option (BSD grep does not):




      Treat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline.




      grep -z 'STAC' input





      share|improve this answer



































        0


















        grep -wn STAC file.txt | cut -d":" -f 1 | xargs -I % sed '1,%d' file.txt



        Get line number of the word, pass it to xargs and use sed to delete.






        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/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%2funix.stackexchange.com%2fquestions%2f534745%2fsed-delete-all-the-words-before-a-match%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









          13


















          sed works linewise, that's why your try will not work.



          So how to do it with sed? Define an address range, starting from the STAC line (/^STAC$/) to the end of the file ($). Those should be printed, so everything else (!) should get deleted:



          sed -i '/^STAC$/,$!d' myfile





          share|improve this answer





















          • 2





            Also possible: sed -ni '/^STAC$/,$p' myfile

            – filbranden
            Aug 9 at 16:17






          • 1





            This is for GNU sed only.

            – Christopher
            Aug 9 at 17:03












          • @Christopher Indeed, the -i option requires an argument (extension) for BSD sed and is not part of the standard. While the OP obviously uses GNU sed, your remark is valuable for future readers, so thank you!

            – Philippos
            Aug 9 at 17:16















          13


















          sed works linewise, that's why your try will not work.



          So how to do it with sed? Define an address range, starting from the STAC line (/^STAC$/) to the end of the file ($). Those should be printed, so everything else (!) should get deleted:



          sed -i '/^STAC$/,$!d' myfile





          share|improve this answer





















          • 2





            Also possible: sed -ni '/^STAC$/,$p' myfile

            – filbranden
            Aug 9 at 16:17






          • 1





            This is for GNU sed only.

            – Christopher
            Aug 9 at 17:03












          • @Christopher Indeed, the -i option requires an argument (extension) for BSD sed and is not part of the standard. While the OP obviously uses GNU sed, your remark is valuable for future readers, so thank you!

            – Philippos
            Aug 9 at 17:16













          13














          13










          13









          sed works linewise, that's why your try will not work.



          So how to do it with sed? Define an address range, starting from the STAC line (/^STAC$/) to the end of the file ($). Those should be printed, so everything else (!) should get deleted:



          sed -i '/^STAC$/,$!d' myfile





          share|improve this answer














          sed works linewise, that's why your try will not work.



          So how to do it with sed? Define an address range, starting from the STAC line (/^STAC$/) to the end of the file ($). Those should be printed, so everything else (!) should get deleted:



          sed -i '/^STAC$/,$!d' myfile






          share|improve this answer













          share|improve this answer




          share|improve this answer










          answered Aug 9 at 15:46









          PhilipposPhilippos

          7,8631 gold badge21 silver badges56 bronze badges




          7,8631 gold badge21 silver badges56 bronze badges










          • 2





            Also possible: sed -ni '/^STAC$/,$p' myfile

            – filbranden
            Aug 9 at 16:17






          • 1





            This is for GNU sed only.

            – Christopher
            Aug 9 at 17:03












          • @Christopher Indeed, the -i option requires an argument (extension) for BSD sed and is not part of the standard. While the OP obviously uses GNU sed, your remark is valuable for future readers, so thank you!

            – Philippos
            Aug 9 at 17:16












          • 2





            Also possible: sed -ni '/^STAC$/,$p' myfile

            – filbranden
            Aug 9 at 16:17






          • 1





            This is for GNU sed only.

            – Christopher
            Aug 9 at 17:03












          • @Christopher Indeed, the -i option requires an argument (extension) for BSD sed and is not part of the standard. While the OP obviously uses GNU sed, your remark is valuable for future readers, so thank you!

            – Philippos
            Aug 9 at 17:16







          2




          2





          Also possible: sed -ni '/^STAC$/,$p' myfile

          – filbranden
          Aug 9 at 16:17





          Also possible: sed -ni '/^STAC$/,$p' myfile

          – filbranden
          Aug 9 at 16:17




          1




          1





          This is for GNU sed only.

          – Christopher
          Aug 9 at 17:03






          This is for GNU sed only.

          – Christopher
          Aug 9 at 17:03














          @Christopher Indeed, the -i option requires an argument (extension) for BSD sed and is not part of the standard. While the OP obviously uses GNU sed, your remark is valuable for future readers, so thank you!

          – Philippos
          Aug 9 at 17:16





          @Christopher Indeed, the -i option requires an argument (extension) for BSD sed and is not part of the standard. While the OP obviously uses GNU sed, your remark is valuable for future readers, so thank you!

          – Philippos
          Aug 9 at 17:16













          9


















          An awk variant which prints all lines after the match (including the match):



          $ awk '/^STAC$/ out=1 out' file
          STAC
          asd
          as


          This matches the line that only contains the string STAC and sets out to a non-zero value. For each line, if out is non-zero, print it.



          Use $0 == "STAC" instead of /^STAC$/ to do a string comparison instead of a regular expression match.




          Slightly more obfuscated but shorter, using the boolean result of the match with the regular expression as an integer (will be 0 for a non-match, and 1 for a match):



          awk 'p += /^STAC$/' file


          If the result in p is non-zero, which it will be from the point where the regular expression first matches, the current line will be printed.



          Use p += ($0 == "STAC") instead of p += /^STAC$/ to do a string comparison instead of a regular expression match.






          share|improve this answer
































            9


















            An awk variant which prints all lines after the match (including the match):



            $ awk '/^STAC$/ out=1 out' file
            STAC
            asd
            as


            This matches the line that only contains the string STAC and sets out to a non-zero value. For each line, if out is non-zero, print it.



            Use $0 == "STAC" instead of /^STAC$/ to do a string comparison instead of a regular expression match.




            Slightly more obfuscated but shorter, using the boolean result of the match with the regular expression as an integer (will be 0 for a non-match, and 1 for a match):



            awk 'p += /^STAC$/' file


            If the result in p is non-zero, which it will be from the point where the regular expression first matches, the current line will be printed.



            Use p += ($0 == "STAC") instead of p += /^STAC$/ to do a string comparison instead of a regular expression match.






            share|improve this answer






























              9














              9










              9









              An awk variant which prints all lines after the match (including the match):



              $ awk '/^STAC$/ out=1 out' file
              STAC
              asd
              as


              This matches the line that only contains the string STAC and sets out to a non-zero value. For each line, if out is non-zero, print it.



              Use $0 == "STAC" instead of /^STAC$/ to do a string comparison instead of a regular expression match.




              Slightly more obfuscated but shorter, using the boolean result of the match with the regular expression as an integer (will be 0 for a non-match, and 1 for a match):



              awk 'p += /^STAC$/' file


              If the result in p is non-zero, which it will be from the point where the regular expression first matches, the current line will be printed.



              Use p += ($0 == "STAC") instead of p += /^STAC$/ to do a string comparison instead of a regular expression match.






              share|improve this answer
















              An awk variant which prints all lines after the match (including the match):



              $ awk '/^STAC$/ out=1 out' file
              STAC
              asd
              as


              This matches the line that only contains the string STAC and sets out to a non-zero value. For each line, if out is non-zero, print it.



              Use $0 == "STAC" instead of /^STAC$/ to do a string comparison instead of a regular expression match.




              Slightly more obfuscated but shorter, using the boolean result of the match with the regular expression as an integer (will be 0 for a non-match, and 1 for a match):



              awk 'p += /^STAC$/' file


              If the result in p is non-zero, which it will be from the point where the regular expression first matches, the current line will be printed.



              Use p += ($0 == "STAC") instead of p += /^STAC$/ to do a string comparison instead of a regular expression match.







              share|improve this answer















              share|improve this answer




              share|improve this answer








              edited Aug 9 at 20:16

























              answered Aug 9 at 16:54









              KusalanandaKusalananda

              171k20 gold badges330 silver badges535 bronze badges




              171k20 gold badges330 silver badges535 bronze badges
























                  7


















                  Another option would be to use a scriptable editor like ed:



                  printf '%sn' '1,/^STAC/-1 d' 'wq' | ed -s myfile


                  This prints two commands to ed:



                  • delete lines from 1 through (the line before the one that starts with STAC)

                  • write the file back to disk and quit

                  The -s option inhibits ed's default printing of the number of bytes read & written.






                  share|improve this answer






























                    7


















                    Another option would be to use a scriptable editor like ed:



                    printf '%sn' '1,/^STAC/-1 d' 'wq' | ed -s myfile


                    This prints two commands to ed:



                    • delete lines from 1 through (the line before the one that starts with STAC)

                    • write the file back to disk and quit

                    The -s option inhibits ed's default printing of the number of bytes read & written.






                    share|improve this answer




























                      7














                      7










                      7









                      Another option would be to use a scriptable editor like ed:



                      printf '%sn' '1,/^STAC/-1 d' 'wq' | ed -s myfile


                      This prints two commands to ed:



                      • delete lines from 1 through (the line before the one that starts with STAC)

                      • write the file back to disk and quit

                      The -s option inhibits ed's default printing of the number of bytes read & written.






                      share|improve this answer














                      Another option would be to use a scriptable editor like ed:



                      printf '%sn' '1,/^STAC/-1 d' 'wq' | ed -s myfile


                      This prints two commands to ed:



                      • delete lines from 1 through (the line before the one that starts with STAC)

                      • write the file back to disk and quit

                      The -s option inhibits ed's default printing of the number of bytes read & written.







                      share|improve this answer













                      share|improve this answer




                      share|improve this answer










                      answered Aug 9 at 16:38









                      Jeff SchallerJeff Schaller

                      51.6k11 gold badges76 silver badges171 bronze badges




                      51.6k11 gold badges76 silver badges171 bronze badges
























                          3


















                          Using awk:



                          awk '/^STAC$/,/$ /' input


                          This will print all lines between STAC and anything (including the matching lines)




                          Or using a grep that supports the -z option (BSD grep does not):




                          Treat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline.




                          grep -z 'STAC' input





                          share|improve this answer
































                            3


















                            Using awk:



                            awk '/^STAC$/,/$ /' input


                            This will print all lines between STAC and anything (including the matching lines)




                            Or using a grep that supports the -z option (BSD grep does not):




                            Treat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline.




                            grep -z 'STAC' input





                            share|improve this answer






























                              3














                              3










                              3









                              Using awk:



                              awk '/^STAC$/,/$ /' input


                              This will print all lines between STAC and anything (including the matching lines)




                              Or using a grep that supports the -z option (BSD grep does not):




                              Treat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline.




                              grep -z 'STAC' input





                              share|improve this answer
















                              Using awk:



                              awk '/^STAC$/,/$ /' input


                              This will print all lines between STAC and anything (including the matching lines)




                              Or using a grep that supports the -z option (BSD grep does not):




                              Treat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline.




                              grep -z 'STAC' input






                              share|improve this answer















                              share|improve this answer




                              share|improve this answer








                              edited Aug 9 at 16:49

























                              answered Aug 9 at 14:53









                              Jesse_bJesse_b

                              20.5k3 gold badges49 silver badges92 bronze badges




                              20.5k3 gold badges49 silver badges92 bronze badges
























                                  0


















                                  grep -wn STAC file.txt | cut -d":" -f 1 | xargs -I % sed '1,%d' file.txt



                                  Get line number of the word, pass it to xargs and use sed to delete.






                                  share|improve this answer






























                                    0


















                                    grep -wn STAC file.txt | cut -d":" -f 1 | xargs -I % sed '1,%d' file.txt



                                    Get line number of the word, pass it to xargs and use sed to delete.






                                    share|improve this answer




























                                      0














                                      0










                                      0









                                      grep -wn STAC file.txt | cut -d":" -f 1 | xargs -I % sed '1,%d' file.txt



                                      Get line number of the word, pass it to xargs and use sed to delete.






                                      share|improve this answer














                                      grep -wn STAC file.txt | cut -d":" -f 1 | xargs -I % sed '1,%d' file.txt



                                      Get line number of the word, pass it to xargs and use sed to delete.







                                      share|improve this answer













                                      share|improve this answer




                                      share|improve this answer










                                      answered Aug 9 at 19:19









                                      Death MetalDeath Metal

                                      893 silver badges13 bronze badges




                                      893 silver badges13 bronze badges































                                          draft saved

                                          draft discarded















































                                          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%2f534745%2fsed-delete-all-the-words-before-a-match%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”?