LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   bash & perl - script adjustment (http://www.linuxquestions.org/questions/programming-9/bash-and-perl-script-adjustment-809748/)

webhope 05-24-2010 05:24 AM

bash & perl - script adjustment
 
Hi,
I wrote this script for bash & perl. If you run it in bash it should work. It changes
title (uuid) kernel initrd ...
to
title \nuuid UUID=the_uuid... kernel initrd ...
. When I wrote it I replaced end of lines by \n . It's the second $block definition. But now I need to repair it, because I will work with the 1st $block definition. That is not to exclude end of lines, but leave it be untouched. Now when you escape the second $block definition, the code does not work. Any idea what I have to do to repair it working with multiline input data?

Code:

block="title Sata 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
initrd (UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c)/boot/initrd.img";

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"; # ESCAPE this line

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;
'
);


Sergei Steshenko 05-24-2010 05:41 AM

Quote:

Originally Posted by webhope (Post 3979238)
Hi,
I wrote this script for bash & perl.
...

Why so ?

webhope 05-24-2010 06:01 AM

Quote:

Originally Posted by Sergei Steshenko (Post 3979245)
Why so ?

I needed to use back references . Originally I wanted to do it by awk but awk doesn't support backreferences except gensub, which is useless for this. Mayabe the code looks long but I don't know shorter solution.

webhope 05-24-2010 06:24 AM

Oh it has simple solution:

$/="\n";
instead
$/="\\n";

Sergei Steshenko 05-24-2010 06:26 AM

Quote:

Originally Posted by webhope (Post 3979263)
I needed to use back references . Originally I wanted to do it by awk but awk doesn't support backreferences except gensub, which is useless for this. Mayabe the code looks long but I don't know shorter solution.

Just Perl is sufficient for what you are doing.

webhope 05-24-2010 06:29 AM

I need to adjust the output because I generate this:

Code:

title Sata Mandriva\nuuid UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c\nkernel  /boot/vmlinuz BOOT_IMAGE=linux root=UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c resume=UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798 splash=silent vga=788
\ninitrd  /boot/initrd.img
\n

Instead:

Code:

title Sata Mandriva
uuid UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c
kernel  /boot/vmlinuz BOOT_IMAGE=linux root=UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c resume=UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798 splash=silent vga=788
initrd  /boot/initrd.img


Sergei Steshenko 05-24-2010 06:31 AM

Quote:

Originally Posted by webhope (Post 3979286)
I need to adjust
...

No, you need to rewrite.

grail 05-24-2010 06:34 AM

@Sergei - good luck

webhope 05-24-2010 06:37 AM

I have it!

Code:

block="title Sata 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
initrd (UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c)/boot/initrd.img";
#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";

block=$(
echo "$block" | perl -e '
$/="\n"; # $/="\\n";
@ia=<STDIN>; # 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";
        $u =~ s/[()]//g;
        @p[$n]="uuid $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";
        @p[$n]="$b\n";
        }
    };
}
print @p; $q=0;
'
);

I just adjusted this:
1) separator $/="\n";
2) remove chomp()
3) removed some \n and changed \\n to \n

And not the ouput is correct:

Code:

title Sata Mandriva
uuid UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c
kernel  /boot/vmlinuz BOOT_IMAGE=linux root=UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c resume=UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798 splash=silent vga=788
initrd  /boot/initrd.img

Just a pity I can't post thanks to myself :cry:

Sergei Steshenko 05-24-2010 06:48 AM

Quote:

Originally Posted by grail (Post 3979292)
@Sergei - good luck

:D:D:D

grail 05-24-2010 06:51 AM

Glad you feel you are progressing :)


All times are GMT -5. The time now is 06:27 AM.