Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Fetching a string after a specified string with bash

32 views
Skip to first unread message

wke...@gmail.com

unread,
Sep 4, 2016, 5:04:44 AM9/4/16
to
Hello,
I want to parse the output of
blkid /dev/sda1 and
fetch the string after UUID= ...

The thing is that using the usual -f -d'' seems to me not possible
because the output string can vary depending on whether a label is set
on this device, so seems a problem to know the location of the required
string.

What I mean is that
on some machines we will have
blkid /dev/sda1
/dev/sda1: LABEL="F24" UUID="69611ffb-a322-43b1-80fd-008a66b1a04e" TYPE="ext4"
and on some there is no LABEL set
/dev/sda1: UUID="0822921e-61d7-439a-a361-eb40116a2a74" TYPE="swap"

I want to have a script which will return, for the first case,
69611ffb-a322-43b1-80fd-008a66b1a04e, and
0822921e-61d7-439a-a361-eb40116a2a74 for the second case.

Any ideas ?
Kevin

Kenny McCormack

unread,
Sep 4, 2016, 5:15:28 AM9/4/16
to
In article <9aa98fb2-20d3-439a...@googlegroups.com>,
<wke...@gmail.com> wrote:
...
>What I mean is that
>on some machines we will have
>blkid /dev/sda1
>/dev/sda1: LABEL="F24" UUID="69611ffb-a322-43b1-80fd-008a66b1a04e" TYPE="ext4"
>and on some there is no LABEL set
>/dev/sda1: UUID="0822921e-61d7-439a-a361-eb40116a2a74" TYPE="swap"
>
>I want to have a script which will return, for the first case,
>69611ffb-a322-43b1-80fd-008a66b1a04e, and
>0822921e-61d7-439a-a361-eb40116a2a74 for the second case.

Using AWK:

sub(/^.*UUID="/,"") && sub(/".*/,"")

--
Christianity is not a religion.

- Rick C Hodgin -

Josef Kleber

unread,
Sep 4, 2016, 5:24:24 AM9/4/16
to
$ blkid1='/dev/sda1: LABEL="F24"
UUID="69611ffb-a322-43b1-80fd-008a66b1a04e" TYPE="ext4"'

$ blkid2='/dev/sda1: UUID="0822921e-61d7-439a-a361-eb40116a2a74"
TYPE="swap"'

$ echo $blkid1 | sed -e "s/.*UUID=\"\(.*\)\" .*/\1/"
69611ffb-a322-43b1-80fd-008a66b1a04e

$ echo $blkid2 | sed -e "s/.*UUID=\"\(.*\)\" .*/\1/"
0822921e-61d7-439a-a361-eb40116a2a74

Josef

--
PGP Public Key:
http://kirza.kleberj.de/pgp
DAA2 4AA6 5157 AD05 F6B8 043A D1BE 1EEA 86C6 4C5F

Kenny McCormack

unread,
Sep 4, 2016, 5:27:18 AM9/4/16
to
In article <nqgonc$g87$1...@news.xmission.com>,
Using lex:

--- Cut Here ---
%option noyywrap
%%
UUID=\"[^\"]*\" { printf("%.*s\n",(int) strlen(yytext)-7,yytext + 6); }
.|\n
%%
int main(void)
{
/* Call the lexer, then quit. */
yylex();
return 0;
}
--- Cut Here ---

And the usual incantation (assume above is z.l):

lex z.l && gcc lex.yy.c && ./a.out < yourfile

--
The randomly chosen signature file that would have appeared here is more than 4
lines long. As such, it violates one or more Usenet RFCs. In order to remain in
compliance with said RFCs, the actual sig can be found at the following web address:
http://www.xmission.com/~gazelle/Sigs/Pearls

Rakesh Sharma

unread,
Sep 4, 2016, 5:54:20 AM9/4/16
to
On Sunday, 4 September 2016 14:34:44 UTC+5:30, wke...@gmail.com wrote:

>
> What I mean is that
> on some machines we will have
> blkid /dev/sda1
> /dev/sda1: LABEL="F24" UUID="69611ffb-a322-43b1-80fd-008a66b1a04e" TYPE="ext4"
> and on some there is no LABEL set
> /dev/sda1: UUID="0822921e-61d7-439a-a361-eb40116a2a74" TYPE="swap"
>
> I want to have a script which will return, for the first case,
> 69611ffb-a322-43b1-80fd-008a66b1a04e, and
> 0822921e-61d7-439a-a361-eb40116a2a74 for the second case.
>
> Any ideas ?
>

out=$(blkid /dev/sda1)
expr "X $out" : 'X.* UUID="\([^"]*\)"'

Ben Bacarisse

unread,
Sep 4, 2016, 6:14:10 AM9/4/16
to
wke...@gmail.com writes:
<snip>
> What I mean is that
> on some machines we will have
> blkid /dev/sda1
> /dev/sda1: LABEL="F24" UUID="69611ffb-a322-43b1-80fd-008a66b1a04e" TYPE="ext4"
> and on some there is no LABEL set
> /dev/sda1: UUID="0822921e-61d7-439a-a361-eb40116a2a74" TYPE="swap"
>
> I want to have a script which will return, for the first case,
> 69611ffb-a322-43b1-80fd-008a66b1a04e, and
> 0822921e-61d7-439a-a361-eb40116a2a74 for the second case.

You can also do it in the shell:

ID=$(blkid /dec/sda1)
ID=${ID#*UUID=\"}
echo ${ID%%\"*}

--
Ben.

David W. Hodgins

unread,
Sep 4, 2016, 7:49:50 AM9/4/16
to
On Sun, 04 Sep 2016 05:04:32 -0400, <wke...@gmail.com> wrote:

> I want to parse the output of
> blkid /dev/sda1 and
> fetch the string after UUID= ...

Bash only ...
bid=$(/sbin/blkid /dev/sda1);idrem=${bid##*UUID=\"};echo ${idrem%%\"*}

Regards, Dave Hodgins

--
Change dwho...@nomail.afraid.org to davidw...@teksavvy.com for
email replies.

Bit Twister

unread,
Sep 4, 2016, 9:30:21 AM9/4/16
to
On Sun, 4 Sep 2016 02:04:32 -0700 (PDT), wke...@gmail.com wrote:
> Hello,
> I want to parse the output of
> blkid /dev/sda1 and
> fetch the string after UUID= ...

just ask blkid to print the UUID.

Here run this command
blkid -s UUID -o value

Janis Papanagnou

unread,
Sep 4, 2016, 10:03:59 AM9/4/16
to
As so often the best solutions are found if the posters' questions
are not taken literally.

Janis

Ed Morton

unread,
Sep 4, 2016, 8:08:20 PM9/4/16
to
idk what "the ususal -d -d''" means but there's so may options, including:

$ cat file
/dev/sda1: LABEL="F24" UUID="69611ffb-a322-43b1-80fd-008a66b1a04e" TYPE="ext4"
/dev/sda1: UUID="0822921e-61d7-439a-a361-eb40116a2a74" TYPE="swap"

$ sed 's/.*UUID="\([^"]*\).*/\1/' file
69611ffb-a322-43b1-80fd-008a66b1a04e
0822921e-61d7-439a-a361-eb40116a2a74

$ awk -F'"' '{print $(NF-3)}' file
69611ffb-a322-43b1-80fd-008a66b1a04e
0822921e-61d7-439a-a361-eb40116a2a74

Rakesh Sharma

unread,
Sep 5, 2016, 10:47:53 AM9/5/16
to
On Sunday, 4 September 2016 14:34:44 UTC+5:30, wke...@gmail.com wrote:

> What I mean is that on some machines we will have
> blkid /dev/sda1
> /dev/sda1: LABEL="F24" UUID="69611ffb-a322-43b1-80fd-008a66b1a04e" TYPE="ext4"
> and on some there is no LABEL set
> /dev/sda1: UUID="0822921e-61d7-439a-a361-eb40116a2a74" TYPE="swap"
>
> I want to have a script which will return, for the first case,
> 69611ffb-a322-43b1-80fd-008a66b1a04e, and
> 0822921e-61d7-439a-a361-eb40116a2a74 for the second case.
>
> Any ideas ?
> Kevin
>


# sed-based solution
sed -ne 's/UUID="/\n/;s/.*\n//;s/"/\n/;P'

# perl
perl -lpe '($_)=/(?<=UUID=").*?(?=")/g'

# sh with builtins
set -f
IFS=\"
set X $out; shift
while case $# in 0) break;; esac; do
case $1 in
'' | *[!UID=\ ]* ) :;;
UUID= ) shift; echo "$1"; break;;
[!\ ]* | ?*[!\ ]*[\ ]* |\
*[\ ][!U\ ]* |\
*[\ ][U][!U]* |\
*[\ ][U][U][!I]* |\
*[\ ][U][U][I][!D]* |\
*[\ ][U][U][I][D][!=]* |\
*[\ ][U][U][I][D][=]?* ) :;;
* ) shift; echo "$1"; break;;
esac
shift
done

Kaz Kylheku

unread,
Sep 5, 2016, 11:07:30 AM9/5/16
to
On 2016-09-05, Rakesh Sharma <shar...@hotmail.com> wrote:
> On Sunday, 4 September 2016 14:34:44 UTC+5:30, wke...@gmail.com wrote:
>
> > What I mean is that on some machines we will have
> > blkid /dev/sda1
> > /dev/sda1: LABEL="F24" UUID="69611ffb-a322-43b1-80fd-008a66b1a04e" TYPE="ext4"
> > and on some there is no LABEL set
> > /dev/sda1: UUID="0822921e-61d7-439a-a361-eb40116a2a74" TYPE="swap"
> >
> > I want to have a script which will return, for the first case,
> > 69611ffb-a322-43b1-80fd-008a66b1a04e, and
> > 0822921e-61d7-439a-a361-eb40116a2a74 for the second case.
> >
> > Any ideas ?
> > Kevin
> >
>
>
> # sed-based solution
> sed -ne 's/UUID="/\n/;s/.*\n//;s/"/\n/;P'

The wide old adage "it's all over but the shouting" was
crafted for these situations.

It was already shown that the the program can dump
the values of selected tags (-s <tag> option), and the format
can be altered (-o <format-keyword> option).

# blkid /dev/sda1
/dev/sda1: UUID="ffe2c614-30e3-4b97-9c00-1b25fef2934f" TYPE="ext4"

# blkid -o value -s UUID /dev/sda1
ffe2c614-30e3-4b97-9c00-1b25fef2934f

If you still feel like doing some clever shell thing,
you can use "-o export" and then eval the result:

# blkid -o export /dev/sda1
UUID=ffe2c614-30e3-4b97-9c00-1b25fef2934f
TYPE=ext4

If we

# eval $(blkid -o export /dev/sda1)

we then we now have UUID and TYPE shell variables.

The man page says that export makes for "easy import into the
environment", from which we might guess that either does shell
escaping for eval (easy import in shell scripts), or else dumps
the strings raw (easy for a C program that reads lines with fgets,
removes the newline from each one, and passes it to putenv).

Ian Zimmerman

unread,
Sep 5, 2016, 11:29:08 AM9/5/16
to
On 2016-09-04 02:04, wke...@gmail.com wrote:

> I want to parse the output of blkid /dev/sda1 and fetch the string
> after UUID= ...

I just avoid blkid, and get that information from the /dev/disk/ subtree.

--
Please *no* private Cc: on mailing lists and newsgroups
Why does the arrow on Hillary signs point to the right?

0 new messages