Sponsored Content
Full Discussion: Perl script solution
Top Forums Shell Programming and Scripting Perl script solution Post 302423157 by webhope on Thursday 20th of May 2010 04:23:02 PM
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..
 

5 More Discussions You Might Find Interesting

1. 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

2. 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

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. 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

5. 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
XML::LibXML::XPathExpression(3) 			User Contributed Perl Documentation			   XML::LibXML::XPathExpression(3)

NAME
XML::LibXML::XPathExpression - XML::LibXML::XPathExpression - interface to libxml2 pre-compiled XPath expressions SYNOPSIS
use XML::LibXML; my $compiled_xpath = XML::LibXML::XPathExpression->new('//foo[@bar="baz"][position()<4]'); # interface from XML::LibXML::Node my $result = $node->find($compiled_xpath); my @nodes = $node->findnodes($compiled_xpath); my $value = $node->findvalue($compiled_xpath); # interface from XML::LibXML::XPathContext my $result = $xpc->find($compiled_xpath,$node); my @nodes = $xpc->findnodes($compiled_xpath,$node); my $value = $xpc->findvalue($compiled_xpath,$node); $compiled = XML::LibXML::XPathExpression->new( xpath_string ); DESCRIPTION
This is a perl interface to libxml2's pre-compiled XPath expressions. Pre-compiling an XPath expression can give in some performance benefit if the same XPath query is evaluated many times. "XML::LibXML::XPathExpression" objects can be passed to all "find..." functions "XML::LibXML" that expect an XPath expression. new() $compiled = XML::LibXML::XPathExpression->new( xpath_string ); The constructor takes an XPath 1.0 expression as a string and returns an object representing the pre-compiled expressions (the actual data structure is internal to libxml2). AUTHORS
Matt Sergeant, Christian Glahn, Petr Pajas VERSION
2.0110 COPYRIGHT
2001-2007, AxKit.com Ltd. 2002-2006, Christian Glahn. 2006-2009, Petr Pajas. LICENSE
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.18.2 2014-02-01 XML::LibXML::XPathExpression(3)
All times are GMT -4. The time now is 11:09 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy