How do I comment lines in fstab using sed?

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











up vote
0
down vote

favorite












I want to comment certain lines in fstab using sed command. The following are the lines I need to comment:



172.0.0.1:/export/project/common /nfs/share nfs4 rw,bg,hard,nointr,rsize=131072,wsize=131072,proto=tcp
172.0.0.1:/export/project/share1 /nfs/shares1 nfs4 rw,bg,hard,nointr,rsize=131072,wsize=131072,proto=tcp


I tried using this command but it didn't work:



sed -i '/172.0.0.1:/export/project/common /nfs/share nfs4 rw,bg,hard,nointr,rsize=131072,wsize=131072,proto=tcp /s/^/#/' /etc/fstab_test
sed -i '/172.0.0.1:/export/project/share1 /nfs/shares1 nfs4 rw,bg,hard,nointr,rsize=131072,wsize=131072,proto=tcp /s/^/#/' /etc/fstab_test









share|improve this question



























    up vote
    0
    down vote

    favorite












    I want to comment certain lines in fstab using sed command. The following are the lines I need to comment:



    172.0.0.1:/export/project/common /nfs/share nfs4 rw,bg,hard,nointr,rsize=131072,wsize=131072,proto=tcp
    172.0.0.1:/export/project/share1 /nfs/shares1 nfs4 rw,bg,hard,nointr,rsize=131072,wsize=131072,proto=tcp


    I tried using this command but it didn't work:



    sed -i '/172.0.0.1:/export/project/common /nfs/share nfs4 rw,bg,hard,nointr,rsize=131072,wsize=131072,proto=tcp /s/^/#/' /etc/fstab_test
    sed -i '/172.0.0.1:/export/project/share1 /nfs/shares1 nfs4 rw,bg,hard,nointr,rsize=131072,wsize=131072,proto=tcp /s/^/#/' /etc/fstab_test









    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I want to comment certain lines in fstab using sed command. The following are the lines I need to comment:



      172.0.0.1:/export/project/common /nfs/share nfs4 rw,bg,hard,nointr,rsize=131072,wsize=131072,proto=tcp
      172.0.0.1:/export/project/share1 /nfs/shares1 nfs4 rw,bg,hard,nointr,rsize=131072,wsize=131072,proto=tcp


      I tried using this command but it didn't work:



      sed -i '/172.0.0.1:/export/project/common /nfs/share nfs4 rw,bg,hard,nointr,rsize=131072,wsize=131072,proto=tcp /s/^/#/' /etc/fstab_test
      sed -i '/172.0.0.1:/export/project/share1 /nfs/shares1 nfs4 rw,bg,hard,nointr,rsize=131072,wsize=131072,proto=tcp /s/^/#/' /etc/fstab_test









      share|improve this question















      I want to comment certain lines in fstab using sed command. The following are the lines I need to comment:



      172.0.0.1:/export/project/common /nfs/share nfs4 rw,bg,hard,nointr,rsize=131072,wsize=131072,proto=tcp
      172.0.0.1:/export/project/share1 /nfs/shares1 nfs4 rw,bg,hard,nointr,rsize=131072,wsize=131072,proto=tcp


      I tried using this command but it didn't work:



      sed -i '/172.0.0.1:/export/project/common /nfs/share nfs4 rw,bg,hard,nointr,rsize=131072,wsize=131072,proto=tcp /s/^/#/' /etc/fstab_test
      sed -i '/172.0.0.1:/export/project/share1 /nfs/shares1 nfs4 rw,bg,hard,nointr,rsize=131072,wsize=131072,proto=tcp /s/^/#/' /etc/fstab_test






      linux sed fstab






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 18 at 9:30









      Rui F Ribeiro

      38.2k1475123




      38.2k1475123










      asked Jul 13 '16 at 3:43









      NetMonkey2040

      34




      34




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Try this,



          sed -e '/[/]/common s/^/#/' /etc/fstab
          sed -e '/[/]/share1 s/^/#/' /etc/fstab



          Specifying this /[/]common/ will select only lines that contain /common.



          If this works then replace -e with -i for executing the changing into the file.



          You can do this with awk



          awk '/[/]common/$0="#"$0 1' /etc/fstab >/etc/fstab.tmp && mv /etc/fstab.tmp /etc/fstab 
          awk '/[/]share1/$0="#"$0 1' /etc/fstab >/etc/fstab.tmp && mv /etc/fstab.tmp /etc/fstab


          Specifying this /[/]common/ $0="#"$0 will chose those lines containing /common and place a # at the beginning of the line.






          share|improve this answer






















          • awk work like charm, but i have smalll query. when run awk command. it also comment all other shares which has common in them, is there a way to only comment common of specific project /project1/common
            – NetMonkey2040
            Jul 13 '16 at 4:38










          • you can select something which is unique on those line of fstab file.
            – Mongrel
            Jul 13 '16 at 4:58










          • if the full paths (either NFS remote or local mount point) are the only unique things on a line, then you'll have to use one of them. and remember to escape all /s as /.
            – cas
            Jul 13 '16 at 11:33











          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: 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/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%2funix.stackexchange.com%2fquestions%2f295537%2fhow-do-i-comment-lines-in-fstab-using-sed%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          Try this,



          sed -e '/[/]/common s/^/#/' /etc/fstab
          sed -e '/[/]/share1 s/^/#/' /etc/fstab



          Specifying this /[/]common/ will select only lines that contain /common.



          If this works then replace -e with -i for executing the changing into the file.



          You can do this with awk



          awk '/[/]common/$0="#"$0 1' /etc/fstab >/etc/fstab.tmp && mv /etc/fstab.tmp /etc/fstab 
          awk '/[/]share1/$0="#"$0 1' /etc/fstab >/etc/fstab.tmp && mv /etc/fstab.tmp /etc/fstab


          Specifying this /[/]common/ $0="#"$0 will chose those lines containing /common and place a # at the beginning of the line.






          share|improve this answer






















          • awk work like charm, but i have smalll query. when run awk command. it also comment all other shares which has common in them, is there a way to only comment common of specific project /project1/common
            – NetMonkey2040
            Jul 13 '16 at 4:38










          • you can select something which is unique on those line of fstab file.
            – Mongrel
            Jul 13 '16 at 4:58










          • if the full paths (either NFS remote or local mount point) are the only unique things on a line, then you'll have to use one of them. and remember to escape all /s as /.
            – cas
            Jul 13 '16 at 11:33















          up vote
          1
          down vote



          accepted










          Try this,



          sed -e '/[/]/common s/^/#/' /etc/fstab
          sed -e '/[/]/share1 s/^/#/' /etc/fstab



          Specifying this /[/]common/ will select only lines that contain /common.



          If this works then replace -e with -i for executing the changing into the file.



          You can do this with awk



          awk '/[/]common/$0="#"$0 1' /etc/fstab >/etc/fstab.tmp && mv /etc/fstab.tmp /etc/fstab 
          awk '/[/]share1/$0="#"$0 1' /etc/fstab >/etc/fstab.tmp && mv /etc/fstab.tmp /etc/fstab


          Specifying this /[/]common/ $0="#"$0 will chose those lines containing /common and place a # at the beginning of the line.






          share|improve this answer






















          • awk work like charm, but i have smalll query. when run awk command. it also comment all other shares which has common in them, is there a way to only comment common of specific project /project1/common
            – NetMonkey2040
            Jul 13 '16 at 4:38










          • you can select something which is unique on those line of fstab file.
            – Mongrel
            Jul 13 '16 at 4:58










          • if the full paths (either NFS remote or local mount point) are the only unique things on a line, then you'll have to use one of them. and remember to escape all /s as /.
            – cas
            Jul 13 '16 at 11:33













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          Try this,



          sed -e '/[/]/common s/^/#/' /etc/fstab
          sed -e '/[/]/share1 s/^/#/' /etc/fstab



          Specifying this /[/]common/ will select only lines that contain /common.



          If this works then replace -e with -i for executing the changing into the file.



          You can do this with awk



          awk '/[/]common/$0="#"$0 1' /etc/fstab >/etc/fstab.tmp && mv /etc/fstab.tmp /etc/fstab 
          awk '/[/]share1/$0="#"$0 1' /etc/fstab >/etc/fstab.tmp && mv /etc/fstab.tmp /etc/fstab


          Specifying this /[/]common/ $0="#"$0 will chose those lines containing /common and place a # at the beginning of the line.






          share|improve this answer














          Try this,



          sed -e '/[/]/common s/^/#/' /etc/fstab
          sed -e '/[/]/share1 s/^/#/' /etc/fstab



          Specifying this /[/]common/ will select only lines that contain /common.



          If this works then replace -e with -i for executing the changing into the file.



          You can do this with awk



          awk '/[/]common/$0="#"$0 1' /etc/fstab >/etc/fstab.tmp && mv /etc/fstab.tmp /etc/fstab 
          awk '/[/]share1/$0="#"$0 1' /etc/fstab >/etc/fstab.tmp && mv /etc/fstab.tmp /etc/fstab


          Specifying this /[/]common/ $0="#"$0 will chose those lines containing /common and place a # at the beginning of the line.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jul 13 '16 at 4:11

























          answered Jul 13 '16 at 3:58









          Mongrel

          2,04131344




          2,04131344











          • awk work like charm, but i have smalll query. when run awk command. it also comment all other shares which has common in them, is there a way to only comment common of specific project /project1/common
            – NetMonkey2040
            Jul 13 '16 at 4:38










          • you can select something which is unique on those line of fstab file.
            – Mongrel
            Jul 13 '16 at 4:58










          • if the full paths (either NFS remote or local mount point) are the only unique things on a line, then you'll have to use one of them. and remember to escape all /s as /.
            – cas
            Jul 13 '16 at 11:33

















          • awk work like charm, but i have smalll query. when run awk command. it also comment all other shares which has common in them, is there a way to only comment common of specific project /project1/common
            – NetMonkey2040
            Jul 13 '16 at 4:38










          • you can select something which is unique on those line of fstab file.
            – Mongrel
            Jul 13 '16 at 4:58










          • if the full paths (either NFS remote or local mount point) are the only unique things on a line, then you'll have to use one of them. and remember to escape all /s as /.
            – cas
            Jul 13 '16 at 11:33
















          awk work like charm, but i have smalll query. when run awk command. it also comment all other shares which has common in them, is there a way to only comment common of specific project /project1/common
          – NetMonkey2040
          Jul 13 '16 at 4:38




          awk work like charm, but i have smalll query. when run awk command. it also comment all other shares which has common in them, is there a way to only comment common of specific project /project1/common
          – NetMonkey2040
          Jul 13 '16 at 4:38












          you can select something which is unique on those line of fstab file.
          – Mongrel
          Jul 13 '16 at 4:58




          you can select something which is unique on those line of fstab file.
          – Mongrel
          Jul 13 '16 at 4:58












          if the full paths (either NFS remote or local mount point) are the only unique things on a line, then you'll have to use one of them. and remember to escape all /s as /.
          – cas
          Jul 13 '16 at 11:33





          if the full paths (either NFS remote or local mount point) are the only unique things on a line, then you'll have to use one of them. and remember to escape all /s as /.
          – cas
          Jul 13 '16 at 11:33


















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f295537%2fhow-do-i-comment-lines-in-fstab-using-sed%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

          How to check contact read email or not when send email to Individual?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay