sed command to remove first occurrence of a word










1















I want to know how to remove the first occurrence of a word in a file.



Example: I want to remove the admin, from the first occurrence only.



account required pam_opendirectory.so
account sufficient pam_self.so
account required pam_group.so no_warn group=admin,wheel fail_safe
account required pam_group.so no_warn deny group=admin,wheel ruser fail_safe


I have tried:



sudo sed -i.bak "s/admin,//1" /etc/pam.d/screensaver


But that removes both cases, any ideas?










share|improve this question


























    1















    I want to know how to remove the first occurrence of a word in a file.



    Example: I want to remove the admin, from the first occurrence only.



    account required pam_opendirectory.so
    account sufficient pam_self.so
    account required pam_group.so no_warn group=admin,wheel fail_safe
    account required pam_group.so no_warn deny group=admin,wheel ruser fail_safe


    I have tried:



    sudo sed -i.bak "s/admin,//1" /etc/pam.d/screensaver


    But that removes both cases, any ideas?










    share|improve this question
























      1












      1








      1








      I want to know how to remove the first occurrence of a word in a file.



      Example: I want to remove the admin, from the first occurrence only.



      account required pam_opendirectory.so
      account sufficient pam_self.so
      account required pam_group.so no_warn group=admin,wheel fail_safe
      account required pam_group.so no_warn deny group=admin,wheel ruser fail_safe


      I have tried:



      sudo sed -i.bak "s/admin,//1" /etc/pam.d/screensaver


      But that removes both cases, any ideas?










      share|improve this question














      I want to know how to remove the first occurrence of a word in a file.



      Example: I want to remove the admin, from the first occurrence only.



      account required pam_opendirectory.so
      account sufficient pam_self.so
      account required pam_group.so no_warn group=admin,wheel fail_safe
      account required pam_group.so no_warn deny group=admin,wheel ruser fail_safe


      I have tried:



      sudo sed -i.bak "s/admin,//1" /etc/pam.d/screensaver


      But that removes both cases, any ideas?







      macos sed






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 30 '15 at 21:03









      Technic1anTechnic1an

      1,16951317




      1,16951317






















          2 Answers
          2






          active

          oldest

          votes


















          4














          Using sed



          To remove the first occurrence in the file:



          $ sed -e ':a' -e '$!N;ba;' -e 's/admin,//1' file
          account required pam_opendirectory.so
          account sufficient pam_self.so
          account required pam_group.so no_warn group=wheel fail_safe
          account required pam_group.so no_warn deny group=admin,wheel ruser fail_safe


          (The above was tested with GNU sed.)



          How it works




          • -e ':a' -e '$!N;ba;'



            This reads the whole file in at once to the pattern space. (If your file is too large for memory, this is not a good approach.)




          • -e 's/admin,//1'



            This performs the substitution on the first occurrence of admin and only the first occurrence.



          Using BSD (OSX) sed



          Wintermute reports that BSD sed cannot do branch instructions in one-liners and suggests this alternative for changing the file in place:



          sed -i.bak -n '1h; 1!H; $ x; s/admin,//; p; ' file


          This reads the whole file in at once and then does the substitution once the last line has been read.



          In more detail:




          • -n



            By default, sed would print each line. This turns that off.




          • 1h



            This places the first line in the hold buffer.




          • 1!H



            For all subsequent lines, this appends them to the hold buffer.




          • $ x; s/admin,//; p;



            For the last line ($), this exchanges the hold and pattern buffer so that the pattern buffer now has the complete file. s/admin,// does the substitution and p prints the result.



          Using awk



          $ awk '/admin/ && !fsub(/admin,/, ""); f=1 1' file >file.tmp && mv file.tmp file


          This results in:



          account required pam_opendirectory.so
          account sufficient pam_self.so
          account required pam_group.so no_warn group=wheel fail_safe
          account required pam_group.so no_warn deny group=admin,wheel ruser fail_safe


          How it works




          • /admin,/ && !fsub(/admin,/, ""); f=1



            For each line, check to see if it contains the word admin, and if the flag f still has its default value of zero. If so, remove the first occurrence of admin, and set the flag to one.




          • 1



            Print each line. 1 is awk's cryptic shorthand for print $0.







          share|improve this answer

























          • Gives me this error: sed: 2: "s/admin,//1 ": unexpected EOF (pending }'s)

            – Technic1an
            Mar 30 '15 at 21:28







          • 2





            OP is using MacOS X, which comes with BSD sed. With BSD sed, you cannot (or at least I have not found a way) use b instructions in one-liners. Try sed -i.bak -n '1h; 1!H; $ x; s/admin,//; p; ' file (that works essentially the same way but uses a different way to assemble the whole file before matching).

            – Wintermute
            Mar 30 '15 at 21:38







          • 1





            Either works. BSD sed does not accept just -i without an argument, though (but -i '' works). By the way: the reason b instructions don't work in one-liners in BSD sed is that something like ba; }; stuff takes everything after the b until the end of the line as a label name and attempts to jump to a label a; }; stuff. This is very silly because I don't think it is possible with BSD sed to declare labels that have a semicolon in their name, but I have the error message to prove it: sed 'ba; stuff' gives sed: 1: "ba; stuff": undefined label 'a; stuff'.

            – Wintermute
            Mar 30 '15 at 21:52






          • 2





            Chaining commands with ; in BSD sed is fine. Vis-a-vis , the thing to know is that with BSD sed, you need a ; before the } that GNU sed doesn't require (p vs. p;). As for the code: 1h copies the current line to the hold buffer if it is the first. 1!H appends it there if it isn't the first. $ ... does something if the current line is the last (at that point the whole file is in the hold buffer). x swaps hold buffer and pattern space, s/admin,// removes the first admin, from the PS, p prints the PS. The -n option disables auto-printing.

            – Wintermute
            Mar 30 '15 at 22:12






          • 1





            @Technic1an I added an explanation for Wintermute's command and updated the awk command to change the file in-place.

            – John1024
            Mar 30 '15 at 22:19


















          0














          Using sed, without reading the whole file:



          sed 's/admin,//;tl;b;:l;n;bl' file


          This waits for a successful substitution and then goes into a loop of printing the rest of the file.



          • s/admin,//;tl;b;: substitute admin with empty string, if substitution occurs jump to label l otherwise print the line and exit (next line will be processed the same way).


          • :l;n;bl: define label l, read line, jump to l.






          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            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%2fstackoverflow.com%2fquestions%2f29356239%2fsed-command-to-remove-first-occurrence-of-a-word%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









            4














            Using sed



            To remove the first occurrence in the file:



            $ sed -e ':a' -e '$!N;ba;' -e 's/admin,//1' file
            account required pam_opendirectory.so
            account sufficient pam_self.so
            account required pam_group.so no_warn group=wheel fail_safe
            account required pam_group.so no_warn deny group=admin,wheel ruser fail_safe


            (The above was tested with GNU sed.)



            How it works




            • -e ':a' -e '$!N;ba;'



              This reads the whole file in at once to the pattern space. (If your file is too large for memory, this is not a good approach.)




            • -e 's/admin,//1'



              This performs the substitution on the first occurrence of admin and only the first occurrence.



            Using BSD (OSX) sed



            Wintermute reports that BSD sed cannot do branch instructions in one-liners and suggests this alternative for changing the file in place:



            sed -i.bak -n '1h; 1!H; $ x; s/admin,//; p; ' file


            This reads the whole file in at once and then does the substitution once the last line has been read.



            In more detail:




            • -n



              By default, sed would print each line. This turns that off.




            • 1h



              This places the first line in the hold buffer.




            • 1!H



              For all subsequent lines, this appends them to the hold buffer.




            • $ x; s/admin,//; p;



              For the last line ($), this exchanges the hold and pattern buffer so that the pattern buffer now has the complete file. s/admin,// does the substitution and p prints the result.



            Using awk



            $ awk '/admin/ && !fsub(/admin,/, ""); f=1 1' file >file.tmp && mv file.tmp file


            This results in:



            account required pam_opendirectory.so
            account sufficient pam_self.so
            account required pam_group.so no_warn group=wheel fail_safe
            account required pam_group.so no_warn deny group=admin,wheel ruser fail_safe


            How it works




            • /admin,/ && !fsub(/admin,/, ""); f=1



              For each line, check to see if it contains the word admin, and if the flag f still has its default value of zero. If so, remove the first occurrence of admin, and set the flag to one.




            • 1



              Print each line. 1 is awk's cryptic shorthand for print $0.







            share|improve this answer

























            • Gives me this error: sed: 2: "s/admin,//1 ": unexpected EOF (pending }'s)

              – Technic1an
              Mar 30 '15 at 21:28







            • 2





              OP is using MacOS X, which comes with BSD sed. With BSD sed, you cannot (or at least I have not found a way) use b instructions in one-liners. Try sed -i.bak -n '1h; 1!H; $ x; s/admin,//; p; ' file (that works essentially the same way but uses a different way to assemble the whole file before matching).

              – Wintermute
              Mar 30 '15 at 21:38







            • 1





              Either works. BSD sed does not accept just -i without an argument, though (but -i '' works). By the way: the reason b instructions don't work in one-liners in BSD sed is that something like ba; }; stuff takes everything after the b until the end of the line as a label name and attempts to jump to a label a; }; stuff. This is very silly because I don't think it is possible with BSD sed to declare labels that have a semicolon in their name, but I have the error message to prove it: sed 'ba; stuff' gives sed: 1: "ba; stuff": undefined label 'a; stuff'.

              – Wintermute
              Mar 30 '15 at 21:52






            • 2





              Chaining commands with ; in BSD sed is fine. Vis-a-vis , the thing to know is that with BSD sed, you need a ; before the } that GNU sed doesn't require (p vs. p;). As for the code: 1h copies the current line to the hold buffer if it is the first. 1!H appends it there if it isn't the first. $ ... does something if the current line is the last (at that point the whole file is in the hold buffer). x swaps hold buffer and pattern space, s/admin,// removes the first admin, from the PS, p prints the PS. The -n option disables auto-printing.

              – Wintermute
              Mar 30 '15 at 22:12






            • 1





              @Technic1an I added an explanation for Wintermute's command and updated the awk command to change the file in-place.

              – John1024
              Mar 30 '15 at 22:19















            4














            Using sed



            To remove the first occurrence in the file:



            $ sed -e ':a' -e '$!N;ba;' -e 's/admin,//1' file
            account required pam_opendirectory.so
            account sufficient pam_self.so
            account required pam_group.so no_warn group=wheel fail_safe
            account required pam_group.so no_warn deny group=admin,wheel ruser fail_safe


            (The above was tested with GNU sed.)



            How it works




            • -e ':a' -e '$!N;ba;'



              This reads the whole file in at once to the pattern space. (If your file is too large for memory, this is not a good approach.)




            • -e 's/admin,//1'



              This performs the substitution on the first occurrence of admin and only the first occurrence.



            Using BSD (OSX) sed



            Wintermute reports that BSD sed cannot do branch instructions in one-liners and suggests this alternative for changing the file in place:



            sed -i.bak -n '1h; 1!H; $ x; s/admin,//; p; ' file


            This reads the whole file in at once and then does the substitution once the last line has been read.



            In more detail:




            • -n



              By default, sed would print each line. This turns that off.




            • 1h



              This places the first line in the hold buffer.




            • 1!H



              For all subsequent lines, this appends them to the hold buffer.




            • $ x; s/admin,//; p;



              For the last line ($), this exchanges the hold and pattern buffer so that the pattern buffer now has the complete file. s/admin,// does the substitution and p prints the result.



            Using awk



            $ awk '/admin/ && !fsub(/admin,/, ""); f=1 1' file >file.tmp && mv file.tmp file


            This results in:



            account required pam_opendirectory.so
            account sufficient pam_self.so
            account required pam_group.so no_warn group=wheel fail_safe
            account required pam_group.so no_warn deny group=admin,wheel ruser fail_safe


            How it works




            • /admin,/ && !fsub(/admin,/, ""); f=1



              For each line, check to see if it contains the word admin, and if the flag f still has its default value of zero. If so, remove the first occurrence of admin, and set the flag to one.




            • 1



              Print each line. 1 is awk's cryptic shorthand for print $0.







            share|improve this answer

























            • Gives me this error: sed: 2: "s/admin,//1 ": unexpected EOF (pending }'s)

              – Technic1an
              Mar 30 '15 at 21:28







            • 2





              OP is using MacOS X, which comes with BSD sed. With BSD sed, you cannot (or at least I have not found a way) use b instructions in one-liners. Try sed -i.bak -n '1h; 1!H; $ x; s/admin,//; p; ' file (that works essentially the same way but uses a different way to assemble the whole file before matching).

              – Wintermute
              Mar 30 '15 at 21:38







            • 1





              Either works. BSD sed does not accept just -i without an argument, though (but -i '' works). By the way: the reason b instructions don't work in one-liners in BSD sed is that something like ba; }; stuff takes everything after the b until the end of the line as a label name and attempts to jump to a label a; }; stuff. This is very silly because I don't think it is possible with BSD sed to declare labels that have a semicolon in their name, but I have the error message to prove it: sed 'ba; stuff' gives sed: 1: "ba; stuff": undefined label 'a; stuff'.

              – Wintermute
              Mar 30 '15 at 21:52






            • 2





              Chaining commands with ; in BSD sed is fine. Vis-a-vis , the thing to know is that with BSD sed, you need a ; before the } that GNU sed doesn't require (p vs. p;). As for the code: 1h copies the current line to the hold buffer if it is the first. 1!H appends it there if it isn't the first. $ ... does something if the current line is the last (at that point the whole file is in the hold buffer). x swaps hold buffer and pattern space, s/admin,// removes the first admin, from the PS, p prints the PS. The -n option disables auto-printing.

              – Wintermute
              Mar 30 '15 at 22:12






            • 1





              @Technic1an I added an explanation for Wintermute's command and updated the awk command to change the file in-place.

              – John1024
              Mar 30 '15 at 22:19













            4












            4








            4







            Using sed



            To remove the first occurrence in the file:



            $ sed -e ':a' -e '$!N;ba;' -e 's/admin,//1' file
            account required pam_opendirectory.so
            account sufficient pam_self.so
            account required pam_group.so no_warn group=wheel fail_safe
            account required pam_group.so no_warn deny group=admin,wheel ruser fail_safe


            (The above was tested with GNU sed.)



            How it works




            • -e ':a' -e '$!N;ba;'



              This reads the whole file in at once to the pattern space. (If your file is too large for memory, this is not a good approach.)




            • -e 's/admin,//1'



              This performs the substitution on the first occurrence of admin and only the first occurrence.



            Using BSD (OSX) sed



            Wintermute reports that BSD sed cannot do branch instructions in one-liners and suggests this alternative for changing the file in place:



            sed -i.bak -n '1h; 1!H; $ x; s/admin,//; p; ' file


            This reads the whole file in at once and then does the substitution once the last line has been read.



            In more detail:




            • -n



              By default, sed would print each line. This turns that off.




            • 1h



              This places the first line in the hold buffer.




            • 1!H



              For all subsequent lines, this appends them to the hold buffer.




            • $ x; s/admin,//; p;



              For the last line ($), this exchanges the hold and pattern buffer so that the pattern buffer now has the complete file. s/admin,// does the substitution and p prints the result.



            Using awk



            $ awk '/admin/ && !fsub(/admin,/, ""); f=1 1' file >file.tmp && mv file.tmp file


            This results in:



            account required pam_opendirectory.so
            account sufficient pam_self.so
            account required pam_group.so no_warn group=wheel fail_safe
            account required pam_group.so no_warn deny group=admin,wheel ruser fail_safe


            How it works




            • /admin,/ && !fsub(/admin,/, ""); f=1



              For each line, check to see if it contains the word admin, and if the flag f still has its default value of zero. If so, remove the first occurrence of admin, and set the flag to one.




            • 1



              Print each line. 1 is awk's cryptic shorthand for print $0.







            share|improve this answer















            Using sed



            To remove the first occurrence in the file:



            $ sed -e ':a' -e '$!N;ba;' -e 's/admin,//1' file
            account required pam_opendirectory.so
            account sufficient pam_self.so
            account required pam_group.so no_warn group=wheel fail_safe
            account required pam_group.so no_warn deny group=admin,wheel ruser fail_safe


            (The above was tested with GNU sed.)



            How it works




            • -e ':a' -e '$!N;ba;'



              This reads the whole file in at once to the pattern space. (If your file is too large for memory, this is not a good approach.)




            • -e 's/admin,//1'



              This performs the substitution on the first occurrence of admin and only the first occurrence.



            Using BSD (OSX) sed



            Wintermute reports that BSD sed cannot do branch instructions in one-liners and suggests this alternative for changing the file in place:



            sed -i.bak -n '1h; 1!H; $ x; s/admin,//; p; ' file


            This reads the whole file in at once and then does the substitution once the last line has been read.



            In more detail:




            • -n



              By default, sed would print each line. This turns that off.




            • 1h



              This places the first line in the hold buffer.




            • 1!H



              For all subsequent lines, this appends them to the hold buffer.




            • $ x; s/admin,//; p;



              For the last line ($), this exchanges the hold and pattern buffer so that the pattern buffer now has the complete file. s/admin,// does the substitution and p prints the result.



            Using awk



            $ awk '/admin/ && !fsub(/admin,/, ""); f=1 1' file >file.tmp && mv file.tmp file


            This results in:



            account required pam_opendirectory.so
            account sufficient pam_self.so
            account required pam_group.so no_warn group=wheel fail_safe
            account required pam_group.so no_warn deny group=admin,wheel ruser fail_safe


            How it works




            • /admin,/ && !fsub(/admin,/, ""); f=1



              For each line, check to see if it contains the word admin, and if the flag f still has its default value of zero. If so, remove the first occurrence of admin, and set the flag to one.




            • 1



              Print each line. 1 is awk's cryptic shorthand for print $0.








            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 30 '15 at 22:17

























            answered Mar 30 '15 at 21:25









            John1024John1024

            77.2k87198




            77.2k87198












            • Gives me this error: sed: 2: "s/admin,//1 ": unexpected EOF (pending }'s)

              – Technic1an
              Mar 30 '15 at 21:28







            • 2





              OP is using MacOS X, which comes with BSD sed. With BSD sed, you cannot (or at least I have not found a way) use b instructions in one-liners. Try sed -i.bak -n '1h; 1!H; $ x; s/admin,//; p; ' file (that works essentially the same way but uses a different way to assemble the whole file before matching).

              – Wintermute
              Mar 30 '15 at 21:38







            • 1





              Either works. BSD sed does not accept just -i without an argument, though (but -i '' works). By the way: the reason b instructions don't work in one-liners in BSD sed is that something like ba; }; stuff takes everything after the b until the end of the line as a label name and attempts to jump to a label a; }; stuff. This is very silly because I don't think it is possible with BSD sed to declare labels that have a semicolon in their name, but I have the error message to prove it: sed 'ba; stuff' gives sed: 1: "ba; stuff": undefined label 'a; stuff'.

              – Wintermute
              Mar 30 '15 at 21:52






            • 2





              Chaining commands with ; in BSD sed is fine. Vis-a-vis , the thing to know is that with BSD sed, you need a ; before the } that GNU sed doesn't require (p vs. p;). As for the code: 1h copies the current line to the hold buffer if it is the first. 1!H appends it there if it isn't the first. $ ... does something if the current line is the last (at that point the whole file is in the hold buffer). x swaps hold buffer and pattern space, s/admin,// removes the first admin, from the PS, p prints the PS. The -n option disables auto-printing.

              – Wintermute
              Mar 30 '15 at 22:12






            • 1





              @Technic1an I added an explanation for Wintermute's command and updated the awk command to change the file in-place.

              – John1024
              Mar 30 '15 at 22:19

















            • Gives me this error: sed: 2: "s/admin,//1 ": unexpected EOF (pending }'s)

              – Technic1an
              Mar 30 '15 at 21:28







            • 2





              OP is using MacOS X, which comes with BSD sed. With BSD sed, you cannot (or at least I have not found a way) use b instructions in one-liners. Try sed -i.bak -n '1h; 1!H; $ x; s/admin,//; p; ' file (that works essentially the same way but uses a different way to assemble the whole file before matching).

              – Wintermute
              Mar 30 '15 at 21:38







            • 1





              Either works. BSD sed does not accept just -i without an argument, though (but -i '' works). By the way: the reason b instructions don't work in one-liners in BSD sed is that something like ba; }; stuff takes everything after the b until the end of the line as a label name and attempts to jump to a label a; }; stuff. This is very silly because I don't think it is possible with BSD sed to declare labels that have a semicolon in their name, but I have the error message to prove it: sed 'ba; stuff' gives sed: 1: "ba; stuff": undefined label 'a; stuff'.

              – Wintermute
              Mar 30 '15 at 21:52






            • 2





              Chaining commands with ; in BSD sed is fine. Vis-a-vis , the thing to know is that with BSD sed, you need a ; before the } that GNU sed doesn't require (p vs. p;). As for the code: 1h copies the current line to the hold buffer if it is the first. 1!H appends it there if it isn't the first. $ ... does something if the current line is the last (at that point the whole file is in the hold buffer). x swaps hold buffer and pattern space, s/admin,// removes the first admin, from the PS, p prints the PS. The -n option disables auto-printing.

              – Wintermute
              Mar 30 '15 at 22:12






            • 1





              @Technic1an I added an explanation for Wintermute's command and updated the awk command to change the file in-place.

              – John1024
              Mar 30 '15 at 22:19
















            Gives me this error: sed: 2: "s/admin,//1 ": unexpected EOF (pending }'s)

            – Technic1an
            Mar 30 '15 at 21:28






            Gives me this error: sed: 2: "s/admin,//1 ": unexpected EOF (pending }'s)

            – Technic1an
            Mar 30 '15 at 21:28





            2




            2





            OP is using MacOS X, which comes with BSD sed. With BSD sed, you cannot (or at least I have not found a way) use b instructions in one-liners. Try sed -i.bak -n '1h; 1!H; $ x; s/admin,//; p; ' file (that works essentially the same way but uses a different way to assemble the whole file before matching).

            – Wintermute
            Mar 30 '15 at 21:38






            OP is using MacOS X, which comes with BSD sed. With BSD sed, you cannot (or at least I have not found a way) use b instructions in one-liners. Try sed -i.bak -n '1h; 1!H; $ x; s/admin,//; p; ' file (that works essentially the same way but uses a different way to assemble the whole file before matching).

            – Wintermute
            Mar 30 '15 at 21:38





            1




            1





            Either works. BSD sed does not accept just -i without an argument, though (but -i '' works). By the way: the reason b instructions don't work in one-liners in BSD sed is that something like ba; }; stuff takes everything after the b until the end of the line as a label name and attempts to jump to a label a; }; stuff. This is very silly because I don't think it is possible with BSD sed to declare labels that have a semicolon in their name, but I have the error message to prove it: sed 'ba; stuff' gives sed: 1: "ba; stuff": undefined label 'a; stuff'.

            – Wintermute
            Mar 30 '15 at 21:52





            Either works. BSD sed does not accept just -i without an argument, though (but -i '' works). By the way: the reason b instructions don't work in one-liners in BSD sed is that something like ba; }; stuff takes everything after the b until the end of the line as a label name and attempts to jump to a label a; }; stuff. This is very silly because I don't think it is possible with BSD sed to declare labels that have a semicolon in their name, but I have the error message to prove it: sed 'ba; stuff' gives sed: 1: "ba; stuff": undefined label 'a; stuff'.

            – Wintermute
            Mar 30 '15 at 21:52




            2




            2





            Chaining commands with ; in BSD sed is fine. Vis-a-vis , the thing to know is that with BSD sed, you need a ; before the } that GNU sed doesn't require (p vs. p;). As for the code: 1h copies the current line to the hold buffer if it is the first. 1!H appends it there if it isn't the first. $ ... does something if the current line is the last (at that point the whole file is in the hold buffer). x swaps hold buffer and pattern space, s/admin,// removes the first admin, from the PS, p prints the PS. The -n option disables auto-printing.

            – Wintermute
            Mar 30 '15 at 22:12





            Chaining commands with ; in BSD sed is fine. Vis-a-vis , the thing to know is that with BSD sed, you need a ; before the } that GNU sed doesn't require (p vs. p;). As for the code: 1h copies the current line to the hold buffer if it is the first. 1!H appends it there if it isn't the first. $ ... does something if the current line is the last (at that point the whole file is in the hold buffer). x swaps hold buffer and pattern space, s/admin,// removes the first admin, from the PS, p prints the PS. The -n option disables auto-printing.

            – Wintermute
            Mar 30 '15 at 22:12




            1




            1





            @Technic1an I added an explanation for Wintermute's command and updated the awk command to change the file in-place.

            – John1024
            Mar 30 '15 at 22:19





            @Technic1an I added an explanation for Wintermute's command and updated the awk command to change the file in-place.

            – John1024
            Mar 30 '15 at 22:19













            0














            Using sed, without reading the whole file:



            sed 's/admin,//;tl;b;:l;n;bl' file


            This waits for a successful substitution and then goes into a loop of printing the rest of the file.



            • s/admin,//;tl;b;: substitute admin with empty string, if substitution occurs jump to label l otherwise print the line and exit (next line will be processed the same way).


            • :l;n;bl: define label l, read line, jump to l.






            share|improve this answer



























              0














              Using sed, without reading the whole file:



              sed 's/admin,//;tl;b;:l;n;bl' file


              This waits for a successful substitution and then goes into a loop of printing the rest of the file.



              • s/admin,//;tl;b;: substitute admin with empty string, if substitution occurs jump to label l otherwise print the line and exit (next line will be processed the same way).


              • :l;n;bl: define label l, read line, jump to l.






              share|improve this answer

























                0












                0








                0







                Using sed, without reading the whole file:



                sed 's/admin,//;tl;b;:l;n;bl' file


                This waits for a successful substitution and then goes into a loop of printing the rest of the file.



                • s/admin,//;tl;b;: substitute admin with empty string, if substitution occurs jump to label l otherwise print the line and exit (next line will be processed the same way).


                • :l;n;bl: define label l, read line, jump to l.






                share|improve this answer













                Using sed, without reading the whole file:



                sed 's/admin,//;tl;b;:l;n;bl' file


                This waits for a successful substitution and then goes into a loop of printing the rest of the file.



                • s/admin,//;tl;b;: substitute admin with empty string, if substitution occurs jump to label l otherwise print the line and exit (next line will be processed the same way).


                • :l;n;bl: define label l, read line, jump to l.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 '18 at 12:19









                perrealperreal

                72.8k10111140




                72.8k10111140



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • 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%2fstackoverflow.com%2fquestions%2f29356239%2fsed-command-to-remove-first-occurrence-of-a-word%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

                    𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                    Edmonton

                    Crossroads (UK TV series)