I'm creating a script that will get the UUID of inserted drive realtime using

udiskctl monitor

these are some of stdout lines

HintPartitionable:          true
HintSystem:                 false
Id:                         by-uuid-EXTERNALLABEL
IdType:                     vfat
IdUUID:                     EXTERNALLABEL
IdUsage:                    filesystem
IdVersion:                  FAT32

So I want to get the string in front of idUUID:

Expected Output:


Anyone have any idea then please suggest?

  • I'm sure there's a duplicates on U&L (or SO) – RomanPerekhrest Nov 21 at 10:32
  • 2
    "In front of" ??? That would normally mean "to the left of". But your expected output is after (to the right of) the tag. Try: awk '/IdUUID/ { print $2 }' – Paul_Pedant Nov 21 at 10:51

This is a frequent question and there are answers on how to do this already, but you can use awk to do this:

udiskctl monitor | awk '{if (match($0,"^[[:space:]]*IdUUID:[[:space:]]*([[:print:]]+)",a)) print a[1]}'

or, even shorter as suggested by @Archemar

udiskctl monitor | awk '/^[[:space:]]*IdUUID:/ {print $2}'

or, more robust as suggested by @Ed Morton:

udiskctl monitor | awk '$1=="IdUUID:"{print $2}'

And or course, good old sed:

udiskctl monitor | sed -e 's/^[[:space:]]*IdUUID:[[:space:]]*//;t;d'
  • 1
    udiskctl monitor | awk '/idUUID/ { print $2}' also work. – Archemar Nov 21 at 10:39
  • 1
    @Archemar /^IdUUID:/ to be more sure to find the correct string. – Kusalananda Nov 21 at 10:41
  • Sorry for not researching enough, but thank you. It works like a charm! – undefinedhax Nov 21 at 10:54
  • @undefinedhax It's ok, it was a nice little exercise .. – AdminBee Nov 21 at 11:30

The correct answer is:

$ awk '$1=="IdUUID:"{print $2}' file

The currently accepted answer is unnecessarily and incorrectly treating the string you want to search for as if it were a regexp and so that approach would fail if your string contained regexp metacharacters.


Copyright James Daniel Marrs Ritchey. This material was created for submission at 'awk - extract value infront of "somestring:"' under the terms of the cc by-sa 4.0 (https://creativecommons.org/licenses/by-sa/4.0/legalcode), but can also be alternatively obtained from 'https://snippetly.blogspot.com/2019/12/extracting-value.html' under the terms of any of the following licenses: Comprehensible Open License 3.0 (https://jamesdanielmarrsritchey.blogspot.com/2019/06/comprehensible-open-license-30.html), MIT (https://opensource.org/licenses/MIT).

Alternatively to Awk, using PHP you can extract the value portion of the string by breaking the output into an array using line breaks as the delimiter. Then break each array value into another array using the colon as the delimiter. Next trim the whitespace from the beginning and end of each value. Lastly perform a check to see if it's the entry you want, and if it is echo the value.


$output = <<<'EOT'

Item1:          Value1

Item2:                     Value2

Item3:                     Value3

Item4:                  Value4


$data = explode("\n", $output);

foreach ($data as $item){

    $result = FALSE;

    $value = explode(':', $item);

    if (trim($value[0]) === 'Item3'){

        echo trim($value[1]) . "\n";




echo "Value not found.\n";


New contributor
Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Method 1:

udiskctl monitor | awk '/IdUUID/{print $NF}'

Method 2:

udiskctl monitor | sed -n '/IdUUID/p' | sed "s/.*://g" | sed -r "s/^\s+//g"
  • 1
    Since you're answering after Ed, you might incorporate their advice in some fashion -- perhaps anchoring the regex or comparing field 1 directly, as they do. – Jeff Schaller Nov 24 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.