On 26 Sep, 20:38, [EMAIL PROTECTED] (Yitzle) wrote: > On 9/26/07, raaki <[EMAIL PROTECTED]> 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 ---- 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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/