LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 05-24-2010, 05:24 AM   #1
webhope
Member
 
Registered: Apr 2010
Posts: 184

Rep: Reputation: 30
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;
' 
);
 
Old 05-24-2010, 05:41 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Quote:
Originally Posted by webhope View Post
Hi,
I wrote this script for bash & perl.
...
Why so ?
 
Old 05-24-2010, 06:01 AM   #3
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by Sergei Steshenko View Post
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.

Last edited by webhope; 05-24-2010 at 06:06 AM.
 
Old 05-24-2010, 06:24 AM   #4
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
Oh it has simple solution:

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

Last edited by webhope; 05-24-2010 at 06:28 AM.
 
Old 05-24-2010, 06:26 AM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Quote:
Originally Posted by webhope View Post
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.
 
Old 05-24-2010, 06:29 AM   #6
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
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
 
Old 05-24-2010, 06:31 AM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Quote:
Originally Posted by webhope View Post
I need to adjust
...
No, you need to rewrite.
 
Old 05-24-2010, 06:34 AM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,179

Rep: Reputation: 1781Reputation: 1781Reputation: 1781Reputation: 1781Reputation: 1781Reputation: 1781Reputation: 1781Reputation: 1781Reputation: 1781Reputation: 1781Reputation: 1781
@Sergei - good luck
 
Old 05-24-2010, 06:37 AM   #9
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
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

Last edited by webhope; 05-24-2010 at 07:01 AM.
 
Old 05-24-2010, 06:48 AM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Quote:
Originally Posted by grail View Post
@Sergei - good luck
 
Old 05-24-2010, 06:51 AM   #11
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,179

Rep: Reputation: 1781Reputation: 1781Reputation: 1781Reputation: 1781Reputation: 1781Reputation: 1781Reputation: 1781Reputation: 1781Reputation: 1781Reputation: 1781Reputation: 1781
Glad you feel you are progressing
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-22-2008 11:25 PM
Possible to have perl code into bash script ? frenchn00b Programming 3 07-03-2007 12:56 PM
Perl error in a Bash script rujin69 Programming 2 03-26-2007 02:40 PM
Help: Perl or Bash Script help embsupafly Programming 1 08-11-2005 06:01 PM


All times are GMT -5. The time now is 07:31 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration