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 . |
What have you tried so far? Why do you want to use perl? Is this a homework or ???
|
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 . |
you do not need to install anything, but we cannot help you to extend your script without knowing it.
|
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 |
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. |
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. |
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... |
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... |
hope, I was a bit clear now :)
|
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 |
Quote:
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. |
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] when the xml file does not contain any valid data will have something like this : Code:
<resources> an working xml file will be like : Code:
<resources> I'd like to have something like Code:
<resources> I\ll try tomorrow to post full script Well, I think now it's a better description of what i\, trying to do |
Code:
|
SOLVED!
Some hints from perlmonks helped me quite a lot !! |
All times are GMT -5. The time now is 11:11 PM. |