JavaRed
JavaRed

Reputation: 708

change a word in a matching line

I try to search and replace a word but only in lines containing a pattern. For my case it's a /etc/fstab file and I would like to modify ro flag to rw but only for some specific share.

This is how my NFS shares look like:

servername1:/path/to/share1 /nfs/mount1 nfs ro,nosuid,soft,vers=3   0 0
servername2:/path/to/share1 /nfs/mount2 nfs ro,nosuid,soft,vers=3   0 0
servername1:/path/to/share2 /nfs/mount3 nfs ro,nosuid,soft,vers=3   0 0

I want to change this to: servername1:/path/to/share1 /nfs/mount nfs rw,nosuid,soft,vers=3 0 0

But only for a specific server and share. I tried this:

sed '/servername1.*share1 s/ro/rw' fstab

But it didn't work. So which regex should I use in order to accomplish my goal?

Thanks

Upvotes: 1

Views: 90

Answers (1)

anubhava
anubhava

Reputation: 785196

You can use this sed:

sed '/^servername1.*share1/s/ro,/rw,/' fstab

Output:

servername1:/path/to/share1 /nfs/mount1 nfs rw,nosuid,soft,vers=3 0 0
servername2:/path/to/share1 /nfs/mount2 nfs ro,nosuid,soft,vers=3 0 0
servername1:/path/to/share2 /nfs/mount3 nfs ro,nosuid,soft,vers=3 0 0

Upvotes: 1

Related Questions