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: capturing tcl console for script usage
    ... the last proc works just fine with the strings. ... rename puts _puts ... set args "simon says: $args" ... One convenient fact, though, is that the string to be printed ...
    (comp.lang.tcl)
  • 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)
  • Re: How do I reduce the memory usage of a script?
    ... simply do data = nil after processing. ... string 'QuickTime' somewhere in the first few MB, ...
    (comp.lang.ruby)
  • Re: Memory Question
    ... I ran this in accordance with a couple other scripts which utilize 150mb+ of memory, to see if the os would somehow force ruby to do better GC'ing, it didn't and swap was used. ... On my system this script consistently used 42Mb of memory, and the string count never went below 800,000. ... puts count_objects_for ...
    (comp.lang.ruby)