LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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, 09:36 PM   #16
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191

Assuming the format remains the same for all stanzas you could use:
Code:
echo "$block_new" | awk '/UUID/{$4="root=(hd0,2)"}1
 
Old 04-27-2010, 01:58 AM   #17
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
Tinkster

Quote:
Originally Posted by Tinkster View Post
Did you see the changes to your match in my post above?

Code:
{ x=gensub(/.*([0-9a-f-]{36,36}).*/,"\\1","g"); print x}
Yes:
Code:
uuid=$( echo $block_new | 
awk --re-interval '/.*UUID=/{ x=gensub(/([0-9a-f-]{36,36}).*/,"\\1","g");  print x}'
echo $uuid; read
Gives:
Code:
title Sata Mandriva
e12487ff-6d6f-44c4-9e03-33f545b3b798
initrd (hd0,2)/boot/initrd.img
Instead:
e12487ff-6d6f-44c4-9e03-33f545b3b798

But How can you be sure it gives correct number? I need to specify what (or what type of device) is there. So "UUID=" before hexadecimal digit expression.

Next thing - in this case I need to get uuid, not whole block

Grail

Quote:
Originally Posted by grail View Post
Assuming the format remains the same for all stanzas you could use:
Code:
echo "$block_new" | awk '/UUID/{$4="root=(hd0,2)"}1
What is sense? It doesn't do what I need. I repeat, one of these expressions:

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

    uuid=$( echo $block_new |  awk '/.*UUID=/{ x=gensub(/UUID=([^ ]+).*/,"\\1","g");  print x}')
    echo $uuid; read
Outputs:
Code:
kernel (hd0,2)/boot/vmlinuz BOOT_IMAGE=linux root=UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c
# // Pressed Enter //
kernel (hd0,2)/boot/vmlinuz BOOT_IMAGE=linux root=eab515e9-bc3e-4024-9f01-55fddaa0fb1c
So I need to remove the preceding part: "kernel (hd0,2)/boot/vmlinuz BOOT_IMAGE=linux root="

Last edited by webhope; 04-27-2010 at 02:07 AM.
 
Old 04-27-2010, 02:14 AM   #18
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I am sorry, I thought the point was to replace the current root=UUID=<blah> with root=(hd0,2):
Quote:
I wanted to change
root=UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c
to
root=(hd0,2)
 
Old 04-27-2010, 02:30 AM   #19
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by grail View Post
I am sorry, I thought the point was to replace the current root=UUID=<blah> with root=(hd0,2):
I tryied to explain situation. But first I need to get correct uuid. I don't know location of system at this point. I will replace it later. Can you explain me where is the fault my awk pattern doesn't cut out (remove) beginning?

Last edited by webhope; 04-27-2010 at 02:50 AM.
 
Old 04-27-2010, 06:01 AM   #20
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
I forgot to put there .* before UUID= in the gensub() fnc

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

uuid=$( echo $block_new |  awk '/.*UUID=/{ x=gensub(/.*UUID=([^ ]+).*/,"\\1","g");  print x}')
echo $uuid; read
Thanx to Tinkster and Pixellany

Last edited by webhope; 04-27-2010 at 06:03 AM.
 
Old 04-27-2010, 06:06 AM   #21
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Quote:
Originally Posted by pixellany View Post
PS:
Does anyone know if a uuid always has the same number of characters? If so, that could be incorporated into the regex.
Look at the uuid of a fat32 filesystem. It will be short.

Here are examples for uuids from the /dev/disk/by-uuid/ directory on my laptop. The first one is from an NTFS filesystem. The second is for a fat32 filesystem. A Linux filesystem or swap partition follows the last pattern.
Code:
145266D35266B95E
2CBD-5986
5783d30e-8dc4-447a-80f4-8952129e9d9e

Last edited by jschiwal; 04-27-2010 at 06:13 AM.
 
Old 04-27-2010, 07:08 AM   #22
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
I found one more problem (or two):

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
In these code are two uuids. It shows the last one (which is not on my system) - e12487ff-6d6f-44c4-9e03-33f545b3b798 ....

Code:
    uuid=$( echo $block_new |  awk '/.*UUID=/{ x=gensub(/.*UUID=([^ ]+).*/,"\\1","g");  print x}')
echo $uuid

Therefor I need to give all uuid to one string separeted by space.

Do you know how to do it?

This is little more complicated.

Last edited by webhope; 04-27-2010 at 07:11 AM.
 
Old 04-27-2010, 07:37 AM   #23
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So I will ask again, is there any reason to think the start will be different from:
Quote:
kernel (hd0,2)/boot/vmlinuz BOOT_IMAGE=linux root=UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c
ie it might not be the same words exactly but will there be 4+ fields in this type of order?

I do use grub, but am not sure of all the possible combinations?

If it is always to be 4+ with root in specified position then do a sub on $4 and return it.
 
Old 04-27-2010, 07:52 AM   #24
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
Grail,
I don't know what you mean "4+ fields"?
Do you speak about the resume argument?

"I do use grub, but am not sure of all the possible combinations?"
I use configuration of Mandriva Linux auto generated.

Originaly I wanted awk pattern to return uuid.
But now situation is such that I would reather return all uuids to one string. Then I will check if the uuids is in blkid output. I will replace the existing UUID=uuid to hd(?,?). I will reinstall my system and change target partitions. So this script will be usefull after reinstalling system.
 
Old 04-27-2010, 08:04 AM   #25
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
I don't know what you mean "4+ fields"?
Code:
field_1=kernel 
field_2=(hd0,2)/boot/vmlinuz 
field_3=BOOT_IMAGE=linux 
field_4=root=UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c
field_+=...
So if we know we always will have at a minimum, 4 fields in this order then we can simply deal with field $4 or if you want resume as well and it is always
there then field $5.

I have found the simplest way to get the first uuid to be 2 simple subs:
Code:
block_new=$( echo "$block_new" | awk '/UUID/{sub(/.*root=UUID=/,"");sub(/ .*/,"");print}')
 
Old 04-27-2010, 08:30 AM   #26
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by grail View Post
So if we know we always will have at a minimum, 4 fields in this order ...
No. This is changable. In menu.lst are more systems and configurations, depending on installer of distribution. So this is not sure. Therefor I want all uuids to one string. Then I will do some type of replace with help of loop.

Quote:
Originally Posted by grail View Post
I have found the simplest way to get the first uuid to be 2 simple subs:
Code:
block_new=$( echo "$block_new" | awk '/UUID/{sub(/.*root=UUID=/,"");sub(/ .*/,"");print}')
This is interesting (clever), but as I say. There can be situation, that there will be more uuids on one line and in block, as installer creates such configuration. So putting all uuids to one string "... ... ... ..." will be better to fix all possibilities.

Last edited by webhope; 04-27-2010 at 08:31 AM.
 
Old 04-27-2010, 09:02 AM   #27
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Code:
#!/bin/bash

block_new="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"

block_new=($(echo "$block_new" | awk 'BEGIN{RS="UUID="}/-/{gsub(/ .*/,"");print}'))

for x in ${block_new[@]}
do
	echo "|$x|"
done
 
Old 04-27-2010, 10:12 AM   #28
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
Thank you, I'll write later.
 
Old 04-28-2010, 02:52 AM   #29
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by grail View Post
Code:
#!/bin/bash

block_new="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"
I continue. I have this code. In case that 1 of uuids is not found in your system it should echo warning. But the if cond. is not right. What is in the d var if awk nothing found?

Code:
uuid=($(echo "$block_new" | awk 'BEGIN{RS="UUID="}/-/{gsub(/ .*/,"");print}'))
for d in ${uuid[@]}; do
  dev=$(blkid | grep $d |  awk 'BEGIN{FS=":"} {print $1}');
  if [[ "$dev" != "" ]] && [[ "$dev" != "\n" ]]; then
  echo $dev
  else
  echo "uuid $uuid not found in list of devices"
  fi;
done

Last edited by webhope; 04-28-2010 at 03:42 AM.
 
Old 04-28-2010, 03:16 AM   #30
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
What is in the d var if awk nothing found?
The for loop is immediately exited as the array has no items to step through.
If you put an echo after done, this will be the only thing on screen.
 
  


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:29 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