I am trying to modify /etc/fstab with sed/awk. Much like in this question, however, the solutions in that question aren’t quite working for me.
I need to add the nodev (and nosuid) option to nfs or nfs4 mount points.
I have working;
awk '$4!~/nodev/&&$3~"nfs"{$4=$4",nodev"}1' OFS="\t" /etc/fstab > /etc/fstab.tmp && mv /etc/fstab.tmp /etc/fstab
awk '$4!~/nosuid/&&$3~"nfs"{$4=$4",nosuid"}1' OFS="\t" /etc/fstab > /etc/fstab.tmp && mv /etc/fstab.tmp /etc/fstab
However, it obliterates existing white space and replaces with a single tab. My question is: How can I make this change in a less intrusive manner? This answer looked promising, but I don’t understand it well enough to adapt it to my use.
I am open to solutions other than sed or awk, but any other tools need to exist in a default Red Hat/CentOS environment (during %post in Kickstart.)
Unfortunately,
awkdoes tend to munge whitespace if you change any of the fields. But this is pretty easy with sed. If your sed supports\Sto represent non-whitespace, you can do:sed 's/\S\S*/&,nodev/4'to append,nodevto the 4th column. So try:If your sed does not allow
/S, use[^ ]instead (actuall space and a tab inside brackets.) This does not limit the match ofnfsto the 3rd column. Exercise left for the reader.