[SOLVED] How to only get certain parts from the output of a command

Jan 27, 2021
99
8
45
4
I have a script I am working on that I need to cut out part of the output from a command. For example:

Code:
#! /bin/bash

blkid
read -p "Enter the drive from the list above (example: /dev/sda1): " drive

blkid | grep $drive # <-- This is where I am stuck. I want to sort out the UUID, PARTUUID, and the TYPE of filesystem

echo "UUID="$uuid" /home/Documents "$format" defaults,nofail 0 0" | sudo tee -a /etc/fstab
I know about the sed command, but I have no idea on how to use it. Any suggestions?
 
Apr 4, 2021
91
22
45
4
Aha, well this being Linux command line there probably about 1,000,001 ways to do what you want :giggle:.

Ok, well this is probably more what you want: (-s is the same as --match-tag except alot less typing)
blkid -s UUID -o value /dev/sda1

Hope that helps.


{GoofyOne's 2 worth ... proud he's learning all sorts of stuff about blkid command lately}
 

kanewolf

Titan
Moderator
I have a script I am working on that I need to cut out part of the output from a command. For example:

Code:
#! /bin/bash

blkid
read -p "Enter the drive from the list above (example: /dev/sda1): " drive

blkid | grep $drive # <-- This is where I am stuck. I want to sort out the UUID, PARTUUID, and the TYPE of filesystem

echo "UUID="$uuid" /home/Documents "$format" defaults,nofail 0 0" | sudo tee -a /etc/fstab
I know about the sed command, but I have no idea on how to use it. Any suggestions?
Have you tried the "-o device" option on blkid ???
 
Apr 4, 2021
91
22
45
4
G'dday.

You can select particular info you want from the blkid command.

Try these commands:
blkid --match-tag TYPE
blkid --match-tag UUID
blkid --match-tag PARTUUID

You can then pipe that with grep to select only the device you want.
blkid --match-tag TYPE | grep /dev/sda1

Another thing you can do is make it create shell variables that you can use later on in your script.
eval $(blkid --match-tag TYPE | grep /dev/sdb2 | gawk '{ print $2 }')

That sends the command TYPE="ext4" (if it's a ext4 file system of course) to the shell, so then you will have a variable named TYPE which has the value "ext4". And then you can do: echo $TYPE and it should display ext4 on the screen.



{GoofOne's 2c worth .... pining for the good ole days when he used to be good with Linux}
 
Reactions: Cj-tech

kanewolf

Titan
Moderator
No. I can try it when I get back to my computer. I looked at the man page and it doesn’t seem like it can help. I want to separate the output of blkid to get each thing and set them as variables.
Since your code snippet seemed to want a list of devices, the "-o device" would be appropriate.
Maybe if you better described what you wanted, we could offer better better help. Right now we are guessing.
 
Jan 27, 2021
99
8
45
4
Since your code snippet seemed to want a list of devices, the "-o device" would be appropriate.
Maybe if you better described what you wanted, we could offer better better help. Right now we are guessing.
I’m super sorry for any confusion. I’ve had a really busy day and typed the original post too fast. @GoofyOne seems to have a solution. Though I think this is how I would create variables:
Code:
var=$(blkid --match-tag TYPE | grep /dev/sda1)
 
Jan 27, 2021
99
8
45
4
G'dday.

You can select particular info you want from the blkid command.

Try these commands:
blkid --match-tag TYPE
blkid --match-tag UUID
blkid --match-tag PARTUUID

You can then pipe that with grep to select only the device you want.
blkid --match-tag TYPE | grep /dev/sda1

Another thing you can do is make it create shell variables that you can use later on in your script.
eval $(blkid --match-tag TYPE | grep /dev/sdb2 | gawk '{ print $2 }')

That sends the command TYPE="ext4" (if it's a ext4 file system of course) to the shell, so then you will have a variable named TYPE which has the value "ext4". And then you can do: echo $TYPE and it should display ext4 on the screen.



{GoofOne's 2c worth .... pining for the good ole days when he used to be good with Linux}
Actually there is a problem with that. It still lists the drive name in the output:
Code:
$ blkid --match-tag TYPE | grep /dev/sda1
/dev/sda1: TYPE="#####"
 
Last edited:
Apr 4, 2021
91
22
45
4
Yes, so pipe that to gawk. On the end of that command put | gawk '{ print $2 }'

gawk parses the input, and makes into arguments separated by space, so $1 will be the /dev/sda1: , and $2 will be PARTUUID="#######"


{GoofyOne}
 
Jan 27, 2021
99
8
45
4
Yes, so pipe that to gawk. On the end of that command put | gawk '{ print $2 }'

gawk parses the input, and makes into arguments separated by space, so $1 will be the /dev/sda1: , and $2 will be PARTUUID="#######"


{GoofyOne}
I guess that will work, but gawk does not come pre-installed on most operating systems. I'm trying to incorporate this into a script that can be used on most systems.
 
Apr 4, 2021
91
22
45
4
Aha, well this being Linux command line there probably about 1,000,001 ways to do what you want :giggle:.

Ok, well this is probably more what you want: (-s is the same as --match-tag except alot less typing)
blkid -s UUID -o value /dev/sda1

Hope that helps.


{GoofyOne's 2 worth ... proud he's learning all sorts of stuff about blkid command lately}
 
Jan 27, 2021
99
8
45
4
Aha, well this being Linux command line there probably about 1,000,001 ways to do what you want :giggle:.

Ok, well this is probably more what you want: (-s is the same as --match-tag except alot less typing)
blkid -s UUID -o value /dev/sda1

Hope that helps.


{GoofyOne's 2 worth ... proud he's learning all sorts of stuff about blkid command lately}
Haha.... I found an alternative while waiting but yours is so much simpler. What I found that also works is:
Code:
uuid=$(blkid --match-tag UUID | grep /dev/sda1) &&  uuid1="${uuid#*=}"
echo $uuid1
I'll definitely use yours as it is so much simpler.... Thanks for all the advice :)

Edit: Just in case anyone is interested, this script is available here on my GitHub repository. It automatically configures a drive to mount at reboot (specifically for Raspberry Pi's). Thanks for the advice @GoofyOne. I hope to use this knowledge in future projects.
 
Last edited:
Thread starter Similar threads Forum Replies Date
C Open Source 1

ASK THE COMMUNITY

TRENDING THREADS