Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

grep

sed

awk

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.

like image 994
Talespin_Kit Avatar asked Nov 26 '12 13:11

Talespin_Kit


3 Answers

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
like image 143
Vanuan Avatar answered Nov 16 '22 21:11

Vanuan


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.

like image 22
Guru Avatar answered Nov 16 '22 22:11

Guru


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'
like image 2
sampson-chen Avatar answered Nov 16 '22 22:11

sampson-chen