LinuxQuestions.org
Review your favorite Linux distribution.
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
  Search this Thread
Old 08-31-2015, 03:19 AM   #1
fritz001
Member
 
Registered: Aug 2004
Posts: 176

Rep: Reputation: 18
Exclamation 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 .
 
Old 08-31-2015, 03:21 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,685

Rep: Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274
What have you tried so far? Why do you want to use perl? Is this a homework or ???
 
Old 08-31-2015, 03:27 AM   #3
fritz001
Member
 
Registered: Aug 2004
Posts: 176

Original Poster
Rep: Reputation: 18
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 .
 
Old 08-31-2015, 03:29 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,685

Rep: Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274
you do not need to install anything, but we cannot help you to extend your script without knowing it.
 
Old 08-31-2015, 03:47 AM   #5
fritz001
Member
 
Registered: Aug 2004
Posts: 176

Original Poster
Rep: Reputation: 18
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
 
Old 08-31-2015, 08:02 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
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.
 
Old 08-31-2015, 08:16 AM   #7
fritz001
Member
 
Registered: Aug 2004
Posts: 176

Original Poster
Rep: Reputation: 18
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.
 
Old 08-31-2015, 08:21 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,685

Rep: Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274
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...
 
Old 08-31-2015, 08:42 AM   #9
fritz001
Member
 
Registered: Aug 2004
Posts: 176

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

Last edited by fritz001; 08-31-2015 at 08:47 AM.
 
Old 08-31-2015, 08:47 AM   #10
fritz001
Member
 
Registered: Aug 2004
Posts: 176

Original Poster
Rep: Reputation: 18
hope, I was a bit clear now
 
Old 08-31-2015, 12:15 PM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,685

Rep: Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274
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
 
Old 08-31-2015, 12:48 PM   #12
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,876
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by fritz001 View Post
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.
 
Old 08-31-2015, 01:54 PM   #13
fritz001
Member
 
Registered: Aug 2004
Posts: 176

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

Last edited by fritz001; 09-01-2015 at 01:45 AM.
 
Old 09-01-2015, 02:24 AM   #14
fritz001
Member
 
Registered: Aug 2004
Posts: 176

Original Poster
Rep: Reputation: 18
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/; }

}
}

Last edited by fritz001; 09-01-2015 at 02:27 AM.
 
Old 09-02-2015, 11:24 AM   #15
fritz001
Member
 
Registered: Aug 2004
Posts: 176

Original Poster
Rep: Reputation: 18
SOLVED!

Some hints from perlmonks helped me quite a lot !!
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux command - Putting Two Consecutive Lines into One Line rajaniyer123 Linux - Server 1 07-05-2014 11:39 AM
[SOLVED] script to read and compare two consecutive lines in a file smritisingh03 Linux - Newbie 16 08-28-2012 04:36 AM
[SOLVED] Sed -> Search on 2 consecutive lines and then replace lebrocoli Programming 3 04-27-2012 02:20 PM
[SOLVED] Shell script to convert values on successive lines into consecutive ranges? kmkocot Programming 5 07-09-2010 10:59 AM
sed/awk: Three consecutive blank lines in a file, how to delete two of them? recomboDNA Programming 8 06-17-2010 09:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration