sed: replacing entries in the /etc/fstab











up vote
4
down vote

favorite












I'm in the process of hardening some of our systems. As part of that hardening process, I need to update a few entries in the /etc/fstab to limit the capabilities of some of the various partitions.



With that said, I would like be able to use a sed in-line replace to update the rows. Below is a snippet from the current /etc/fstab:



# /etc/fstab
# Created by anaconda on Wed Feb 21 09:37:23 2018
/dev/mapper/vg1-lv_root / ext4 defaults 1 1
/dev/mapper/vg1-lv_home /home ext4 defaults 1 2
tmpfs /dev/shm tmpfs defaults 0 0


After the sed command is run I would like the file to look like the following:



# /etc/fstab
# Created by anaconda on Wed Feb 21 09:37:23 2018
/dev/mapper/vg1-lv_root / ext4 defaults 1 1
/dev/mapper/vg1-lv_home /home ext4 defaults,nodev 1 2
tmpfs /dev/shm tmpfs defaults 0 0


Basically, I need to add "nodev" to all the rows that are ext[2-4], that aren't the root partition.



The sed command that I put together comes close to doing this, but for whatever reason, I can't get the regex to not match the "/" partition, so it always updates that row also.



sed '/^[^#].*ext[2-4]/s/defaults/defaults,nodev/g' /etc/fstab


I would like to key off of the "/" surrounded by spaces, not the vg1-lv_root. The following works, but I don't like the solution because it's clunky:



sed '/^[^#].*ext[2-4]/s/defaults/defaults,nodev/g' /etc/fstab | sed '/^[^#].*root.*ext[2-4]/s/defaults,nodev/defaults/' > /etc/fstab









share|improve this question









New contributor




Jason is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 3




    Is this on a Linux machine? Can we assume GNU tools?
    – terdon
    yesterday















up vote
4
down vote

favorite












I'm in the process of hardening some of our systems. As part of that hardening process, I need to update a few entries in the /etc/fstab to limit the capabilities of some of the various partitions.



With that said, I would like be able to use a sed in-line replace to update the rows. Below is a snippet from the current /etc/fstab:



# /etc/fstab
# Created by anaconda on Wed Feb 21 09:37:23 2018
/dev/mapper/vg1-lv_root / ext4 defaults 1 1
/dev/mapper/vg1-lv_home /home ext4 defaults 1 2
tmpfs /dev/shm tmpfs defaults 0 0


After the sed command is run I would like the file to look like the following:



# /etc/fstab
# Created by anaconda on Wed Feb 21 09:37:23 2018
/dev/mapper/vg1-lv_root / ext4 defaults 1 1
/dev/mapper/vg1-lv_home /home ext4 defaults,nodev 1 2
tmpfs /dev/shm tmpfs defaults 0 0


Basically, I need to add "nodev" to all the rows that are ext[2-4], that aren't the root partition.



The sed command that I put together comes close to doing this, but for whatever reason, I can't get the regex to not match the "/" partition, so it always updates that row also.



sed '/^[^#].*ext[2-4]/s/defaults/defaults,nodev/g' /etc/fstab


I would like to key off of the "/" surrounded by spaces, not the vg1-lv_root. The following works, but I don't like the solution because it's clunky:



sed '/^[^#].*ext[2-4]/s/defaults/defaults,nodev/g' /etc/fstab | sed '/^[^#].*root.*ext[2-4]/s/defaults,nodev/defaults/' > /etc/fstab









share|improve this question









New contributor




Jason is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 3




    Is this on a Linux machine? Can we assume GNU tools?
    – terdon
    yesterday













up vote
4
down vote

favorite









up vote
4
down vote

favorite











I'm in the process of hardening some of our systems. As part of that hardening process, I need to update a few entries in the /etc/fstab to limit the capabilities of some of the various partitions.



With that said, I would like be able to use a sed in-line replace to update the rows. Below is a snippet from the current /etc/fstab:



# /etc/fstab
# Created by anaconda on Wed Feb 21 09:37:23 2018
/dev/mapper/vg1-lv_root / ext4 defaults 1 1
/dev/mapper/vg1-lv_home /home ext4 defaults 1 2
tmpfs /dev/shm tmpfs defaults 0 0


After the sed command is run I would like the file to look like the following:



# /etc/fstab
# Created by anaconda on Wed Feb 21 09:37:23 2018
/dev/mapper/vg1-lv_root / ext4 defaults 1 1
/dev/mapper/vg1-lv_home /home ext4 defaults,nodev 1 2
tmpfs /dev/shm tmpfs defaults 0 0


Basically, I need to add "nodev" to all the rows that are ext[2-4], that aren't the root partition.



The sed command that I put together comes close to doing this, but for whatever reason, I can't get the regex to not match the "/" partition, so it always updates that row also.



sed '/^[^#].*ext[2-4]/s/defaults/defaults,nodev/g' /etc/fstab


I would like to key off of the "/" surrounded by spaces, not the vg1-lv_root. The following works, but I don't like the solution because it's clunky:



sed '/^[^#].*ext[2-4]/s/defaults/defaults,nodev/g' /etc/fstab | sed '/^[^#].*root.*ext[2-4]/s/defaults,nodev/defaults/' > /etc/fstab









share|improve this question









New contributor




Jason is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I'm in the process of hardening some of our systems. As part of that hardening process, I need to update a few entries in the /etc/fstab to limit the capabilities of some of the various partitions.



With that said, I would like be able to use a sed in-line replace to update the rows. Below is a snippet from the current /etc/fstab:



# /etc/fstab
# Created by anaconda on Wed Feb 21 09:37:23 2018
/dev/mapper/vg1-lv_root / ext4 defaults 1 1
/dev/mapper/vg1-lv_home /home ext4 defaults 1 2
tmpfs /dev/shm tmpfs defaults 0 0


After the sed command is run I would like the file to look like the following:



# /etc/fstab
# Created by anaconda on Wed Feb 21 09:37:23 2018
/dev/mapper/vg1-lv_root / ext4 defaults 1 1
/dev/mapper/vg1-lv_home /home ext4 defaults,nodev 1 2
tmpfs /dev/shm tmpfs defaults 0 0


Basically, I need to add "nodev" to all the rows that are ext[2-4], that aren't the root partition.



The sed command that I put together comes close to doing this, but for whatever reason, I can't get the regex to not match the "/" partition, so it always updates that row also.



sed '/^[^#].*ext[2-4]/s/defaults/defaults,nodev/g' /etc/fstab


I would like to key off of the "/" surrounded by spaces, not the vg1-lv_root. The following works, but I don't like the solution because it's clunky:



sed '/^[^#].*ext[2-4]/s/defaults/defaults,nodev/g' /etc/fstab | sed '/^[^#].*root.*ext[2-4]/s/defaults,nodev/defaults/' > /etc/fstab






text-processing awk sed regular-expression fstab






share|improve this question









New contributor




Jason is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Jason is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 22 hours ago









Rui F Ribeiro

38.1k1475123




38.1k1475123






New contributor




Jason is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









Jason

513




513




New contributor




Jason is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Jason is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Jason is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 3




    Is this on a Linux machine? Can we assume GNU tools?
    – terdon
    yesterday














  • 3




    Is this on a Linux machine? Can we assume GNU tools?
    – terdon
    yesterday








3




3




Is this on a Linux machine? Can we assume GNU tools?
– terdon
yesterday




Is this on a Linux machine? Can we assume GNU tools?
– terdon
yesterday










4 Answers
4






active

oldest

votes

















up vote
5
down vote













You could use awk to add the logic to add the string and column to reformat the final output file. Assuming you have write permissions to the /etc/ and /tmp/ folders



tempfile=$(mktemp /tmp/tmpfile.XXXXXXXX)


This would create the temporary file in the /tmp/ path in which you can write the awk output to and re-direct that back to the original file



awk '$3 ~ "ext[2-4]"{ $4=$4",nodev" }1 ' /etc/fstab | column -t > "$tempfile" && mv -- "$tempfile" /etc/fstab


The column -t part is just redundant and needed to look the output file more readable, rather to make it disordered and clunky.






share|improve this answer



















  • 1




    Thank you for the response. That's a cool way of solving the problem.
    – Jason
    yesterday






  • 3




    I undeleted this since it is a good and helpful answer.
    – terdon
    yesterday










  • Thanks @terdon, I saw OP's comment of having to use sed and without using temporary file
    – Inian
    yesterday








  • 1




    Well, @Jason (and Inian) if you have access to a newish GNU awk (which you do if you're on Linux), you can do gawk -iinplace '$3 ~ "ext[2-4]"{ $4=$4",nodev" }1 ' OFS="t" /etc/fstab. Not as pretty as column, but it uses a tab for output which looks cleaner than a space and it will edit in place.
    – terdon
    yesterday










  • You can avoid the tempfile by using sponge, i.e. awk ... | column ... | sponge /etc/fstab. Sponge will wait until it's read the entirety of stdin ("soaking it up") before writing to the file specified.
    – JoL
    20 hours ago




















up vote
5
down vote













Here's a simpler sed approach:



$ sed -E 's|(s/S+s+ext[2-4]s+defaults)|1,nodev|' fstab 
# /etc/fstab
# Created by anaconda on Wed Feb 21 09:37:23 2018
/dev/mapper/vg1-lv_root / ext4 defaults 1 1
/dev/mapper/vg1-lv_home /home ext4 defaults,nodev 1 2
tmpfs /dev/shm tmpfs defaults 0 0


The trick is to look for whitespace followed by a / and one or more non-whitespace characters (s/S+), then ext[2-4] but only if preceded by whitespace (s+ext[2-4]), more whitespace and defaults. That should only match the cases you are interested in. So if it does match, replace the entire match with itself plus nodev: 1,nodev.



I am not sure how portable this is, however. The -E for extended regular expressions is supported by many sed implementations, but it isn't POSIX. For a more portable approach, you can try the same idea in Perl:



$ perl -pe 's|(s/S+s+ext[2-4]s+defaults)|1,nodev|' fstab 
# /etc/fstab
# Created by anaconda on Wed Feb 21 09:37:23 2018
/dev/mapper/vg1-lv_root / ext4 defaults 1 1
/dev/mapper/vg1-lv_home /home ext4 defaults,nodev 1 2
tmpfs /dev/shm tmpfs defaults 0 0


In both cases, to edit the file in place, use -i:



perl -i -pe 's|(s/S+s+ext[2-4]s+defaults)|1,nodev|' fstab 
sed -E 's|(s/S+s+ext[2-4]s+defaults)|1,nodev|' fstab


Or, for BSD or OSX sed:



sed -i '' -E 's|(s/S+s+ext[2-4]s+defaults)|1,nodev|' fstab 


Note that the above assume that the defaults option will either be the only one or, at least, the last one. They will fail if you have something like nodev,defaults for example.






share|improve this answer






























    up vote
    3
    down vote













    Of course, ten minutes after posting the question, I was finally able to get it to do what I want with the following:



    sed -r '/^[^#].*[ t]+/[^[:space:]].*ext[2-4]/s/defaults/defaults,nodev/g' /etc/fstab


    If someone has a cleaner or more foolproof answer, I'd love to hear it. Thanks!






    share|improve this answer










    New contributor




    Jason is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.


















    • Yeah, the main question was, "what am I doing wrong with my regular expression?". Sorry, I probably should have been more clear in my original post. I would prefer to not create temporary files if I can solve the problem with a sed one-liner.
      – Jason
      yesterday






    • 1




      Why are you assuming t? As far as I know, all fstab needs is whitespace so there's no reason you will always have a tab there.
      – terdon
      yesterday


















    up vote
    0
    down vote













    Excluding matches are difficult to maintain. It's safer to only match the partition you want. And, for only matching one line, the trailing 'g' isn't necessary.



    sed -r 's#(/homes.*defaults)s#1,nodev #' /etc/fstab 





    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',
      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
      });


      }
      });






      Jason is a new contributor. Be nice, and check out our Code of Conduct.










       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f481257%2fsed-replacing-entries-in-the-etc-fstab%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
      5
      down vote













      You could use awk to add the logic to add the string and column to reformat the final output file. Assuming you have write permissions to the /etc/ and /tmp/ folders



      tempfile=$(mktemp /tmp/tmpfile.XXXXXXXX)


      This would create the temporary file in the /tmp/ path in which you can write the awk output to and re-direct that back to the original file



      awk '$3 ~ "ext[2-4]"{ $4=$4",nodev" }1 ' /etc/fstab | column -t > "$tempfile" && mv -- "$tempfile" /etc/fstab


      The column -t part is just redundant and needed to look the output file more readable, rather to make it disordered and clunky.






      share|improve this answer



















      • 1




        Thank you for the response. That's a cool way of solving the problem.
        – Jason
        yesterday






      • 3




        I undeleted this since it is a good and helpful answer.
        – terdon
        yesterday










      • Thanks @terdon, I saw OP's comment of having to use sed and without using temporary file
        – Inian
        yesterday








      • 1




        Well, @Jason (and Inian) if you have access to a newish GNU awk (which you do if you're on Linux), you can do gawk -iinplace '$3 ~ "ext[2-4]"{ $4=$4",nodev" }1 ' OFS="t" /etc/fstab. Not as pretty as column, but it uses a tab for output which looks cleaner than a space and it will edit in place.
        – terdon
        yesterday










      • You can avoid the tempfile by using sponge, i.e. awk ... | column ... | sponge /etc/fstab. Sponge will wait until it's read the entirety of stdin ("soaking it up") before writing to the file specified.
        – JoL
        20 hours ago

















      up vote
      5
      down vote













      You could use awk to add the logic to add the string and column to reformat the final output file. Assuming you have write permissions to the /etc/ and /tmp/ folders



      tempfile=$(mktemp /tmp/tmpfile.XXXXXXXX)


      This would create the temporary file in the /tmp/ path in which you can write the awk output to and re-direct that back to the original file



      awk '$3 ~ "ext[2-4]"{ $4=$4",nodev" }1 ' /etc/fstab | column -t > "$tempfile" && mv -- "$tempfile" /etc/fstab


      The column -t part is just redundant and needed to look the output file more readable, rather to make it disordered and clunky.






      share|improve this answer



















      • 1




        Thank you for the response. That's a cool way of solving the problem.
        – Jason
        yesterday






      • 3




        I undeleted this since it is a good and helpful answer.
        – terdon
        yesterday










      • Thanks @terdon, I saw OP's comment of having to use sed and without using temporary file
        – Inian
        yesterday








      • 1




        Well, @Jason (and Inian) if you have access to a newish GNU awk (which you do if you're on Linux), you can do gawk -iinplace '$3 ~ "ext[2-4]"{ $4=$4",nodev" }1 ' OFS="t" /etc/fstab. Not as pretty as column, but it uses a tab for output which looks cleaner than a space and it will edit in place.
        – terdon
        yesterday










      • You can avoid the tempfile by using sponge, i.e. awk ... | column ... | sponge /etc/fstab. Sponge will wait until it's read the entirety of stdin ("soaking it up") before writing to the file specified.
        – JoL
        20 hours ago















      up vote
      5
      down vote










      up vote
      5
      down vote









      You could use awk to add the logic to add the string and column to reformat the final output file. Assuming you have write permissions to the /etc/ and /tmp/ folders



      tempfile=$(mktemp /tmp/tmpfile.XXXXXXXX)


      This would create the temporary file in the /tmp/ path in which you can write the awk output to and re-direct that back to the original file



      awk '$3 ~ "ext[2-4]"{ $4=$4",nodev" }1 ' /etc/fstab | column -t > "$tempfile" && mv -- "$tempfile" /etc/fstab


      The column -t part is just redundant and needed to look the output file more readable, rather to make it disordered and clunky.






      share|improve this answer














      You could use awk to add the logic to add the string and column to reformat the final output file. Assuming you have write permissions to the /etc/ and /tmp/ folders



      tempfile=$(mktemp /tmp/tmpfile.XXXXXXXX)


      This would create the temporary file in the /tmp/ path in which you can write the awk output to and re-direct that back to the original file



      awk '$3 ~ "ext[2-4]"{ $4=$4",nodev" }1 ' /etc/fstab | column -t > "$tempfile" && mv -- "$tempfile" /etc/fstab


      The column -t part is just redundant and needed to look the output file more readable, rather to make it disordered and clunky.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited yesterday

























      answered yesterday









      Inian

      3,695823




      3,695823








      • 1




        Thank you for the response. That's a cool way of solving the problem.
        – Jason
        yesterday






      • 3




        I undeleted this since it is a good and helpful answer.
        – terdon
        yesterday










      • Thanks @terdon, I saw OP's comment of having to use sed and without using temporary file
        – Inian
        yesterday








      • 1




        Well, @Jason (and Inian) if you have access to a newish GNU awk (which you do if you're on Linux), you can do gawk -iinplace '$3 ~ "ext[2-4]"{ $4=$4",nodev" }1 ' OFS="t" /etc/fstab. Not as pretty as column, but it uses a tab for output which looks cleaner than a space and it will edit in place.
        – terdon
        yesterday










      • You can avoid the tempfile by using sponge, i.e. awk ... | column ... | sponge /etc/fstab. Sponge will wait until it's read the entirety of stdin ("soaking it up") before writing to the file specified.
        – JoL
        20 hours ago
















      • 1




        Thank you for the response. That's a cool way of solving the problem.
        – Jason
        yesterday






      • 3




        I undeleted this since it is a good and helpful answer.
        – terdon
        yesterday










      • Thanks @terdon, I saw OP's comment of having to use sed and without using temporary file
        – Inian
        yesterday








      • 1




        Well, @Jason (and Inian) if you have access to a newish GNU awk (which you do if you're on Linux), you can do gawk -iinplace '$3 ~ "ext[2-4]"{ $4=$4",nodev" }1 ' OFS="t" /etc/fstab. Not as pretty as column, but it uses a tab for output which looks cleaner than a space and it will edit in place.
        – terdon
        yesterday










      • You can avoid the tempfile by using sponge, i.e. awk ... | column ... | sponge /etc/fstab. Sponge will wait until it's read the entirety of stdin ("soaking it up") before writing to the file specified.
        – JoL
        20 hours ago










      1




      1




      Thank you for the response. That's a cool way of solving the problem.
      – Jason
      yesterday




      Thank you for the response. That's a cool way of solving the problem.
      – Jason
      yesterday




      3




      3




      I undeleted this since it is a good and helpful answer.
      – terdon
      yesterday




      I undeleted this since it is a good and helpful answer.
      – terdon
      yesterday












      Thanks @terdon, I saw OP's comment of having to use sed and without using temporary file
      – Inian
      yesterday






      Thanks @terdon, I saw OP's comment of having to use sed and without using temporary file
      – Inian
      yesterday






      1




      1




      Well, @Jason (and Inian) if you have access to a newish GNU awk (which you do if you're on Linux), you can do gawk -iinplace '$3 ~ "ext[2-4]"{ $4=$4",nodev" }1 ' OFS="t" /etc/fstab. Not as pretty as column, but it uses a tab for output which looks cleaner than a space and it will edit in place.
      – terdon
      yesterday




      Well, @Jason (and Inian) if you have access to a newish GNU awk (which you do if you're on Linux), you can do gawk -iinplace '$3 ~ "ext[2-4]"{ $4=$4",nodev" }1 ' OFS="t" /etc/fstab. Not as pretty as column, but it uses a tab for output which looks cleaner than a space and it will edit in place.
      – terdon
      yesterday












      You can avoid the tempfile by using sponge, i.e. awk ... | column ... | sponge /etc/fstab. Sponge will wait until it's read the entirety of stdin ("soaking it up") before writing to the file specified.
      – JoL
      20 hours ago






      You can avoid the tempfile by using sponge, i.e. awk ... | column ... | sponge /etc/fstab. Sponge will wait until it's read the entirety of stdin ("soaking it up") before writing to the file specified.
      – JoL
      20 hours ago














      up vote
      5
      down vote













      Here's a simpler sed approach:



      $ sed -E 's|(s/S+s+ext[2-4]s+defaults)|1,nodev|' fstab 
      # /etc/fstab
      # Created by anaconda on Wed Feb 21 09:37:23 2018
      /dev/mapper/vg1-lv_root / ext4 defaults 1 1
      /dev/mapper/vg1-lv_home /home ext4 defaults,nodev 1 2
      tmpfs /dev/shm tmpfs defaults 0 0


      The trick is to look for whitespace followed by a / and one or more non-whitespace characters (s/S+), then ext[2-4] but only if preceded by whitespace (s+ext[2-4]), more whitespace and defaults. That should only match the cases you are interested in. So if it does match, replace the entire match with itself plus nodev: 1,nodev.



      I am not sure how portable this is, however. The -E for extended regular expressions is supported by many sed implementations, but it isn't POSIX. For a more portable approach, you can try the same idea in Perl:



      $ perl -pe 's|(s/S+s+ext[2-4]s+defaults)|1,nodev|' fstab 
      # /etc/fstab
      # Created by anaconda on Wed Feb 21 09:37:23 2018
      /dev/mapper/vg1-lv_root / ext4 defaults 1 1
      /dev/mapper/vg1-lv_home /home ext4 defaults,nodev 1 2
      tmpfs /dev/shm tmpfs defaults 0 0


      In both cases, to edit the file in place, use -i:



      perl -i -pe 's|(s/S+s+ext[2-4]s+defaults)|1,nodev|' fstab 
      sed -E 's|(s/S+s+ext[2-4]s+defaults)|1,nodev|' fstab


      Or, for BSD or OSX sed:



      sed -i '' -E 's|(s/S+s+ext[2-4]s+defaults)|1,nodev|' fstab 


      Note that the above assume that the defaults option will either be the only one or, at least, the last one. They will fail if you have something like nodev,defaults for example.






      share|improve this answer



























        up vote
        5
        down vote













        Here's a simpler sed approach:



        $ sed -E 's|(s/S+s+ext[2-4]s+defaults)|1,nodev|' fstab 
        # /etc/fstab
        # Created by anaconda on Wed Feb 21 09:37:23 2018
        /dev/mapper/vg1-lv_root / ext4 defaults 1 1
        /dev/mapper/vg1-lv_home /home ext4 defaults,nodev 1 2
        tmpfs /dev/shm tmpfs defaults 0 0


        The trick is to look for whitespace followed by a / and one or more non-whitespace characters (s/S+), then ext[2-4] but only if preceded by whitespace (s+ext[2-4]), more whitespace and defaults. That should only match the cases you are interested in. So if it does match, replace the entire match with itself plus nodev: 1,nodev.



        I am not sure how portable this is, however. The -E for extended regular expressions is supported by many sed implementations, but it isn't POSIX. For a more portable approach, you can try the same idea in Perl:



        $ perl -pe 's|(s/S+s+ext[2-4]s+defaults)|1,nodev|' fstab 
        # /etc/fstab
        # Created by anaconda on Wed Feb 21 09:37:23 2018
        /dev/mapper/vg1-lv_root / ext4 defaults 1 1
        /dev/mapper/vg1-lv_home /home ext4 defaults,nodev 1 2
        tmpfs /dev/shm tmpfs defaults 0 0


        In both cases, to edit the file in place, use -i:



        perl -i -pe 's|(s/S+s+ext[2-4]s+defaults)|1,nodev|' fstab 
        sed -E 's|(s/S+s+ext[2-4]s+defaults)|1,nodev|' fstab


        Or, for BSD or OSX sed:



        sed -i '' -E 's|(s/S+s+ext[2-4]s+defaults)|1,nodev|' fstab 


        Note that the above assume that the defaults option will either be the only one or, at least, the last one. They will fail if you have something like nodev,defaults for example.






        share|improve this answer

























          up vote
          5
          down vote










          up vote
          5
          down vote









          Here's a simpler sed approach:



          $ sed -E 's|(s/S+s+ext[2-4]s+defaults)|1,nodev|' fstab 
          # /etc/fstab
          # Created by anaconda on Wed Feb 21 09:37:23 2018
          /dev/mapper/vg1-lv_root / ext4 defaults 1 1
          /dev/mapper/vg1-lv_home /home ext4 defaults,nodev 1 2
          tmpfs /dev/shm tmpfs defaults 0 0


          The trick is to look for whitespace followed by a / and one or more non-whitespace characters (s/S+), then ext[2-4] but only if preceded by whitespace (s+ext[2-4]), more whitespace and defaults. That should only match the cases you are interested in. So if it does match, replace the entire match with itself plus nodev: 1,nodev.



          I am not sure how portable this is, however. The -E for extended regular expressions is supported by many sed implementations, but it isn't POSIX. For a more portable approach, you can try the same idea in Perl:



          $ perl -pe 's|(s/S+s+ext[2-4]s+defaults)|1,nodev|' fstab 
          # /etc/fstab
          # Created by anaconda on Wed Feb 21 09:37:23 2018
          /dev/mapper/vg1-lv_root / ext4 defaults 1 1
          /dev/mapper/vg1-lv_home /home ext4 defaults,nodev 1 2
          tmpfs /dev/shm tmpfs defaults 0 0


          In both cases, to edit the file in place, use -i:



          perl -i -pe 's|(s/S+s+ext[2-4]s+defaults)|1,nodev|' fstab 
          sed -E 's|(s/S+s+ext[2-4]s+defaults)|1,nodev|' fstab


          Or, for BSD or OSX sed:



          sed -i '' -E 's|(s/S+s+ext[2-4]s+defaults)|1,nodev|' fstab 


          Note that the above assume that the defaults option will either be the only one or, at least, the last one. They will fail if you have something like nodev,defaults for example.






          share|improve this answer














          Here's a simpler sed approach:



          $ sed -E 's|(s/S+s+ext[2-4]s+defaults)|1,nodev|' fstab 
          # /etc/fstab
          # Created by anaconda on Wed Feb 21 09:37:23 2018
          /dev/mapper/vg1-lv_root / ext4 defaults 1 1
          /dev/mapper/vg1-lv_home /home ext4 defaults,nodev 1 2
          tmpfs /dev/shm tmpfs defaults 0 0


          The trick is to look for whitespace followed by a / and one or more non-whitespace characters (s/S+), then ext[2-4] but only if preceded by whitespace (s+ext[2-4]), more whitespace and defaults. That should only match the cases you are interested in. So if it does match, replace the entire match with itself plus nodev: 1,nodev.



          I am not sure how portable this is, however. The -E for extended regular expressions is supported by many sed implementations, but it isn't POSIX. For a more portable approach, you can try the same idea in Perl:



          $ perl -pe 's|(s/S+s+ext[2-4]s+defaults)|1,nodev|' fstab 
          # /etc/fstab
          # Created by anaconda on Wed Feb 21 09:37:23 2018
          /dev/mapper/vg1-lv_root / ext4 defaults 1 1
          /dev/mapper/vg1-lv_home /home ext4 defaults,nodev 1 2
          tmpfs /dev/shm tmpfs defaults 0 0


          In both cases, to edit the file in place, use -i:



          perl -i -pe 's|(s/S+s+ext[2-4]s+defaults)|1,nodev|' fstab 
          sed -E 's|(s/S+s+ext[2-4]s+defaults)|1,nodev|' fstab


          Or, for BSD or OSX sed:



          sed -i '' -E 's|(s/S+s+ext[2-4]s+defaults)|1,nodev|' fstab 


          Note that the above assume that the defaults option will either be the only one or, at least, the last one. They will fail if you have something like nodev,defaults for example.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited yesterday

























          answered yesterday









          terdon

          126k30239417




          126k30239417






















              up vote
              3
              down vote













              Of course, ten minutes after posting the question, I was finally able to get it to do what I want with the following:



              sed -r '/^[^#].*[ t]+/[^[:space:]].*ext[2-4]/s/defaults/defaults,nodev/g' /etc/fstab


              If someone has a cleaner or more foolproof answer, I'd love to hear it. Thanks!






              share|improve this answer










              New contributor




              Jason is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.


















              • Yeah, the main question was, "what am I doing wrong with my regular expression?". Sorry, I probably should have been more clear in my original post. I would prefer to not create temporary files if I can solve the problem with a sed one-liner.
                – Jason
                yesterday






              • 1




                Why are you assuming t? As far as I know, all fstab needs is whitespace so there's no reason you will always have a tab there.
                – terdon
                yesterday















              up vote
              3
              down vote













              Of course, ten minutes after posting the question, I was finally able to get it to do what I want with the following:



              sed -r '/^[^#].*[ t]+/[^[:space:]].*ext[2-4]/s/defaults/defaults,nodev/g' /etc/fstab


              If someone has a cleaner or more foolproof answer, I'd love to hear it. Thanks!






              share|improve this answer










              New contributor




              Jason is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.


















              • Yeah, the main question was, "what am I doing wrong with my regular expression?". Sorry, I probably should have been more clear in my original post. I would prefer to not create temporary files if I can solve the problem with a sed one-liner.
                – Jason
                yesterday






              • 1




                Why are you assuming t? As far as I know, all fstab needs is whitespace so there's no reason you will always have a tab there.
                – terdon
                yesterday













              up vote
              3
              down vote










              up vote
              3
              down vote









              Of course, ten minutes after posting the question, I was finally able to get it to do what I want with the following:



              sed -r '/^[^#].*[ t]+/[^[:space:]].*ext[2-4]/s/defaults/defaults,nodev/g' /etc/fstab


              If someone has a cleaner or more foolproof answer, I'd love to hear it. Thanks!






              share|improve this answer










              New contributor




              Jason is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.









              Of course, ten minutes after posting the question, I was finally able to get it to do what I want with the following:



              sed -r '/^[^#].*[ t]+/[^[:space:]].*ext[2-4]/s/defaults/defaults,nodev/g' /etc/fstab


              If someone has a cleaner or more foolproof answer, I'd love to hear it. Thanks!







              share|improve this answer










              New contributor




              Jason is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.









              share|improve this answer



              share|improve this answer








              edited yesterday









              terdon

              126k30239417




              126k30239417






              New contributor




              Jason is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.









              answered yesterday









              Jason

              513




              513




              New contributor




              Jason is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.





              New contributor





              Jason is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.






              Jason is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.












              • Yeah, the main question was, "what am I doing wrong with my regular expression?". Sorry, I probably should have been more clear in my original post. I would prefer to not create temporary files if I can solve the problem with a sed one-liner.
                – Jason
                yesterday






              • 1




                Why are you assuming t? As far as I know, all fstab needs is whitespace so there's no reason you will always have a tab there.
                – terdon
                yesterday


















              • Yeah, the main question was, "what am I doing wrong with my regular expression?". Sorry, I probably should have been more clear in my original post. I would prefer to not create temporary files if I can solve the problem with a sed one-liner.
                – Jason
                yesterday






              • 1




                Why are you assuming t? As far as I know, all fstab needs is whitespace so there's no reason you will always have a tab there.
                – terdon
                yesterday
















              Yeah, the main question was, "what am I doing wrong with my regular expression?". Sorry, I probably should have been more clear in my original post. I would prefer to not create temporary files if I can solve the problem with a sed one-liner.
              – Jason
              yesterday




              Yeah, the main question was, "what am I doing wrong with my regular expression?". Sorry, I probably should have been more clear in my original post. I would prefer to not create temporary files if I can solve the problem with a sed one-liner.
              – Jason
              yesterday




              1




              1




              Why are you assuming t? As far as I know, all fstab needs is whitespace so there's no reason you will always have a tab there.
              – terdon
              yesterday




              Why are you assuming t? As far as I know, all fstab needs is whitespace so there's no reason you will always have a tab there.
              – terdon
              yesterday










              up vote
              0
              down vote













              Excluding matches are difficult to maintain. It's safer to only match the partition you want. And, for only matching one line, the trailing 'g' isn't necessary.



              sed -r 's#(/homes.*defaults)s#1,nodev #' /etc/fstab 





              share|improve this answer

























                up vote
                0
                down vote













                Excluding matches are difficult to maintain. It's safer to only match the partition you want. And, for only matching one line, the trailing 'g' isn't necessary.



                sed -r 's#(/homes.*defaults)s#1,nodev #' /etc/fstab 





                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Excluding matches are difficult to maintain. It's safer to only match the partition you want. And, for only matching one line, the trailing 'g' isn't necessary.



                  sed -r 's#(/homes.*defaults)s#1,nodev #' /etc/fstab 





                  share|improve this answer












                  Excluding matches are difficult to maintain. It's safer to only match the partition you want. And, for only matching one line, the trailing 'g' isn't necessary.



                  sed -r 's#(/homes.*defaults)s#1,nodev #' /etc/fstab 






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 10 hours ago









                  hellork

                  665




                  665






















                      Jason is a new contributor. Be nice, and check out our Code of Conduct.










                       

                      draft saved


                      draft discarded


















                      Jason is a new contributor. Be nice, and check out our Code of Conduct.













                      Jason is a new contributor. Be nice, and check out our Code of Conduct.












                      Jason is a new contributor. Be nice, and check out our Code of Conduct.















                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f481257%2fsed-replacing-entries-in-the-etc-fstab%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest




















































































                      Popular posts from this blog

                      Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                      ComboBox Display Member on multiple fields

                      Is it possible to collect Nectar points via Trainline?