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
text-processing awk sed regular-expression fstab
New contributor
add a comment |
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
text-processing awk sed regular-expression fstab
New contributor
3
Is this on a Linux machine? Can we assume GNU tools?
– terdon♦
yesterday
add a comment |
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
text-processing awk sed regular-expression fstab
New contributor
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
text-processing awk sed regular-expression fstab
New contributor
New contributor
edited 22 hours ago
Rui F Ribeiro
38.1k1475123
38.1k1475123
New contributor
asked yesterday
Jason
513
513
New contributor
New contributor
3
Is this on a Linux machine? Can we assume GNU tools?
– terdon♦
yesterday
add a comment |
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
add a comment |
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.
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 usesed
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 dogawk -iinplace '$3 ~ "ext[2-4]"{ $4=$4",nodev" }1 ' OFS="t" /etc/fstab
. Not as pretty ascolumn
, 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 usingsponge
, 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
add a comment |
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.
add a comment |
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!
New contributor
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 assumingt
? As far as I know, allfstab
needs is whitespace so there's no reason you will always have a tab there.
– terdon♦
yesterday
add a comment |
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
add a comment |
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.
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 usesed
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 dogawk -iinplace '$3 ~ "ext[2-4]"{ $4=$4",nodev" }1 ' OFS="t" /etc/fstab
. Not as pretty ascolumn
, 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 usingsponge
, 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
add a comment |
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.
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 usesed
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 dogawk -iinplace '$3 ~ "ext[2-4]"{ $4=$4",nodev" }1 ' OFS="t" /etc/fstab
. Not as pretty ascolumn
, 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 usingsponge
, 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
add a comment |
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.
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.
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 usesed
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 dogawk -iinplace '$3 ~ "ext[2-4]"{ $4=$4",nodev" }1 ' OFS="t" /etc/fstab
. Not as pretty ascolumn
, 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 usingsponge
, 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
add a comment |
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 usesed
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 dogawk -iinplace '$3 ~ "ext[2-4]"{ $4=$4",nodev" }1 ' OFS="t" /etc/fstab
. Not as pretty ascolumn
, 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 usingsponge
, 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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited yesterday
answered yesterday
terdon♦
126k30239417
126k30239417
add a comment |
add a comment |
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!
New contributor
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 assumingt
? As far as I know, allfstab
needs is whitespace so there's no reason you will always have a tab there.
– terdon♦
yesterday
add a comment |
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!
New contributor
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 assumingt
? As far as I know, allfstab
needs is whitespace so there's no reason you will always have a tab there.
– terdon♦
yesterday
add a comment |
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!
New contributor
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!
New contributor
edited yesterday
terdon♦
126k30239417
126k30239417
New contributor
answered yesterday
Jason
513
513
New contributor
New contributor
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 assumingt
? As far as I know, allfstab
needs is whitespace so there's no reason you will always have a tab there.
– terdon♦
yesterday
add a comment |
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 assumingt
? As far as I know, allfstab
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered 10 hours ago
hellork
665
665
add a comment |
add a comment |
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.
Jason is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
3
Is this on a Linux machine? Can we assume GNU tools?
– terdon♦
yesterday