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-30-2010 03:49 PM

Where is problem:

Code:

echo $block_new | awk --re-interval '{sub("'$hd'","'$uuid'","g") } $0'
Nothing outputs. It should output the $block_new with hd to uuid replaced.

grail 04-30-2010 11:54 PM

Awk sub command:
Code:

sub(regexp, replacement [, target])
All of this - '{sub("'$hd'","'$uuid'","g") } $0' - needs to be in the above format
Assuming you wish to do a global substitution (ie the "g" in your code), you can use:

Code:

gsub(regexp, replacement [, target])
Also, --re-interval is not required here

webhope 05-01-2010 01:21 AM

Quote:

Originally Posted by grail (Post 3953580)
Also, --re-interval is not required here

$hd includes gensub qualifier interval.

Edit:
How should be the regexp?
Code:

/hd\\(0,2\\)/
or
Code:

/hd\(0,2\)/
?


awk: warning: escape sequence `\(' treated as plain `('

Edit
I think that the double escaped backslash should be ok:
Code:

hd=${hd//(/\\\\(}; # left bracket escape
hd=${hd//)/\\\\)}; # right bracket escape

because this
Code:

hd=${hd//(/\\(}; # left bracket escape
hd=${hd//)/\\)}; # right bracket escape

returns awk: warning: escape sequence `\(' treated as plain `('

Edit:
But through ... nothing been replaced :-/

The block output still looks like:

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
And this was a try to replace (hd0,2) to uuid

grail 05-01-2010 04:25 AM

So now you are putting UUID at the front of "/boot/vmlinuz"?

Again I would ask if you have tested that this will actually work, ie is having UUID there plausible, have you tested it?

This worked for me without errors:
Code:

echo "(hd0,2)/boot/vmlinuz" | awk 'gsub(/\(hd0,2\)/,"uuid")'

webhope 05-01-2010 06:25 AM

Quote:

Originally Posted by grail (Post 3953681)
So now you are putting UUID at the front of "/boot/vmlinuz"?

Again I would ask if you have tested that this will actually work, ie is having UUID there plausible, have you tested it?

This worked for me without errors:
Code:

echo "(hd0,2)/boot/vmlinuz" | awk 'gsub(/\(hd0,2\)/,"uuid")'

What is "ie"?
I didn't test. Never mind. Grub boot manager has a editor to change boot item before booting.

Problem is elsewhere. NOT hd(0,2) BUT (hd0,2) - my fault!

catkin 05-01-2010 06:30 AM

Quote:

Originally Posted by webhope (Post 3953740)
What is "ie"?

Strictly i.e., it is short for the Latin expression id est meaning "that is".

webhope 05-01-2010 06:33 AM

One more problem in the regex replace. This is the effect: UUID=-4033-55

Code:

++ echo title Sata Mandriva kernel '(hd0,2)/boot/vmlinuz'
++ awk '{gsub(/\(hd0,2\)/,"UUID="eab515e9-bc3e-4024-9f01-55fddaa0fb1c"") } $0'
+ block_new='title Sata Mandriva kernel UUID=-4033-55/boot/vmlinuz

???

webhope 05-01-2010 06:34 AM

Quote:

Originally Posted by catkin (Post 3953742)
Strictly i.e., it is short for the Latin expression id est meaning "that is".

Quite confusing. Because IE means Internet Explorer

catkin 05-01-2010 06:37 AM

Quote:

Originally Posted by webhope (Post 3953745)
Quite confusing. Because IE means Internet Explorer

And Indo-European and Industrial Engineer(ing)!

EDIT: but none of these was credible in context :hattip:

grail 05-01-2010 07:20 AM

Quote:

"UUID="eab515e9-bc3e-4024-9f01-55fddaa0fb1c""
Quotes around the actual UUID are not required, should be:

Code:

"UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c"
Also, sorry about the "ie." thing, it is quite common place in english the same as "eg." to mean for example.

On the other hand not nearly as confusing as the czech in some of your posts :)

webhope 05-01-2010 08:13 AM

OK. Now I try to assign the $block_new back to the content:

Code:

content[$i]=$block_new;
echo $content;

Strange is the output. It looks like the $content was overwriten by $block_new:

Code:

title Sata Mandriva kernel (hd0,2)/boot/vmlinuz BOOT_IMAGE=linux root=(hd0,2) resume=UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798 splash=silent vga=788 initrd (hd0,2)/boot/initrd.img
The content of $content. We should see alll block of menu.lst keeped in memory.

But in real, $block_new should be placed to content[0]

grail 05-01-2010 09:03 AM

I think you better have a look at some bash programming sites and how arrays work.

http://tldp.org/LDP/abs/html/arrays.html

webhope 05-01-2010 09:16 AM

Well this works fine
Code:

echo ${content[*]};

grail 05-01-2010 09:26 AM

Correct ;)

webhope 05-01-2010 11:50 AM

I did this:

If uuid is not found in blkid list, then:

Code:

echo "Enter device:"; echo "Pattern: (hd0,0)"; read hd
[[ "$(echo $hd | od -A n -t dC)" -eq 10 ]] && hd="hd(0,0)"

Nice way how to use od


All times are GMT -5. The time now is 07:50 PM.