Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

How can I make a regex apply to each line of a file?

by Kage (Scribe)
on Oct 28, 2001 at 05:54 UTC ( #121791=perlquestion: print w/replies, xml ) Need Help??
Kage has asked for the wisdom of the Perl Monks concerning the following question:

ok... if I did like
open (FILE,"<$footer"); if ($lock){ flock(DATA, $lock)} @footerfile = <FILE>; close(FILE); foreach $footerline(@footerfile) { print "$footerline"; }
how would I put in a  while( $tmpl = <FILE> ) { $tmpl =~ s/<<whatever>>/$whatever/g; } ?

Edit kudra, 2001-10-30 Changed title

Replies are listed 'Best First'.
Re: Oooh, Oooh, Help Me Help Me (:
by jj808 (Hermit) on Oct 28, 2001 at 07:44 UTC
    Use grep map. Update: Of course, ChemBoy is entirely right about this. That's what I get for posting after a 13-hour night shift...

    If you just want to print the (modified) contents of the file, you can do

    open (FILE,"<$filename") or die "Cannot open: $!"; map{s/stuff/other_stuff/g;print}(<FILE>);
    Or if you want to keep the modified file in an array, try
    open (FILE,"<$filename") or die "Cannot open: $!"; @ary = map{s/stuff/other_stuff/g}(<FILE>);
    Update: You could also do this as a one-liner:
    perl -pe 's/stuff/other_stuff/g' filename.txt
    Hope this helps,


      No, please don't use grep that way, unless you actually want to strip lines out of your input if they don't match the substitution pattern. Use map, instead--that's what it's for.

      @footerfile = map {s/something/else/} <FILE>;

      Note that the first solution proposed using grep wouldn't have the undesired side-effects, but it's still bad coding practice--never use grep or map in void context (obfuscatory and inefficient).

      And Kage, please find a better descriptive title for your question next time...

      Update: or, of course, you could alter the array in place after reading it in, using

      s/nothing/something/ for @footerfile;

      Update 2: of course, it would help if I told you the right syntax for map, too...

      @footerfile = map {s/something/else/;$_} <FILE>;
      (thanks, Zaxo! And I wish I had as good an excuse as jj808 for screwing this one up....)

      If God had meant us to fly, he would *never* have given us the railroads.
          --Michael Flanders

Re: How can I make a regex apply to each line of a file?
by Hofmator (Curate) on Oct 30, 2001 at 21:02 UTC

    and it might be a good idea to check the return value of your open call open (FILE, "<$footer") or die "Couldn't open file $footer: $!";

    Apart from that, it might not be necessary to read the whole file into memory. So you could do something like

    open (FILE, "<$footer") or die "Couldn't open file $footer: $!"; while (my $line = <FILE>) { $line =~ s/something/something_else/g; # print changed line print $line; # do some very complicated other stuff here } close FILE;
    But this depends strongly on what you are doing with the lines of the file.

    Update: Added comment pointing to more complex operations in while loop.

    Thanks davorg, I was aware of your command line solution. Nevertheless I posted in order to stress the fact that there is the while-loop alternative to the memory intensive 'map on array' which was suggested by jj808 and ChemBoy. And I assumed that Kage wanted to do a little bit more than just printing the changed line - although my short snippet didn't show that :)

    -- Hofmator

      That's the kind of thing that Perl command line options make very easy:

      perl -i.bak -pe 's/something/something else/g' input.txt

      "The first rule of Perl club is you don't talk about Perl club."

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://121791]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2018-01-20 15:40 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (226 votes). Check out past polls.