Simplest way to comment/uncomment certain lines using command line

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
30
down vote

favorite
18












Is there a way to comment/uncomment a shell/config/ruby script using command line?



for example:



$ comment 14-18 bla.conf
$ uncomment 14-18 bla.conf


this would add or remove # sign on bla.conf on line 14 to 18. Normally I use sed, but I must know the contents of those lines and then do a find-replace operation, and that would give a wrong result when the there are more than one needle (and we're only want to replace the N-th one).










share|improve this question



















  • 1




    There is an A to this Q titled: Uncommenting multiple lines of code, specified by line numbers, using vi or vim which shows many methods for doing this using sed, perl, etc. Specifically the A by terdon!
    – slm♦
    May 9 '14 at 3:24















up vote
30
down vote

favorite
18












Is there a way to comment/uncomment a shell/config/ruby script using command line?



for example:



$ comment 14-18 bla.conf
$ uncomment 14-18 bla.conf


this would add or remove # sign on bla.conf on line 14 to 18. Normally I use sed, but I must know the contents of those lines and then do a find-replace operation, and that would give a wrong result when the there are more than one needle (and we're only want to replace the N-th one).










share|improve this question



















  • 1




    There is an A to this Q titled: Uncommenting multiple lines of code, specified by line numbers, using vi or vim which shows many methods for doing this using sed, perl, etc. Specifically the A by terdon!
    – slm♦
    May 9 '14 at 3:24













up vote
30
down vote

favorite
18









up vote
30
down vote

favorite
18






18





Is there a way to comment/uncomment a shell/config/ruby script using command line?



for example:



$ comment 14-18 bla.conf
$ uncomment 14-18 bla.conf


this would add or remove # sign on bla.conf on line 14 to 18. Normally I use sed, but I must know the contents of those lines and then do a find-replace operation, and that would give a wrong result when the there are more than one needle (and we're only want to replace the N-th one).










share|improve this question















Is there a way to comment/uncomment a shell/config/ruby script using command line?



for example:



$ comment 14-18 bla.conf
$ uncomment 14-18 bla.conf


this would add or remove # sign on bla.conf on line 14 to 18. Normally I use sed, but I must know the contents of those lines and then do a find-replace operation, and that would give a wrong result when the there are more than one needle (and we're only want to replace the N-th one).







shell-script text-processing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 9 '14 at 18:02









Braiam

22.8k1972133




22.8k1972133










asked May 9 '14 at 3:01









Kokizzu

2,17663358




2,17663358







  • 1




    There is an A to this Q titled: Uncommenting multiple lines of code, specified by line numbers, using vi or vim which shows many methods for doing this using sed, perl, etc. Specifically the A by terdon!
    – slm♦
    May 9 '14 at 3:24













  • 1




    There is an A to this Q titled: Uncommenting multiple lines of code, specified by line numbers, using vi or vim which shows many methods for doing this using sed, perl, etc. Specifically the A by terdon!
    – slm♦
    May 9 '14 at 3:24








1




1




There is an A to this Q titled: Uncommenting multiple lines of code, specified by line numbers, using vi or vim which shows many methods for doing this using sed, perl, etc. Specifically the A by terdon!
– slm♦
May 9 '14 at 3:24





There is an A to this Q titled: Uncommenting multiple lines of code, specified by line numbers, using vi or vim which shows many methods for doing this using sed, perl, etc. Specifically the A by terdon!
– slm♦
May 9 '14 at 3:24











4 Answers
4






active

oldest

votes

















up vote
37
down vote



accepted










To comment lines 2 through 4 of bla.conf:



sed -i '2,4 s/^/#/' bla.conf


To make the command that you wanted, just put the above into a shell script called comment:



#!/bin/sh
sed -i "$1"' s/^/#/' "$2"


This script is used the same as yours with the exception that the first and last lines are to be separated by a comma rather than a dash. For example:



comment 2,4 bla.conf


An uncomment command can be created analogously.



Advanced feature



sed's line selection is quite powerful. In addition to specifying first and last lines by number, it is also possible to specify them by a regex. So, if you want to command all lines from the one containing foo to the one containing bar, use:



comment '/foo/,/bar/' bla.conf


BSD (OSX) Systems



With BSD sed, the -i option needs an argument even if it is just an empty string. Thus, for example, replace the top command above with:



sed -i '' '2,4 s/^/#/' bla.conf


And, replace the command in the script with:



sed -i '' "$1"' s/^/#/' "$2"





share|improve this answer


















  • 1




    Are the double ' after the -i option intentional? Didn't need them.
    – raphinesse
    Mar 26 '15 at 9:50

















up vote
5
down vote













With GNU sed (to replace the files in place with the -i option):



sed -i '14,18 s/^/#/' bla.conf
sed -i '14,18 s/^##*//' bla.conf





share|improve this answer



























    up vote
    1
    down vote













    You can use Vim in Ex mode:



    ex -sc 'g/^s*[^#]/s/^/#/' -cx bla.conf
    ex -sc 'g/^s*#/s/#//' -cx bla.conf


    1. g global regex


    2. s substitute


    3. x save and close






    share|improve this answer



























      up vote
      0
      down vote













      You can create a bash_file with functions to reuse it in your projects



      #!/bin/bash

      # your target file
      CONFIG=./config.txt

      # comment target
      comment()
      sed -i '' "s/^$1/#$1/" $CONFIG


      # comment target
      uncomment()
      echo $1
      sed -i '' "s/^#$1/$1/" $CONFIG



      # Use it so:
      uncomment enable_uart
      comment arm_freq




      share




















        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',
        convertImagesToLinks: false,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        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%2f128593%2fsimplest-way-to-comment-uncomment-certain-lines-using-command-line%23new-answer', 'question_page');

        );

        Post as a guest






























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        37
        down vote



        accepted










        To comment lines 2 through 4 of bla.conf:



        sed -i '2,4 s/^/#/' bla.conf


        To make the command that you wanted, just put the above into a shell script called comment:



        #!/bin/sh
        sed -i "$1"' s/^/#/' "$2"


        This script is used the same as yours with the exception that the first and last lines are to be separated by a comma rather than a dash. For example:



        comment 2,4 bla.conf


        An uncomment command can be created analogously.



        Advanced feature



        sed's line selection is quite powerful. In addition to specifying first and last lines by number, it is also possible to specify them by a regex. So, if you want to command all lines from the one containing foo to the one containing bar, use:



        comment '/foo/,/bar/' bla.conf


        BSD (OSX) Systems



        With BSD sed, the -i option needs an argument even if it is just an empty string. Thus, for example, replace the top command above with:



        sed -i '' '2,4 s/^/#/' bla.conf


        And, replace the command in the script with:



        sed -i '' "$1"' s/^/#/' "$2"





        share|improve this answer


















        • 1




          Are the double ' after the -i option intentional? Didn't need them.
          – raphinesse
          Mar 26 '15 at 9:50














        up vote
        37
        down vote



        accepted










        To comment lines 2 through 4 of bla.conf:



        sed -i '2,4 s/^/#/' bla.conf


        To make the command that you wanted, just put the above into a shell script called comment:



        #!/bin/sh
        sed -i "$1"' s/^/#/' "$2"


        This script is used the same as yours with the exception that the first and last lines are to be separated by a comma rather than a dash. For example:



        comment 2,4 bla.conf


        An uncomment command can be created analogously.



        Advanced feature



        sed's line selection is quite powerful. In addition to specifying first and last lines by number, it is also possible to specify them by a regex. So, if you want to command all lines from the one containing foo to the one containing bar, use:



        comment '/foo/,/bar/' bla.conf


        BSD (OSX) Systems



        With BSD sed, the -i option needs an argument even if it is just an empty string. Thus, for example, replace the top command above with:



        sed -i '' '2,4 s/^/#/' bla.conf


        And, replace the command in the script with:



        sed -i '' "$1"' s/^/#/' "$2"





        share|improve this answer


















        • 1




          Are the double ' after the -i option intentional? Didn't need them.
          – raphinesse
          Mar 26 '15 at 9:50












        up vote
        37
        down vote



        accepted







        up vote
        37
        down vote



        accepted






        To comment lines 2 through 4 of bla.conf:



        sed -i '2,4 s/^/#/' bla.conf


        To make the command that you wanted, just put the above into a shell script called comment:



        #!/bin/sh
        sed -i "$1"' s/^/#/' "$2"


        This script is used the same as yours with the exception that the first and last lines are to be separated by a comma rather than a dash. For example:



        comment 2,4 bla.conf


        An uncomment command can be created analogously.



        Advanced feature



        sed's line selection is quite powerful. In addition to specifying first and last lines by number, it is also possible to specify them by a regex. So, if you want to command all lines from the one containing foo to the one containing bar, use:



        comment '/foo/,/bar/' bla.conf


        BSD (OSX) Systems



        With BSD sed, the -i option needs an argument even if it is just an empty string. Thus, for example, replace the top command above with:



        sed -i '' '2,4 s/^/#/' bla.conf


        And, replace the command in the script with:



        sed -i '' "$1"' s/^/#/' "$2"





        share|improve this answer














        To comment lines 2 through 4 of bla.conf:



        sed -i '2,4 s/^/#/' bla.conf


        To make the command that you wanted, just put the above into a shell script called comment:



        #!/bin/sh
        sed -i "$1"' s/^/#/' "$2"


        This script is used the same as yours with the exception that the first and last lines are to be separated by a comma rather than a dash. For example:



        comment 2,4 bla.conf


        An uncomment command can be created analogously.



        Advanced feature



        sed's line selection is quite powerful. In addition to specifying first and last lines by number, it is also possible to specify them by a regex. So, if you want to command all lines from the one containing foo to the one containing bar, use:



        comment '/foo/,/bar/' bla.conf


        BSD (OSX) Systems



        With BSD sed, the -i option needs an argument even if it is just an empty string. Thus, for example, replace the top command above with:



        sed -i '' '2,4 s/^/#/' bla.conf


        And, replace the command in the script with:



        sed -i '' "$1"' s/^/#/' "$2"






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 9 '15 at 20:18

























        answered May 9 '14 at 3:04









        John1024

        44.9k4101117




        44.9k4101117







        • 1




          Are the double ' after the -i option intentional? Didn't need them.
          – raphinesse
          Mar 26 '15 at 9:50












        • 1




          Are the double ' after the -i option intentional? Didn't need them.
          – raphinesse
          Mar 26 '15 at 9:50







        1




        1




        Are the double ' after the -i option intentional? Didn't need them.
        – raphinesse
        Mar 26 '15 at 9:50




        Are the double ' after the -i option intentional? Didn't need them.
        – raphinesse
        Mar 26 '15 at 9:50












        up vote
        5
        down vote













        With GNU sed (to replace the files in place with the -i option):



        sed -i '14,18 s/^/#/' bla.conf
        sed -i '14,18 s/^##*//' bla.conf





        share|improve this answer
























          up vote
          5
          down vote













          With GNU sed (to replace the files in place with the -i option):



          sed -i '14,18 s/^/#/' bla.conf
          sed -i '14,18 s/^##*//' bla.conf





          share|improve this answer






















            up vote
            5
            down vote










            up vote
            5
            down vote









            With GNU sed (to replace the files in place with the -i option):



            sed -i '14,18 s/^/#/' bla.conf
            sed -i '14,18 s/^##*//' bla.conf





            share|improve this answer












            With GNU sed (to replace the files in place with the -i option):



            sed -i '14,18 s/^/#/' bla.conf
            sed -i '14,18 s/^##*//' bla.conf






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered May 9 '14 at 3:04









            Gilles

            517k12210311558




            517k12210311558




















                up vote
                1
                down vote













                You can use Vim in Ex mode:



                ex -sc 'g/^s*[^#]/s/^/#/' -cx bla.conf
                ex -sc 'g/^s*#/s/#//' -cx bla.conf


                1. g global regex


                2. s substitute


                3. x save and close






                share|improve this answer
























                  up vote
                  1
                  down vote













                  You can use Vim in Ex mode:



                  ex -sc 'g/^s*[^#]/s/^/#/' -cx bla.conf
                  ex -sc 'g/^s*#/s/#//' -cx bla.conf


                  1. g global regex


                  2. s substitute


                  3. x save and close






                  share|improve this answer






















                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    You can use Vim in Ex mode:



                    ex -sc 'g/^s*[^#]/s/^/#/' -cx bla.conf
                    ex -sc 'g/^s*#/s/#//' -cx bla.conf


                    1. g global regex


                    2. s substitute


                    3. x save and close






                    share|improve this answer












                    You can use Vim in Ex mode:



                    ex -sc 'g/^s*[^#]/s/^/#/' -cx bla.conf
                    ex -sc 'g/^s*#/s/#//' -cx bla.conf


                    1. g global regex


                    2. s substitute


                    3. x save and close







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Apr 17 '16 at 5:40









                    Steven Penny

                    2,41221635




                    2,41221635




















                        up vote
                        0
                        down vote













                        You can create a bash_file with functions to reuse it in your projects



                        #!/bin/bash

                        # your target file
                        CONFIG=./config.txt

                        # comment target
                        comment()
                        sed -i '' "s/^$1/#$1/" $CONFIG


                        # comment target
                        uncomment()
                        echo $1
                        sed -i '' "s/^#$1/$1/" $CONFIG



                        # Use it so:
                        uncomment enable_uart
                        comment arm_freq




                        share
























                          up vote
                          0
                          down vote













                          You can create a bash_file with functions to reuse it in your projects



                          #!/bin/bash

                          # your target file
                          CONFIG=./config.txt

                          # comment target
                          comment()
                          sed -i '' "s/^$1/#$1/" $CONFIG


                          # comment target
                          uncomment()
                          echo $1
                          sed -i '' "s/^#$1/$1/" $CONFIG



                          # Use it so:
                          uncomment enable_uart
                          comment arm_freq




                          share






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            You can create a bash_file with functions to reuse it in your projects



                            #!/bin/bash

                            # your target file
                            CONFIG=./config.txt

                            # comment target
                            comment()
                            sed -i '' "s/^$1/#$1/" $CONFIG


                            # comment target
                            uncomment()
                            echo $1
                            sed -i '' "s/^#$1/$1/" $CONFIG



                            # Use it so:
                            uncomment enable_uart
                            comment arm_freq




                            share












                            You can create a bash_file with functions to reuse it in your projects



                            #!/bin/bash

                            # your target file
                            CONFIG=./config.txt

                            # comment target
                            comment()
                            sed -i '' "s/^$1/#$1/" $CONFIG


                            # comment target
                            uncomment()
                            echo $1
                            sed -i '' "s/^#$1/$1/" $CONFIG



                            # Use it so:
                            uncomment enable_uart
                            comment arm_freq





                            share











                            share


                            share










                            answered 1 min ago









                            Jan

                            1031




                            1031



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f128593%2fsimplest-way-to-comment-uncomment-certain-lines-using-command-line%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Popular posts from this blog

                                Peggy Mitchell

                                Palaiologos

                                The Forum (Inglewood, California)