Re: help regarding Comparision of two lines
- From: yitzle@xxxxxxxxxxxxxxxxxxxxx (Yitzle)
- Date: Wed, 26 Sep 2007 14:38:50 -0400
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 ---
.
- Follow-Ups:
- Re: help regarding Comparision of two lines
- From: Raaki
- Re: help regarding Comparision of two lines
- References:
- help regarding Comparision of two lines
- From: Raaki
- help regarding Comparision of two lines
- Prev by Date: Re: large Files management
- Next by Date: Re: help regarding Comparision
- Previous by thread: help regarding Comparision of two lines
- Next by thread: Re: help regarding Comparision of two lines
- Index(es):
Relevant Pages
|
|