Rav
Rav

Reputation: 21

Replace a word on lines matching two conditions

I'm trying to replace defaults with defaults,nodev for /, /home, /opt, /tmp, /var.

However defaults setting should not be changed for swap and proc.

This is the /etc/fstab file:

/dev/rootvg/swap     swap                swap       defaults              0 0
/dev/rootvg/sysroot  /                   ext3       defaults              1 1
/dev/sda1            /boot               ext3       acl,user_xattr        1 2
/dev/rootvg/home     /home               ext3       defaults              1 2
/dev/rootvg/opt      /opt                ext3       defaults              1 2
/dev/rootvg/tmp      /tmp                ext3       defaults              1 2
/dev/rootvg/var      /var                ext3       defaults              1 2
proc                 /proc               proc       defaults              0 0
sysfs                /sys                sysfs      noauto                0 0
debugfs              /sys/kernel/debug   debugfs    noauto                0 0
devpts               /dev/pts            devpts     mode=0620,gid=5       0 0

Upvotes: 2

Views: 47

Answers (3)

Kent
Kent

Reputation: 195039

It seems that you want to change the option for all ext3 file systems. then you can:

sed '/ext[2-4]/s/defaults/&,nodev/' file

with this line, it will apply the substitution on ext2, 3, 4 FS. You can adjust it if you want , e.g. ext3 for ext3 only. But this may not always give you correct result, since regex was used.

If you have to do the substitution for certain mount point, I would use awk check the column and I won't use regex either.

Upvotes: 0

Kalanidhi
Kalanidhi

Reputation: 5092

I hope this sed command will help to you

sed '/proc\|swap/!s/defaults/&,nodev/' /etc/fstab

Output:

/dev/rootvg/swap     swap                swap       defaults              0 0
/dev/rootvg/sysroot  /                   ext3       defaults,nodev              1 1
/dev/sda1            /boot               ext3       acl,user_xattr        1 2
/dev/rootvg/home     /home               ext3       defaults,nodev              1 2
/dev/rootvg/opt      /opt                ext3       defaults,nodev              1 2
/dev/rootvg/tmp      /tmp                ext3       defaults,nodev              1 2
/dev/rootvg/var      /var                ext3       defaults,nodev              1 2
proc                 /proc               proc       defaults              0 0
sysfs                /sys                sysfs      noauto                0 0
debugfs              /sys/kernel/debug   debugfs    noauto                0 0
devpts               /dev/pts            devpts     mode=0620,gid=5       0 0

Note: If you want changes in file use -i option

Upvotes: 2

Toby Speight
Toby Speight

Reputation: 30728

You can use { to combine two regex adresses:

/^\//{/swap/!s/defaults/defaults,nodev/}

More verbosely:

#!/bin/sed
/^\//{
/swap/!s/defaults/defaults,nodev/
}

So on lines that begin with /, then if that line doesn't contain swap, then do the substitution.

In fact, for that particular file, all the disk filesystems are ext3, so you could simplify to just

/ext3/s/defaults/defaults,nodev/

Upvotes: 1

Related Questions