I want to filter blkid output to get UUID.
The blkid output looks like
CASE 1: -
$ blkid /dev/sda2: LABEL="A" UUID="4CC9-0015" /dev/sda3: LABEL="B" UUID="70CF-169F" /dev/sda1: LABEL=" NTFS_partition" UUID="3830C24D30C21234"
In somecases, blkid output looks like
CASE 2: -
$ blkid /dev/sda1: UUID="d7ec380e-2521-4fe5-bd8e-b7c02ce41601" TYPE="ext4" /dev/sda2: UUID="fc54f19a-8ec7-418b-8eca-fbc1af34e57f" TYPE="ext4" /dev/sda3: UUID="6f218da5-3ba3-4647-a44d-a7be19a64e7a" TYPE="swap"
I want to filter out the UUID.
Using a combination of grep and cut , this can be done as
/sbin/blkid | /bin/grep 'sda1' | /bin/grep -o -E 'UUID="[a-zA-Z|0-9|\-]*' | /bin/cut -c 7-
I tried using awk , grep and cut as shown below to filter the UUID
$ /sbin/blkid | /bin/grep 'sda1' | /usr/bin/awk '{print $2}' | /bin/sed 's/\"//g' | cut -c 7- 7ec380e-2521-4fe5-bd8e-b7c02ce41601
The above command (which uses awk) is not reliable, because sometimes an additional field, such as LABEL, may be present at the output of the blkid program, as shown in the above output.
What is the best way to create a team using awk that works reliably? Please send a message if any other elegant method comes out to work using bin and core utils. I do not want to use perl or python, since it needs to be run on busybox.
NOTE. -I use busybox blkid, to which / dev / sda1 cannot be passed as arguments (the version I use does not support), hence grep to filter the line.
UPDATE: - added CASE 2: -output to show that the field position cannot be relied on.
Talespin_Kit
source share