Home > Archive > PERL Beginners > September 2007 > help regarding Comparision of two lines
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
help regarding Comparision of two lines
|
|
| Raaki 2007-09-26, 10:00 pm |
| 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";
| |
| Yitzle 2007-09-26, 10:00 pm |
| On 9/26/07, raaki <hemanth004@gmail.com> 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 ---
| |
|
| On 26 Sep, 20:38, yit...@users.sourceforge.net (Yitzle) wrote:
> On 9/26/07, raaki <hemanth...@gmail.com> wrote:
>
>
>
>
>
>
>
>
>
>
>
> 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 ---- Hide quoted text -
>
> - Show quoted text -
hi Yitzle,
thanks for the answer. i try to explain detailly.i had a file which i
have to replace and write some more lines so i started writing a perl
script..
my file is more or less like ..
strat------------
puts $conCheckFail "#--------------------------------------------"
puts $conCheckFail "# MEMORY CLOCKS"
puts $conCheckFail "#--------------------------------------------"
.....
.....
.....
puts $conCheckFail
"#-------------------------------------------------"
puts $conCheckFail "# FOR PORT TYPE address"
puts $conCheckFail
"#-------------------------------------------------" (# 6th time
appearing)
.....
.....
.....
#----------------------
#FORCING RESET
#----------------------
.....
.....
end
i need to print some lines after the appearence of "puts
$conCheckFail" for the 6th time and the continuation must be the
same.i use print statement for using this.the code i wrote is below
rename "$ARGV[0]", "$ARGV[0].tmp";
open INPUT, "$ARGV[0].tmp";
open OUTPUT, "> $ARGV[0]";
while (<INPUT> ) {
my $count == 0;
if ($_=~/puts$conCheckFail/) {
$count ++ ;
};
if ( $count == 6) {
print.....
print....
} else {
print OUTPUT "$_";
};
}
}
can u help me with the errors in this code..
thasnkyou
| |
| Yitzle 2007-09-29, 10:07 pm |
| #!/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 $ARGV[0] : $!\n"; # I
think it was #!...
my $count = 0;
my $linesPrinted = 0;
while (<$FH> ) { # Count lines with this string and print the 6th line
with this string
$count++ if ( /puts$conCheckFail/);
if ( $count <= 6 and $linesPrinted <= 6 ) {
$linesPrinted++; # Count lines printed. Stop after 6
print;
}
}
|
|
|
|
|