Re: help regarding Comparision of two lines



On 9/26/07, raaki <hemanth004@xxxxxxxxx> wrote:
hi friends

recently i started learning perl.i need to print some statements after
perticular line.in my file there is one line called "puts
conCheckFAIL#-----------"occuring number of times.i need to print some
statements after the 6th time it occuered.can you help me with that


puts \$conCheckFail
\"#-------------------------------------------------\"\n"; # 5th time
occurance of puts...
puts \$conCheckFail \"# CONNECTIVITY CHECK ERRORS FOR PORT TYPE address
\"\n";
puts \$conCheckFail
\"#-------------------------------------------------\"\n"; # 6th
time occurance of puts...

i try to write the code as below .trying to compare the above line
last part as a string and print the statements but after printing the
print statements the 6 th occurance is printing again .can u help in
any way solving this either when the 6th time it appeared i needto
print my print statements and rest of the file text need to be the
same...



rename "$ARGV[0]", "$ARGV[0].tmp";
open INPUT, "$ARGV[0].tmp";
open OUTPUT, "> $ARGV[0]";
while (<INPUT>) {
if ($_=~"PORT TYPE address") {
print ..
print ..
print ..
print ..
} else {
print OUTPUT "$_";
};
}
unlink "$ARGV[0].tmp";

rename "$ARGV[0]", "$ARGV[0].gate";

I am a bit unclear about what you are trying to do.
If you want to search for the string "PORT TYPE address", the code you
have looks good.
If you want to find the 6th occurrence, you could you a counter.

My input file:
--- START ---
1: puts $conCheckFail "#-------------------------------------------------"\n";
2: puts $conCheckFail "#-------------------------------------------------"\n";
3: puts $conCheckFail "#"PORT TYPE address""\n";
4: puts $conCheckFail "#-------------------------------------------------"\n";
5: puts $conCheckFail "#-------------------------------------------------"\n";
6: puts $conCheckFail "#-------------------------------------------------"\n";
7: puts $conCheckFail "#-------------------------------------------------"\n";
8: puts $conCheckFail "#-------------------------------------------------"\n";
--- END ---

Code:
--- START ---
#!/usr/bin/perl

use warnings;
use strict;

# Make sure we got a valid file in $ARGV[0]
die "Wrong usage\n" unless (defined $ARGV[0] and -f $ARGV[0]);

open my $FH, "< $ARGV[0]" or die "Can't open file\n";
while (<$FH>) { # Search for this string and print out the line that has it
print if ( /"PORT TYPE address"/);
}
close $FH;

print "***\n";

open $FH, "< $ARGV[0]" or die "Can't open file\n";
my $count = 0;
while (<$FH>) { # Count lines with this string and print the 6th line
with this string
$count++ if ( /conCheckFail/);
print if $count == 6;
}
--- END ---

Output:
--- START ---
3: puts $conCheckFail "#"PORT TYPE address""\n";
***
6: puts $conCheckFail "#-------------------------------------------------"\n";
--- END ---
.



Relevant Pages

  • Re: Perl Pro but Java Newbie: Need nudge in proper direction for my favorite Perl routine in Java
    ... You can do this in Java: ... puts("A single string test." ... puts(myStringArray); ... public static void puts(Stringargs) { ...
    (comp.lang.java.programmer)
  • Re: Tutorial challenge program help
    ... while replytt!= 'BYE' ... puts 'WHAT\'S THAT SONNY SPEAK UP!' ... # available to any given string. ...
    (comp.lang.ruby)
  • Re: String spliting and inclusion
    ... puts "Attention, some backtracking here:" ... puts "I cannot come up with a non backtracking solution right now:(" ... have a long string and I want to split into smaller ... strings no longer than 50 characters in length. ...
    (comp.lang.ruby)
  • Re: Tutorial challenge program help
    ... Until then, you learn about instance variables and OOP, it would probably be ... puts "HUH?! ... local variable with the and then compare that input to ... To embed something in a string, you have the opening and closing " symbols, ...
    (comp.lang.ruby)
  • Re: puts variable without quoting
    ... but was wondering about performance of quoting it anyway. ... quotes with a puts string. ... Tclers omit the "$quotes" as a matter of style. ...
    (comp.lang.tcl)