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/)

grail 05-01-2010 11:47 PM

Do you happen to know what the "10" is referring to???
Try the following and it will show you which characters you are working with:
Code:

echo "(hd0,0)" | od -A n -t c
You will notice that the piece of code you have written never fails.

webhope 05-02-2010 02:43 AM

What is it good for? I see characters in output
echo <string>

Edit:
Now I am going to write a script save the blocks. You know that I operate with block separately not with whole file. There for all items/blocks from array content I need to proceed through for loop and then do a search of bock and replace. Is it possible to do such search in sed? I now that sed has problems with multiline searches. It's like to search something like this:

line 1 title \n
line 2 ... \n
line 3 ... \n

and replace it...

I would better prefer to identify hole block so ... if in file exists more same titles but with different uuid, it will not be overwritten because of identical titles, but he would reconize that the next line is different from the pattern....

grail 05-02-2010 03:28 AM

Quote:

What is it good for?
What you were supposed to see is that the 10 you refer to is the value for "\n" which will ALWAYS be at the end of an echo.
This therefore makes your test useless as it will NEVER be wrong.

Yes you can do multiline edits in sed but you will need to be careful about how you go about it.

Personally I think you need to step back and rethink what you are trying to do.
Also, as identified on several occasions, you need to test a lot more of what you are trying to do before going ahead.
Currently you are trying to change fields which may or may not actually work/be allowed.

Most of all you need to try and build your script a line at a time by trialling what you want to do on the command line first
and see what the results are. This process would save a lot of the stoppages you have been running into.

webhope 05-02-2010 03:57 AM

Quote:

Originally Posted by grail (Post 3954439)
Currently you are trying to change fields which may or may not actually work/be allowed.

Actually it looks that the array asignment work well...

Quote:

Originally Posted by grail (Post 3954439)
Most of all you need to try and build your script a line at a time by trialling what you want to do on the command line first
and see what the results are. This process would save a lot of the stoppages you have been running into.

I guess this will be complicated?

Edit:
Quote:

Originally Posted by grail (Post 3954439)
Yes you can do multiline edits in sed but you will need to be careful about how you go about it.

From your answer I am still not sure ... does it mean the replace pattern for sed is standard? Or the process will be more complicated? And do you think I need call sed for every single block from the $content variable? Or can sed accept a array ($content) and to do such loop and process every item in array automatically. Now I am prepared to do a while cycle for the sed.

grail 05-02-2010 05:19 AM

Quote:

Actually it looks that the array asignment work well...
Actually, I was referring to the changes you are making in the menu.lst not having been tested.

Quote:

From your answer I am still not sure ... does it mean the replace pattern for sed is standard? Or the process will be more complicated? And do you think I need call sed for every single block from the $content variable? Or can sed accept a array ($content) and to do such loop and process every item in array automatically. Now I am prepared to do a while cycle for the sed.
It may be possible to send the value of the relevant part of the array to sed, but as i have said before, I would be changing the uuid once and sending that to sed as root, for example, would typically always be the same.
As with my previous post, really the best way to see what will work is at the command line on a test scenario.

webhope 05-03-2010 02:55 AM

I found something unsolved yet in the script. At the beginning needed to be a code that decides about way of conversion. It means for all blocks I need to convert from uuid to hd or from hd to uuid. This thing I forgot. Now I created this code:

Code:

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"

((fst++)); [[ $fst -eq 1 ]] && first=$(echo "$block_new" | awk '/(.*UUID=.*)|(\(hd)/ {print $1}')
echo $first;

In the $1 should be the first found "hd(" or "UUID". It works good if it is just one line. But if it is more lines of block, than it failes. Can you tell me how to capture only 1 result? It means either "hd(" or "UUID" to $1? Thanks

Edit:
One more awk. This code should escaped characters of class [/+()[]]
Code:

block_new="  /  +  (      ) [ ] " 
p=$( echo $block_new |  awk '{ x=gensub(/([/+()[]])/,"//\\1","g");  print x}')
echo $p

also not working :-(

grail 05-03-2010 05:09 AM

For your first problem set a variable and once it is a value do not print anymore.

Second problem:
1. /.*/ - this is a complete waste as once again, refer to previous posts, it is ALWAYS true so not required
2. What do you expect from echo $p?

webhope 05-03-2010 05:17 AM

Edit 1:
Quote:

Originally Posted by grail (Post 3955464)
For your first problem set a variable and once it is a value do not print anymore.

But the problem is that it
Code:

awk '/(UUID=)|(\(hd)/'
returns whole line instead just "UUID" value or "hd(" . I need this two possibilities for if condition.

Edit 2:
Quote:

Originally Posted by grail (Post 3955464)
2. What do you expect from echo $p?

It was just a text to escape some characters. I wanted to prepare pattern for sed.

Code:

p=$( echo $block_new |  awk '{ x=gensub(  /([/+()[]])/  ,"//\\1","g");  print x}')
echo $p

Still the same problem (nothing escaped)


All times are GMT -5. The time now is 01:38 PM.