The right tool for filtering UUIDs from blkid output (using grep, cut or awk, etc)

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.

+7
source share
4 answers

For all UUIDs you can:

 $ 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 attempts to group the contents in double quotes after the UUID keyword and replaces the entire string with a token.

+4
source

Why do you make it so complicated?

Try the following:

 # blkid -s UUID -o value d7ec380e-2521-4fe5-bd8e-b7c02ce41601 fc54f19a-8ec7-418b-8eca-fbc1af34e57f 6f218da5-3ba3-4647-a44d-a7be19a64e7a 

Or that:

 # blkid -s UUID -o value /dev/sda1 d7ec380e-2521-4fe5-bd8e-b7c02ce41601 

Install the appropriate blkid package if you do not have it:

 sudo apt-get install util-linux sudo yum install util-linux 
+36
source

Here's a short awk solution:

 blkid | awk 'BEGIN{FS="[=\"]"} {print $(NF-1)}' 

Output:

 4CC9-0015 70CF-169F 3830C24D30C21234 

Explanation:

  • BEGIN{FS="[=\"]"} : use = and " as delimiters
  • {print $(NF-1)} : NF field number icons; here we print the second in the last field
  • This is based on the sequential structure of the blkid output: the UUID in quotation marks is at the end of each line.

As an alternative:

 blkid | awk 'BEGIN{FS="="} {print $NF}' | sed 's/"//g' 
+2
source

data.txt

 /dev/sda2: LABEL="A" UUID="4CC9-0015" /dev/sda3: LABEL="B" UUID="70CF-169F" /dev/sda1: LABEL=" NTFS_partition" UUID="3830C24D30C21234" 

awk and sed combination

 cat data.txt | awk 'BEGIN{FS="UUID";RS="\n"} {print $2}' | sed -e 's/=//' -e 's/"//g' 

Explanation:

  • Set the field separator to the string "UUID", $2 will give the remaining result
  • use sed, then remove = and " as shown here, where -e is the switch so you can give multiple sed commands / expression in one.
  • All occurrences are deleted using the termination parameter g , that is, global.
0
source

All Articles