Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace UUID value in fstab using sed

Tags:

linux

replace

sed

My fstab is somewhat like the following:

UUID=somevalue1  
UUID=somevalue2

I want to edit the 2nd UUID. How do I do that using sed?

So after editing the fstab would look like follows:

UUID=somevalue1
UUID=somevalue3

Any help would be really appreciated

like image 493
user3266083 Avatar asked Oct 19 '25 09:10

user3266083


1 Answers

If you know somevalue2, then you can just do something like

VALUETOREPLACE='somevalue2'
sed "s/^UUID=$VALUETOREPLACE$/UUID=somevalue3/" </etc/fstab

If you don't know, you can just do the replacement 2nd time UUID= is found in the fstab:

sed ':a;N;$!ba;s/UUID=[A-Fa-f0-9-]*/UUID=somevalue3/2' </etc/fstab

You can try changing that 2 to 3 or anything else select another n-th match. Also note that on modern systems /etc/fstab frequently has this line in it:

# device; this may be used with UUID= as a more robust way to name devices

which also happens to have UUID= in it and that may mess your numbering.

like image 88
molnija Avatar answered Oct 21 '25 23:10

molnija



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!