LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   awk regexp for uuid (https://www.linuxquestions.org/questions/programming-9/awk-regexp-for-uuid-804324/)

webhope 04-26-2010 01:34 PM

awk regexp for uuid
 
I try to get uuid value from variable $block got from menu.lst
This try does not work. What is wrong?
Code:

uuid=$( echo $block | awk --re-interval '/\(root=UUID=/{ x=gensub(/([0-9a-f-]{36,36})/,"\\1","g");
print $1}')


pixellany 04-26-2010 02:26 PM

Where does "block" get assigned?

Regardless, this works:
I put a section of my menu.lst into a file "uuid"
Code:

[mherring@mystical play]$ more uuid
title  Arch Linux                                                                   
root  (hd0,1)                                                                     
kernel /boot/vmlinuz26 root=/dev/disk/by-uuid/b48c40f6-3a7f-4498-b45c-ea68e1d11071 ro                                                                       
initrd /boot/kernel26.img   
[mherring@mystical play]$ sed -r -n 's:.*by-uuid/([a-f0-9-]*).*:\1:p' uuid
b48c40f6-3a7f-4498-b45c-ea68e1d11071


pixellany 04-26-2010 02:27 PM

PS:
Does anyone know if a uuid always has the same number of characters? If so, that could be incorporated into the regex.

colucix 04-26-2010 02:38 PM

Yes. uuid is always in the form xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, that is 8-4-4-4-12 characters. There is an utility called uuidgen to generate random uuid sequences.. here are some results:
Code:

$ uuidgen
9880a9d6-671b-42ea-a90d-3189a9b03608
$ uuidgen
546d3081-d1db-4162-b41d-297764b4ef7f
$ uuidgen
30cb693c-b8a1-451b-939e-fd8a56ab69bc

Just three of the (about) 10e+38 possible combinations! ;)

pixellany 04-26-2010 02:45 PM

Quote:

10e+38 possible combinations!
That should be enough!!!

World = 6.8E+9
10E+38 / 6.8E+9 = ~ 1.5E+29 uuids for every man, woman, and child.....Use them wisely...;)

webhope 04-26-2010 03:11 PM

The $block is just a block of lines for one OS from menu.lst . I don't access menu.lst directly. I extracted the block from one big file.

I forgot. I wanted to write a more simple rule with [^ ]*

I wait till awk solution.

Tinkster 04-26-2010 03:30 PM

W/o knowing what "$block"s content looks like it will be difficult to
ascertain whether your awk statement will match or not....


We can only guess....
Code:

'{ x=gensub(/.*([0-9a-f-]{36,36}).*/,"\\1","g"); print x}


Cheers,
Tink

pixellany 04-26-2010 03:46 PM

Quote:

Originally Posted by webhope (Post 3948464)
I wait till awk solution.

Even if SED is better?......;)

webhope 04-26-2010 03:47 PM

block e.g.

Code:

title Sata Mandriva
kernel (hd0,2)/boot/vmlinuz BOOT_IMAGE=linux root=UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c  resume=UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798 splash=silent vga=788
initrd (hd0,2)/boot/initrd.img

looking for the
UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c
UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798

values...

Edit

My try:

Code:

uuid=$( echo $block_new | awk --re-interval '/\UUID=/{ x=gensub(/([0-9a-f-]{36,36}).*/,"\\1","g");
print x}')
echo $uuid;

Outputs not correct:

Code:

kernel (hd0,2)/boot/vmlinuz BOOT_IMAGE=linux root=UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c
I still need remoove the "kernel (hd0,2)/boot/vmlinuz BOOT_IMAGE=linux "


I wanted to change
root=UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c
to
root=(hd0,2)
is it valid for use in menu.lst?

webhope 04-26-2010 03:56 PM

Quote:

Originally Posted by pixellany (Post 3948504)
Even if SED is better?......;)

I want to learn in awk. Next thing is that I moste of parts of script is in awk. It would be strange to mix awk and sed commands if all can be done by awk.

Edit
Acording pixellany's menu.lst

Code:

kernel /boot/vmlinuz26 root=/dev/disk/by-uuid/b48c40f6-3a7f-4498-b45c-ea68e1d11071 ro
I need to think up next version of command. I didn't think about this,
/dev/disk/by-uuid/...
I have UUID=....

pixellany 04-26-2010 04:01 PM

Quote:

Originally Posted by webhope (Post 3948505)

I wanted to change
root=UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c
to
root=(hd0,2)
is it valid for use in menu.lst?

Yes and no.....

There are two usages of "root" in the GRUB menu.lst:

1. root (hd0,2) means: "GRUB, please look in the 3rd partition of drive #1 to find your files."

2. kernel /boot/vmlinuzxyz root=/dev/sda3 ro Here, the "root" statement is a message to the kernel, telling it where to mount the filesystem. (sda3 also means 3rd partition of drive 1)

The grub root and the filesystem root are not necessarily the same.

webhope 04-26-2010 04:18 PM

Quote:

Originally Posted by pixellany (Post 3948527)
The grub root and the filesystem root are not necessarily the same.

I thing (hd0,2) is better because depending on OS I can have
/dev/sda3 (Mandriva) or /dev/hda3 (Ubuntu)
I look for solid solution, not changeable

Edit
Was is surprising for me is POST #9 CODE - line 2 - different uuids. I guess its error in my code. Is it normal to have two different uuids on one line?

pixellany 04-26-2010 04:32 PM

Quote:

Originally Posted by webhope (Post 3948554)
Edit
Was is surprising for me is POST #9 CODE - line 2 - different uuids. I guess its error in my code. Is it normal to have two different uuids on one line?

there are 2 kernel commands: "root" is where to mount the filesystem. I assume that "resume" means where to go when waking up from suspend or hibernate----but I am just guessing. You will need to do some more research.

As for the "root" explanation, there can be only one right answer for each one---you don't have a choice. Did you understand the 2 definitions?

webhope 04-26-2010 04:44 PM

Quote:

Originally Posted by pixellany (Post 3948574)
Did you understand the 2 definitions?

Yes thanx

Yet I have problem with the awk.

Code:

uuid=$( echo $block_new | awk --re-interval '/^.*UUID=/{ x=gensub(/([0-9a-f-]{36,36}).*/,"\\1","g");  print x}')
or
uuid=$( echo $block_new |  awk --re-interval '/^.*UUID=/{ x=gensub(/UUID=([^ ]+).*/,"\\1","g");  print x}')
or
uuid=$( echo $block_new | awk --re-interval '/.*UUID=/{ x=gensub(/([0-9a-f-]{36,36}).*/,"\\1","g");  print x}')
or
uuid=$( echo $block_new |  awk --re-interval '/.*UUID=/{ x=gensub(/UUID=([^ ]+).*/,"\\1","g");  print x}')

Both give
Code:

kernel (hd0,2)/boot/vmlinuz BOOT_IMAGE=linux root=UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c
Why cannot to cut out the part before UUID= ?

Tinkster 04-26-2010 05:10 PM

Quote:

Originally Posted by webhope (Post 3948582)
Yes thanx

Yet I have problem with the awk.

Code:

uuid=$( echo $block_new | awk --re-interval '/^.*UUID=/{ x=gensub(/([0-9a-f-]{36,36}).*/,"\\1","g");  print x}')
or
uuid=$( echo $block_new |  awk --re-interval '/^.*UUID=/{ x=gensub(/UUID=([^ ]+).*/,"\\1","g");  print x}')
or
uuid=$( echo $block_new | awk --re-interval '/.*UUID=/{ x=gensub(/([0-9a-f-]{36,36}).*/,"\\1","g");  print x}')
or
uuid=$( echo $block_new |  awk --re-interval '/.*UUID=/{ x=gensub(/UUID=([^ ]+).*/,"\\1","g");  print x}')

Both give
Code:

kernel (hd0,2)/boot/vmlinuz BOOT_IMAGE=linux root=UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c
Why cannot to cut out the part before UUID= ?

Did you see the changes to your match in my post above?

Code:

{ x=gensub(/.*([0-9a-f-]{36,36}).*/,"\\1","g"); print x}


All times are GMT -5. The time now is 10:27 AM.