Perl script solution


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl script solution
# 1  
Old 05-17-2010
Hammer & Screwdriver Perl script solution

Hi
I need to do this thing in awk (or perl?). I try to find out how can I identify 1st and 2nd result from the OR expression in gensub:

Code:
block='title Sata Mandriva
kernel /boot/vmlinuz
initrd /boot/initrd.img'
echo "$block" | awk '{ x=gensub(/(kernel|initrd) /,"\\1XXX","g"); print x }'

Generates this:
Code:
title Sata Mandriva
kernelXXX/boot/vmlinuz
initrdXXX/boot/initrd.img

Now I need to apply the change only on 1st found result:
Code:
title Sata Mandriva
kernelXXX/boot/vmlinuz
initrd /boot/initrd.img

Problem is that I don't know how to identify the 1st and second substitution.

Somebody help?

Last edited by zaxxon; 05-20-2010 at 11:28 AM.. Reason: User asked for changing subject
# 2  
Old 05-17-2010
Hi webhope!

What do you want exactly? To replace only the first occurrence of either `initrd' or `kernel' in the whole stdin text? Or simply replace `kernel' with `kernelXXX' in this particular variable? And what does NFR mean - may be it should be FNR (record number)?
# 3  
Old 05-17-2010
Why must it be awk?

---------- Post updated at 02:37 PM ---------- Previous update was at 02:25 PM ----------

Code:
$> cat infile
title Sata Mandriva
kernel /boot/vmlinuz
initrd /boot/initrd.img
$> echo "$block"| awk '/(kernel|initrd)/ && f!=1 {sub(/ /,"XXX"); f=1; print; next}1' f=0
title Sata Mandriva
kernelXXX/boot/vmlinuz
initrd /boot/initrd.img


Last edited by zaxxon; 05-17-2010 at 09:46 AM.. Reason: removed my own stupid comment ;)
# 4  
Old 05-17-2010
that was mistake:
not print x NFR
but
print x

I gave you simplified form of code.

Quote:
Originally Posted by zaxxon
Why must it be awk?

---------- Post updated at 02:37 PM ---------- Previous update was at 02:25 PM ----------

Code:
$> cat infile
title Sata Mandriva
kernel /boot/vmlinuz
initrd /boot/initrd.img
$> echo "$block"| awk '/(kernel|initrd)/ && f!=1 {sub(/ /,"XXX"); f=1; print; next}1' f=0
title Sata Mandriva
kernelXXX/boot/vmlinuz
initrd /boot/initrd.img

I must to explain you what I want to do.

Originally. I had something like ... kernel (hd0,2) ... root=(hd0,2) .... I replaced it with ...
Code:
kernel (UUID=theuuid) ... root=(UUID=theuuid)
initrd (UUID=theuuid)

It is not ideal but what I do in previous code is that I replace (hd?,?) for uuid and uuid for (hd?,?)

My code was:
Code:
block=$( echo "$block" | awk '{ x=gensub(/(kernel|init)( +)\(UUID=[-0-9a-f]*\)/,"uuid '$two'\\\\n\\1 ","g"); print x}' );

Original input text was like:
Code:
title Sata Mandriva\nkernel (UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c)/boot/vmlinuz BOOT_IMAGE=linux root=(UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c) resume=(UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798) splash=silent vga=788 uuid UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c\ninitrd /boot/initrd.img

Includes \n twice fot newline character.
See:
Code:
\nkernel (UUID=
\ninitrd /boot/initrd.img

I tried your code, but it result in:
Code:
titleXXXSata Mandriva kernel (UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c)/boot/vmlinuz BOOT_IMAGE=linux root=(UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c) resume=(UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798) splash=silent vga=788 uuid UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c\ninitrd /boot/initrd.img



---------- Post updated at 03:58 PM ---------- Previous update was at 03:30 PM ----------

Smilie

What I want to do:
The 1st found result from (..|..) to give on separete line with uuid:
change this:
Code:
kernel (UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c) /boot ...
initrd (UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c) /boot

to this:
Code:
uuid UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c
kernel /boot...
initrd /boot...

It is important to specify we look for kernel or initrd. "initrd (UUID=... " or "kernel (UUID=..." .
Because there also can occur words like "unhide (UUID=" or "hide (UUID=" or similar.

Last edited by webhope; 05-17-2010 at 11:08 AM..
# 5  
Old 05-17-2010
I am not sure if I understood it completely, anyway:

Code:
$> echo "$block"
kernel (UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c) /boot ...
initrd (UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c) /boot
$> echo "$block"| awk '/^(kernel|initrd) / && f!=1 {f=1; p=$1; r=$3$4; gsub(/[()]/,"",$2); print "uuid",$2; print p,r; next} /^(kernel|initrd) / && f==1 {print $1,$3; next}' f=0
uuid UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c
kernel /boot...
initrd /boot

Not sure if there are more similar blocks in this config etc. which relations to which parameters you have etc.

---------- Post updated at 04:43 PM ---------- Previous update was at 04:26 PM ----------

Changed sub to gsub - there was ) left being not substituted by " " - just in case so you notice.

Last edited by zaxxon; 05-17-2010 at 11:47 AM.. Reason: Colored some parts to show relationship and slight correction
# 6  
Old 05-17-2010
Yes, it is menu.lst
It is almost working but...

Code:
block='title Sata Mandriva
kernel (UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c) /boot ...
initrd (UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c) /boot'

echo "$block"| awk '/^(kernel|initrd) / && f!=1 {f=1; p=$1; r=$3$4; sub(/[()]/,"",$2); print "uuid",$2; print p,r; next} /^(kernel|initrd) / && f==1 {print $1,r; next}' f=0

Produces:
Code:
uuid UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c)
kernel /boot...
initrd /boot...

It should not remove the title ... and other lines ...
In fact $block doesn't need to have the kernel or init attributes. It can be block for Linux or Windows with map command. So if no kernel or initrd in it, then save it untouched.
# 7  
Old 05-17-2010
I changed the sub vs gsub for the ) that got not substituted - stated it in an update but you answered already Smilie

Try this one:
Code:
echo "$block"| awk '/^(kernel|initrd) / && f!=1 {f=1; p=$1; r=$3$4; gsub(/[()]/,"",$2); print "uuid",$2; print p,r; next} /^(kernel|initrd) / && f==1 {print $1,$3; next}1' f=0

) is substituted and the title should not be touched.
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script solution as singleline ?

Hello My script has following line and output find path -type d | awk -F "/" 'NF == 4{print $3}' path/custype=Type1/logdate=20160414 path/custype=Type11122/logdate=20160414 But I need following output that I need custtype information between "" like... (4 Replies)
Discussion started by: msuluhan
4 Replies

2. Shell Programming and Scripting

Looking for a perl-compatible regex solution

This is for PHP preg_match code - which is PCRE therefore looking for a perl compatible suggestion I have this line returned I want to match and return.. I want to match the two instances of string ending 'ABCXYZ' into an array. And on second element (ie. RootABCXYZ) only return the word... (4 Replies)
Discussion started by: deadyetagain
4 Replies

3. Shell Programming and Scripting

Expect Script - Need help to find solution

I am facing below issue with my script. below is one of the test out of 50 test from the tool which i m trying to automate. the test may show up or may not depending upon the previous results and also from the test some inputs may be asked or may not be asked depending upon previous results so you... (1 Reply)
Discussion started by: snehalb
1 Replies

4. UNIX for Dummies Questions & Answers

How to get the script corrected to get the solution

Can anyone help it out, My Requirement: Actually i grep for the items in the atrblist (Result of it will provide the line where the item present and also the next line of where it presents)then it will be stored in $i.txt From tat result i wil grep 2nd word after getdate() word and store... (2 Replies)
Discussion started by: prsam
2 Replies

5. Shell Programming and Scripting

Please suggest a script or solution?

I have to solve a programming problem for my wife who is engaged in Research in Breast Cancer. 1. She has frequently to search a long single line of alphabetic characters (lower case) for an exact match of a string. e.g.... (4 Replies)
Discussion started by: nmsinghe
4 Replies
Login or Register to Ask a Question