in reply to Re^10: Help editing a file based off seach criteria
in thread Help editing a file based off seach criteria

Have you actually tested your code? Assigning to $. does not reset the file pointer in any way. You need to either use seek, or go a step back and look at the original suggestion of using Tie::File. Or, actually, start thinking about the approach I outlined twice already.

Replies are listed 'Best First'.
Re^12: Help editing a file based off seach criteria
by perlnewb123 (Sexton) on Jan 13, 2009 at 18:07 UTC
    I cant use Tie, it's not included in my version of perl, and im restricted on what I can install. Yes this works, otherwise I wouldnt have posted it.

      First of all, Yes, even you can use CPAN, and you can always copy the code from within modules into your program.

      Second, are you sure that it works? Because after adding some print statements to your code to monitor its progress it seem to me that it's not doing anything sensible:

      #!/usr/bin/perl use strict; #open DATA, "</home/file.conf"; my $DESIRED_LINE_NUMBER; my $LINE; while (<DATA>) { if(m/END/) { print "END line is on line >$.\n"; print "END line is >$_\n"; my $pos = $.; $pos--; chomp $pos; $DESIRED_LINE_NUMBER = $pos; } } print "Moving to line number $DESIRED_LINE_NUMBER\n"; $. = 0; do { $LINE = <DATA>; print "Discarding: $LINE\n" } until $. == $DESIRE +D_LINE_NUMBER || eof; print "Found desired line $DESIRED_LINE_NUMBER as '$LINE'\n"; __DATA__ foo bar baz END bloob blargh blubb blubb2

      But then again, if you're confident that it does what you want, most likely you're also able to modify it yourself to do more of what you want.

      The modified program outputs:

      C:\Projekte>perl -w tmp.pl >4 >END Moving to line number 3 Use of uninitialized value $LINE in concatenation (.) or string at tmp +.pl line 2 6. Discarding: Use of uninitialized value $LINE in concatenation (.) or string at tmp +.pl line 2 7. Found desired line 3 as ''

      ... which doesn't look like the line above the END line to me.

        This is an example... The match im trying to make is a commented filed in a file that has the pattern END in the string. So create a file with junk in it, put some commented field in the file #END. From here I need to be able to jump up one line, insert data, then place a new line under the new data. Some compaines restrict mod installs, as on platforms like aix, upgrades will sometimes render the mod usless, which is this case, so using CPAN is not an option. Even looking at TIE im not seeing an easy option for this.
        open DATA, "</home/file.conf"; while (<DATA>) { if(m/\END\b/) { print "$.\n"; print "$_\n"; $pos = $.; $pos--; chomp $pos; $DESIRED_LINE_NUMBER = $pos; } } #$. = 0; do { $LINE = <DATA> } until $. == $DESIRED_LINE_NUMBER || eof; print $LINE;
        Result: 1675 ---> Line found matching Search # *** END NEW MAPPING RULES SECTION *** ---Txt from Line -->Blank Line where I need to add values.