LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   perl check consecutive lines (https://www.linuxquestions.org/questions/programming-9/perl-check-consecutive-lines-4175552199/)

fritz001 08-31-2015 03:19 AM

perl check consecutive lines
 
Hello,

I'm having quite a issue trying to implement this:

i have one file < xml file >

I want to read the file and the check 2 consecutive lines :

if line N contains <resources> and line N+1 contains <definitions>

I haveeeeee no idea how to do it ...

Any advice would be appreciated .

pan64 08-31-2015 03:21 AM

What have you tried so far? Why do you want to use perl? Is this a homework or ???

fritz001 08-31-2015 03:27 AM

It's a script I'm using at work and due to some restrains it's the only valid solution I have.

I'm not allow to install ANY perl modules, if you want to suggest something .

pan64 08-31-2015 03:29 AM

you do not need to install anything, but we cannot help you to extend your script without knowing it.

fritz001 08-31-2015 03:47 AM

well, the whole idea is:

I have a file that contains

<resources>
<script file="/etc/cluster/rgppgs0525.ksh" name="script1"/>

Now, I want to verify if line N contains word "resource" and exactly next one ( line N+1 ) contains word "script"

in my case, as I said , need to verify exactly line N and line N+1

grail 08-31-2015 08:02 AM

Well I think the first suggestion would be to either seek help from your manager if they have told you Perl is the only option and you clearly know nothing about it or if you would
like to learn more yourself and figure it out, I would suggest you google for how to read a file line by line in Perl and go from there.

fritz001 08-31-2015 08:16 AM

grail, buddy, i don't know who are you, i don't care who are you, i dont\t know what you know, but I know something for sure, if you have some knowledges and "willing to help" someoone at least you can give some hint.

Now, i'm just trying to be polite... and if you don't understand my question I can can explain to you once more, or even i can draw.

pan64 08-31-2015 08:21 AM

the problem is you gave almost no information, you just want to have a solution.
You ought to read the guidelines of this forum. We won't write you this program, but will gladly help you to complete it. So please show what you have already....
Otherwise the best hint we can give you is just google, there are a lot of examples around there about how to read a file line by line and check the lines if they matches...

fritz001 08-31-2015 08:42 AM

I think I've managed to do it

while (<$fhIn>) {

if ($. == 2) { s/(\d+)/1+$1/ge; }

foreach my $se (sort keys data) {

if (/<resources>/){
last;
}
if (/script file/) {print $fhOut "definition$N\n" if /script file/;
} else {
print $fhOut "definition$N\n" if /01_lv001/;}

}


Ok, just to resume :


I have one xml ( config file like this)

empty one


</failoverdomains>
<resources>
<script file="/etc/x212.sh" name="script1"/>
<ip address="10.51.18.220/23" monitor_link="1" sleeptime="2"/>
</resources>


When is empty I just wan to add these line right after " script file"


</failoverdomains>
<resources>
<definition01 lv=01_lv001>
<definition02 lv=01_lv002>
<script file="/etc/x212.sh" name="script1"/>
<ip address="10.51.18.220/23" monitor_link="1" sleeptime="2"/>
</resources>


but let's say I do already have these line with definition, Now i just want to add deffinition3, deffinition4, etc...

fritz001 08-31-2015 08:47 AM

hope, I was a bit clear now :)

pan64 08-31-2015 12:15 PM

please use [code]here comes your code[/code] to keep formatting.
Also you can use variables in your script, to "remember" things. So you can set for example a variable $found if something was found and add lines at the end if $found is still zero (or undefined)

But I do not really understand what do you want to achieve

rtmistler 08-31-2015 12:48 PM

Quote:

Originally Posted by fritz001 (Post 5413651)
I think I've managed to do it
...
but let's say I do already have these line with definition, Now i just want to add deffinition3, deffinition4, etc...

Not sure if what you wrote serves your original intent, however you say it does. Therefore good.

That last addition is what I call "feature creep", because now it sounds as if instead of your original request where you detect a line with <resources> and then a following line with <script, you're now saying that if you have these <definition1 and <definition2 terms, in that case you now wish to add terms for <definition3 and <definition4.

The first overall problem is that you stated a problem and did not show your script, thereby making it difficult for someone to help you properly.

Now at this point you've added to the requirements, or you've created confusion because it's difficult to determine what criteria you are looking for and what possible results you are looking to attain.

What would help is if you were to examine the total requirements and summarize them better.

Obviously what further helps if when you post your script or code along with the problem description so that people who understand the particular language can offer their assistance.

fritz001 08-31-2015 01:54 PM

Well, rofl, even me I have problems to understand what I said last time :)

Ok, so, here is what I'm trying to achieve:


i have a config file:

vg.ini

Code:

[vg01]
01_lv001=mounted
02_lv002=mounted


[vg02]
02_lv001=dismount
02_lv002=mounted

and and xml file

when the xml file does not contain any valid data will have something like this :

Code:

<resources>
<script file="/etc/cluster/rgppgs0525.ksh" name="script1"/>


an working xml file will be like :

Code:

<resources>
<vgfs=vg01/lv01_001>
<vgfs=vg01/lv01_002>
<vgfs=vg02/lv01_001>
<vgfs=vg02/lv01_001>
<script file="/etc/cluster/rgppgs0525.ksh" name="script1"/>

<status definition>
<mount status: 01_lv001/mounted>
<mount stauts: 01_lv002/mounted>
<mount status: 02_lv001/dismount>
<mount stauts: 02_lv002/mounted>
<resource definition>

Now, If I want to add let's say vg03/04

I'd like to have something like

Code:

<resources>
<vgfs=vg01/lv01_001>
<vgfs=vg01/lv01_002>
<vgfs=vg02/lv01_001>
<vgfs=vg02/lv01_001>
<vgfs=vg03/lv01_001>
<vgfs=vg03/lv01_001>
<vgfs=vg04/lv01_001>
<vgfs=vg04/lv01_001>

<script file="/etc/cluster/rgppgs0525.ksh" name="script1"/>

<status definition>
<mount status: 01_lv001/mounted>
<mount stauts: 01_lv002/mounted>
<mount status: 02_lv001/dismount>
<mount stauts: 02_lv002/mounted>
<mount status: 03_lv001/mounted>
<mount stauts: 03_lv002/mounted>
<mount status: 04_lv001/mounted>
<mount stauts: 04_lv002/mounted>
<resource definition>

I do have issues to process both situations : when xml file is empty and when already contains some data

I\ll try tomorrow to post full script



Well, I think now it's a better description of what i\, trying to do

fritz001 09-01-2015 02:24 AM

Code:


my $filename = shift or die "Usage: $0 filename\n";
open my $fh, '<', $filename or die "Could not open '$filename' $!";

while (my $line = <$fh>) {

    next  if $line =~ /^\#/ || !$line; #skip blanks && commented

    if ($line =~ /^\[(.*)\]\s*$/) {
        $section = $1;
        next;
    }

    if ($line =~ /^([^=]+?)\s*=\s*(.*?)\s*$/) {
        my ($field, $value) = ($1, $2);
        if (not defined $section) {
            warn "Line outside of seciton '$line'\n";
            next;
        }
        $data{$section}{$field} =  $value;
    }
}

open (my $fhIn,  '<', 'cl.xml')      or die $!;
open (my $fhOut, '>', 'cl.xml.new')  or die $!;


while (<$fhIn>) {

foreach my $se (sort keys data) {

        foreach my $param (sort keys {$data{$se}}) {
        my $mp=$data{$se}->{$param};

        if (/<resources>/){
                last;
        }
        if (/script file/) {print $fhOut  "\t\t\t<vgfs=$se/${mp}\" >\"/>\n" if /script file/;
        } else { print $fhOut  "\t\t\t<lvgfs=$se/${se}_$num\"/>\n" if /lv01_001/; }

}
}


fritz001 09-02-2015 11:24 AM

SOLVED!

Some hints from perlmonks helped me quite a lot !!


All times are GMT -5. The time now is 11:11 PM.