Perl script solution


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl script solution
# 8  
Old 05-17-2010
It is stil not ideal, I send you some other forms if input text:
Code:
block='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'

hd0,2 instead (hd0,2)

Code:
block='title Sata Mandriva
kernel (UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798)/boot/vmlinuz BOOT_IMAGE=linux root=(UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798) resume=UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798 splash=silent vga=788
initrd (hd0,2)/boot/initrd.img'

It deletes some spaces: products "linuxroot" instead "linux root"

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

Products "/boot(UUID=" instead "/boot (UUID="
and "/boot" instead "/boot (UUID=eab515e9-"
Edit: I wanted to specify that I need to remove something (UUID=...) that follows the kernel or initrd word.

In my original code I tried to identify UUID format \(UUID=[-0-9a-f]*\ not to be confused with block list (hd?,?). (I am not sure if UUID can have big letters)

I think it is not lucky: sub(/[()]/,"",$2) it removes the parantheses whatever it it uuid od hd. Or Map! map (hd0) (hd1)

I want to ask you about this: r=$3$4; does it mean, you rely on IFS=$' '? How we get $3 and $4 ?

Last edited by webhope; 05-17-2010 at 12:26 PM..
# 9  
Old 05-17-2010
Quote:
I want to ask you about this: r=$3$4; does it mean, you rely on IFS=$' '?
I used the default FS of awk which is [:space:]+ or [ \t]+. I did not touch shell's IFS.

Quote:
I think it is not lucky: sub(/[()]/,"",$2) it removes the parantheses whatever it it uuid od hd. Or Map! map (hd0) (hd1)
You posted an excerpt and I responded to it - if there is more cases of what can be in your menu.lst, then you have to post them Smilie

It is difficult when there is a lot of different things coming new with every new example of your input.

Best might be to just post all different examples and possibilities and according to that your expected output. Else it will never be completed Smilie
# 10  
Old 05-17-2010
Quote:
Originally Posted by zaxxon
It is difficult when there is a lot of different things coming new with every new example of your input.

Best might be to just post all different examples and possibilities and according to that your expected output. Else it will never be completed Smilie
Sorry. It is always hard to me to explain what I work with and where I am going to. I always try to simplify things therefor I 1st publish the simplest form... To better understand. But I see this doesn't work and it is just on the contrary... Smilie
# 11  
Old 05-17-2010
No worries - as said, best try to gather what you want to do with a complete example of the input (describing cases etc. with comments) and a complete example of the wanted output.
There is a lot of more people here in the forum which will be able to help for sure - having a break from work for today, see you tomorrow Smilie
# 12  
Old 05-17-2010
from awk to perl

I'd like to do a remake.

It is good you showed me how to identify the 1st found result of (kernel|init).
Now I would need to access the content of parenthesis (.*) by some referencer.

I will do a remake. In this example I use //1 as a referencer (it is kust to show where I want to access the captured word).

I have this code:
Code:
/^(kernel|initrd)( +)(\(UUID=[-0-9a-f]*\)?)(.*)/ && f!=1 {f=1;
                    print "uuid"; print \\1;
                    next }
/^(kernel|initrd) / && f==1 {
                    next }1' f=0

Is some referencer to access the parenthesis? The FS - spaces and $1,$2,$3 referencer is a problem for me. I would better not use it. I don't know if the \\1 referencer is only for gensub? If I could use this I would use print \\1\\2\\3\\4 to print the content of parenthesis.

---------- Post updated at 08:54 PM ---------- Previous update was at 08:07 PM ----------

I have found interesting thread about storing data and backreferences . So I don't know if is it possible to do it in awk. Maybe if somebody would do it in perl.

storing data and backreferences

Last edited by webhope; 05-20-2010 at 05:27 PM..
# 13  
Old 05-20-2010
In perl

Hi, here I am again. I have learned some basic about perl and I am bringing a solution. If zou know better way how to do it in perl, show it to me. I am newbie to perl :-)

The text I work with 3 lines:
Code:
block='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\ninitrd (UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c)/boot/initrd.img'

Now it is on one line, but later I get it to array with 3 rows.


---------- Post updated at 10:23 PM ---------- Previous update was at 06:00 PM ----------

Step A) This code removes 1st UUID=... from the line with kernel (or initrd) and move it to newline as uuid UUID=...
1) I needed to recognize if the initrd or kernel was found - therefor the step A was done now.
2) If step A is done, then I do second part. I print the initrd without UUID and without "uuid"
B) I do increment $n++ for setting the array in which I have the lines.
C) ($1 eq "") is necessary to recognize if kernel or initrd was find. If not, then I need to put original line as value to the array.

Code:
block=$(
echo "$block" | perl -e '
$/="\\n";
chomp (@ia=<STDIN>);
foreach $i (@ia) {
  $n++; @p[$n]=$i;

  if ($q!=1)  
    {
    $i =~ /^(kernel|initrd)( +)(\(UUID=[-0-9a-f]*\)?)(.*)/ ;
    if ($1 eq "") { @p[$n]=$i; } else {   # if it is not kernel nor initrd I have to set the original value for this line
    $u=$3;
    $q=1;
    $a="$1 $2$4\n";
    $u =~ s/[()]//g;
    @p[$n]="\\nuuid $u\\n$a\\n";
    } 
    } elsif ($q==1) { $q=2; }

  if ($q==2)  
    { 
    $i =~ /^(kernel|initrd)( +)(\(UUID=[-0-9a-f]*\)?)(.*)/ ; 
    if ($1 eq "") { @p[$n]=$i; } else {   # if it is not kernel nor initrd I have to set the original value for this line
    $b="$1 $2$4\n"; 
    @p[$n]="$b\\n";
    }
    }; 
}
print @p; $q=0;
' 
);


Last edited by webhope; 05-20-2010 at 05:22 PM..
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