3

here is example how to add string on the beginning of line when match the UUID number in fstab

sed -e "/UUID=953b1921-ac73-4b7b-abaf-ff983b0fce8a/ s/^/###FAULTY_DISK###/"  -i /etc/fstab

and we can verify with

more /etc/fstab

###FAULTY_DISK###UUID=953b1921-ac73-4b7b-abaf-ff983b0fce8a /data/sdc ext4 defaults,noatime 0 0

but on the second running we get

sed -e "/UUID=953b1921-ac73-4b7b-abaf-ff983b0fce8a/ s/^/###FAULTY_DISK###/"  -i /etc/fstab

more /etc/fstab ###FAULTY_DISK######FAULTY_DISK###UUID=953b1921-ac73-4b7b-abaf-ff983b0fce8a /data/sdc ext4 defaults,noatime 0 0

how to tell sed to ignore the adding of ###FAULTY_DISK### , in case it already set in file fstab

terdon
  • 242,166
yael
  • 13,106

4 Answers4

8

Append only if it was starting with that UUID:

sed -e 's/^UUID=953b1921-ac73-4b7b-abaf-ff983b0fce8a/###FAULTY_DISK###&/' /etc/fstab

or, in case you wanted to ignore leading whitespace too if any:

sed -e 's/^[[:blank:]]*UUID=953b1921-ac73-4b7b-abaf-ff983b0fce8a/###FAULTY_DISK###&/' /etc/fstab
terdon
  • 242,166
αғsнιη
  • 41,407
5

Another approach is to just skip lines with ###FAULTY_DISK###. Consider this example file that has one line with the faulty disk comment and another without:

$ cat fstab 
UUID=953b1921-ac73-4b7b-abaf-ff983b0fce8a /data/sdc ext4 defaults,noatime 0 0
###FAULTY_DISK###UUID=953b1921-ac73-4b7b-abaf-ff983b0fce8a /data/sdc ext4 defaults,noatime 0 0

You can use the n command in sed to move to the next line. So tell sed to move to the next line if this one matches the faulty message:

$ sed  "/###FAULTY_DISK###/n; s/UUID=953b1921-ac73-4b7b-abaf-ff983b0fce8a/###FAULTY_DISK###&/" fstab 
###FAULTY_DISK###UUID=953b1921-ac73-4b7b-abaf-ff983b0fce8a /data/sdc ext4 defaults,noatime 0 0
###FAULTY_DISK###UUID=953b1921-ac73-4b7b-abaf-ff983b0fce8a /data/sdc ext4 defaults,noatime 0 0

As you can see, the message was only added to the line that did not have it.

terdon
  • 242,166
  • You'd want to use b instead of n here (and use two -expressions). With n here, if both line 1 and line 2 contain FAULTY_DISK and line 2 contains the UUID, you'll add an extra FAULTY_DISK – Stéphane Chazelas Feb 07 '22 at 08:14
1

If I understand correctly:

Given

##FAULTY_DISK###UUID=953b1921-ac73-4b7b-abaf-ff983b0fce8a
UUID=953b1921-ac73-4b7b-abaf-ff983b0fce8a

Then

$ sed '/^###FAULTY_DISK###UUID=953b1921-ac73-4b7b-abaf-ff983b0fce8a/!s/^UUID=953b1921-ac73-4b7b-abaf-ff983b0fce8a/###FAULTY_DISK###&/' /etc/fstab 
###FAULTY_DISK###UUID=953b1921-ac73-4b7b-abaf-ff983b0fce8a
###FAULTY_DISK###UUID=953b1921-ac73-4b7b-abaf-ff983b0fce8a
0

Using Raku (formerly known as Perl_6)

raku -pe 's/ ^ ("UUID=86d58af9-801b-4c25-b59d-80b52b4acc61") /\#\#\#FAULTY_DISK\#\#\#$0/;' fstab_test.txt

OR

raku -pe 's[ ^ ("UUID=86d58af9-801b-4c25-b59d-80b52b4acc61") ] = "\#\#\#FAULTY_DISK\#\#\#$0";'

The answers above are written in Raku, a member of the Perl-family of programming languages. Once prepended with ###FAULTY_DISK###, a line in the fstab file is ignored during subsequent runs of the same Raku one-liner, i.e. the modification is stable.

See below link for Sample Input and Sample Output.

https://unix.stackexchange.com/a/689556/227738

jubilatious1
  • 3,195
  • 8
  • 17