right tool to filter the UUID from the output of blkid program (using grep, cut, or awk, e.t.c)

awk, grep, sed

Solution

For all the UUID's, you can do :

$ blkid | sed -n 's/.*UUID=\"\([^\"]*\)\".*/\1/p' 
d7ec380e-2521-4fe5-bd8e-b7c02ce41601
fc54f19a-8ec7-418b-8eca-fbc1af34e57f
6f218da5-3ba3-4647-a44d-a7be19a64e7a

Say, only for a specific sda1:

$ blkid | sed -n '/sda1/s/.*UUID=\"\([^\"]*\)\".*/\1/p' 
d7ec380e-2521-4fe5-bd8e-b7c02ce41601

The sed command tries to group the contents present within the double quotes after the UUID keyword, and replaces the entire line with the token.

Problem

I want to filter the output of the blkid to get the UUID. The output of blkid 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 the output of blkid 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 the combination of grep and cut it can be done as ``` /sbin/blkid | /bin/grep 'sda1' | /bin/grep -o -E 'UUID="[a-zA-Z|0-9|\-]*' | /bin/cut -c 7- ``` I have tried using awk , grep and cut as below for filtering 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 since sometimes an extra field such as LABEL may be present in the output of the blkid program as shown in the above output. What is the best way to create a command using awk which works reliably? Please post if any other elegant method exits for the job using bin and core utils. I dont want to use perl or python since this has to be run on busybox. NOTE:-I am using busybox blkid to which /dev/sda1 can not be passed as the args(the version i am using does not support it) hence the grep to filter the line. UPDATE :- added the CASE 2: -output to show that field position can not be relied upon.

Original source