Member-only story
Mastering UUID Formats: Seamless Conversion with Command-Line Magic
Converting UUID Formats in Command Line
When dealing with UUIDs, you might encounter formats with or without hyphens, and sometimes you need to convert between these two formats. Command-line tools like tr
or sed
can be used for this purpose. Here are specific conversion methods and explanations of command parameters.
In the examples below, the uuidgen
command is used to generate UUIDs in the 8-4-4-4-12 format, and the dbus-uuidgen
command is used to generate UUIDs without hyphens. For more methods to generate UUIDs, refer to the related readings at the end of the article.
Converting from Hyphenated UUID to Non-Hyphenated UUID
Using the tr
command:
~$ uuidgen | tr -d '-'
c560545a286a4ceb90454d0e428e22de
The tr
command is used for character replacement and deletion. Here, tr -d '-'
is used to delete all hyphen characters.
Using the sed
command:
~$ uuidgen | sed 's/-//g'
25375aed97b244a0a1c18616c3305d72
sed
is a stream editor used for text replacement in input streams (or files). s/-//g
replaces all hyphen characters with an empty string.
Converting from Non-Hyphenated UUID to 8–4–4–4–12 Format
Using the sed
command:
~$ dbus-uuidgen | sed -E 's/(.{8})(.{4})(.{4})(.{4})(.*)/\1-\2-\3-\4-\5/'…