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

Why are you making it so complex?

Try this:

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

Or this:

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

Install proper blkid package if you don't have it:

sudo apt-get install util-linux
sudo yum install util-linux

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.


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 stands of Number of Fields; here we print the 2nd to last field
  • This is based on the consistent structure of blkid output: UUID in quotes is at the end of each line.

Alternatively:

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

Tags:

Grep

Awk

Sed