LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-26-2010, 01:34 PM   #1
webhope
Member
 
Registered: Apr 2010
Posts: 184

Rep: Reputation: 30
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}')
 
Old 04-26-2010, 02:26 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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
 
Old 04-26-2010, 02:27 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

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

Last edited by pixellany; 04-26-2010 at 02:28 PM.
 
Old 04-26-2010, 02:38 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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!
 
Old 04-26-2010, 02:45 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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...
 
Old 04-26-2010, 03:11 PM   #6
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
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.

Last edited by webhope; 04-26-2010 at 03:15 PM.
 
Old 04-26-2010, 03:30 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
Old 04-26-2010, 03:46 PM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by webhope View Post
I wait till awk solution.
Even if SED is better?......
 
Old 04-26-2010, 03:47 PM   #9
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
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?

Last edited by webhope; 04-26-2010 at 03:53 PM.
 
Old 04-26-2010, 03:56 PM   #10
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by pixellany View Post
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=....

Last edited by webhope; 04-26-2010 at 04:00 PM.
 
Old 04-26-2010, 04:01 PM   #11
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by webhope View Post

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.
 
Old 04-26-2010, 04:18 PM   #12
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by pixellany View Post
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?

Last edited by webhope; 04-26-2010 at 04:20 PM.
 
Old 04-26-2010, 04:32 PM   #13
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by webhope View Post
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?
 
Old 04-26-2010, 04:44 PM   #14
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by pixellany View Post
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= ?
 
Old 04-26-2010, 05:10 PM   #15
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by webhope View Post
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}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Migrate Regexp from SED to AWK cgcamal Programming 9 04-23-2010 10:32 PM
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 02:18 AM
awk regexp for one character match nemobluesix Linux - General 7 02-16-2009 10:50 PM
Volume has problems including no uuid in /dev/disk/by-uuid abejarano Linux - Hardware 3 12-31-2008 08:41 PM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration