0

In a file, there is:

sd5_crypt UUID=1337 none luks,discard

I'd like to change it to:

sd5_crypt UUID=1337 /dev/disk/by-label/MON_LABEL_ICI:/keyfile luks,keyscript=/lib/cryptsetup/scripts/passdev

with a Shell script.

I dont know (blind) if it is sd5, sd4 or sx999... I dont know the UUID.

I have to change all what is after 1337. Well, said humanly "I delete all after the UUID number - I dunno it - and replace with /dev/dis/by-label.... which I know.

I dont know which is the good solution. Can you help please ?

7
  • 1
    If there is only the line you posted in a file, you can use something like: sed -e 's/none luks,discard/YOURPATH luks,THEREST/g'
    – Wayne_Yux
    Aug 20, 2015 at 13:28
  • Thank you. Why -e not -i ?
    – 3pic
    Aug 20, 2015 at 13:39
  • Yes you're right. Sorry, I messed these up
    – Wayne_Yux
    Aug 20, 2015 at 13:42
  • @Wayne_Yux that YOURPATH part contains forward slashes. Might want to escape those with backslash Aug 20, 2015 at 13:47
  • 1
    @serg yes of course. I think I will turn my comment into an answer and expand it a bit
    – Wayne_Yux
    Aug 20, 2015 at 13:50

1 Answer 1

2

Expanded version of my comment:

You can use sed to edit your file. In general you need:

sed 's/STRING/REPLACEMENT/g'

In your example the following code should work for you:

With explanation:

sed -i                # the -i option allows you to read from and write to the same file
's/none luks,discard/ # the part you want to replace
\/dev\/disk\/by-label\/MON_LABEL_ICI:\/keyfile luks,keyscript=\/lib\/cryptsetup\/scripts\/passdev/g'
                     # the part you want to insert. Note, that the / characters
                     # have to be escaped with \/
 input.txt           # your input file

as one-liner without comments to copy-paste:

sed -i 's/none luks,discard/\/dev\/disk\/by-label\/MON_LABEL_ICI:\/keyfile luks,keyscript=\/lib\/cryptsetup\/scripts\/passdev/g' input.txt
3
  • not sure you need quotes 's/.../.../g'
    – 3pic
    Aug 20, 2015 at 14:11
  • It s working but I bet there is a safer way, imagine a system where it is none luks, discard (note the space after the comma). In such a case it fails. In the future I'll detect the second space (after UUID) and replace all after, with some copy, concatenate, paste.
    – 3pic
    Aug 20, 2015 at 14:13
  • you can also use two sed commands like sed 's/none/FIRST/g' | sed 's/discard/SECOND/g' then you don't have to look for spaces after all
    – Wayne_Yux
    Aug 20, 2015 at 14:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .