in reply to Edit lines from a file and replace multiple lines.

hello Endurance and welcome to the monastery and to the wonderful world of perl!

As you have seen, given scarce or absent effort will not produce code. Brother holli has shown this monastery principle, de facto.

Formatting is important: when you compose a node her, in the bottom of the screen you can find useful links. Put your code and data input and output expected inside  <code> code tags </code>.

As this is your first post a bit of mercy can be spent.. ;)

While learning Perl a good principle is: are you able to do this without a computer? If yes write down the solution in basic english, then try to translate it using the syntax you are able to produce.

Let's try:

Open the file named file.txt. Abort if the file is unreadable. Read the file line per line. If solar wind is found annotate it's line number as solar_linenum Print line just read anyway. Read and print two lines more. If the third line has country then replace it with place and print the line but also set solar_linenum to 0. The same for the other requirement.

Good starting points are: perlintro and the open and perlopentut then foreach ( update ..oops see here as wisely stated below AnomalousMonk) also perlvar can be handy (hint: search for $. that holds current line num while reading files.. ;).

Never forget to begin your program with:

use strict; use warnings; # while learning also the following can be handy; uncomment as needed: # use diagnostics; # lower case: is a pragma not a module

HtH*

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: Edit lines from a file and replace multiple lines. -- basic english approach
by AnomalousMonk (Archbishop) on Mar 26, 2019 at 18:51 UTC
Re^2: Edit lines from a file and replace multiple lines. -- basic english approach
by Endurance (Novice) on Mar 29, 2019 at 06:42 UTC
    The code I wrote is as follows:
    #!/usr/bin/perl use strict; use warnings; my $line = ''; while (<>) { $line .= $_; if($line =~ /solar winds/) { { if( $line =~ /country/) { $line =~ s/country/place/; } } elsif($line =~ /solar power/) { { if( $line =~ /country/) { $line =~ s/country/place/; } } $line = ''; }
    This doesn't work as I dont know how to traverse to the third line and substitute the word country. Also file might have these conditions multiple times.
      Hello Endurance,

      > This doesn't work..

      ..not even compile ;) A saner indentation will help:

      use strict; use warnings; my $line = ''; while (<>) { $line .= $_; if($line =~ /solar winds/) { { if( $line =~ /country/) { $line =~ s/country/place/; } } elsif($line =~ /solar power/) { { if( $line =~ /country/) { $line =~ s/country/place/; } } $line = ''; } __END__ syntax error at endurance01.pl line 16, near "elsif" syntax error at endurance01.pl line 23, near "}" Missing right curly or square bracket at endurance01.pl line 23, at en +d of line endurance01.pl had compilation errors.

      You used too much curlys in your if statements. Then: you didnt follow my approach but instead you are adding to $line and so loosing any notion of the current line that is one of your requirements. As side note if you s/this/that/ is not needed the matching before if $x =~ /this/ because the sostitution only acts if it matches.

      But let's try to implement (partially: is your work ;) my approach:

      > Open the file named file.txt. Abort if the file is unreadable. Read the file line per line. If solar wind is found annotate it's line number as solar_linenum Print line just read anyway. Read and print two lines more. If the third line has country then replace it with place and print the line but also set solar_linenum to 0. The same for the other requirement.

      use strict; use warnings; # Open the file named file.txt. Abort if the file is unreadable. my $filename = 'file.txt'; open my $file_handle, '<', $filename or die "Unable to open the file[$ +filename]!"; # a switch to hold if solar wind is found; my $solar_wind_found_at_line = undef; # Read the file line per line. while ( my $line = <$file_handle> ){ # remove the newline chomp $line; # print is your friend! is the first and powerful debug tool.. print "DEBUG: line $. -->$line<--\n"; # check for solar wind if ( $line =~ /solar winds/){ # annotate the current line number $solar_wind_found_at_line = $.; print "DEBUG: 'solar wind' found at line $.\n"; } # if ( $solar_wind_found_at_line IS DEFINED AND THE CURERENT +LINE IS THE THIRD AFTER SOLAR WINDS ){ # REPLACE COUNTRY WITH PLACE (THIS CAN HAPPEN OR NOT) # RESET $solar_wind_found_at_line TO UNDEF (THIS MUST HAPP +EN ANYWAY) # } # at the end the expected output print "OUTPUT DESIRED: [$line]\n\n" }

      HtH

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.