http://www.perlmonks.org?node_id=282858


in reply to Use of grep and foreach

You can't use grep for that, but the following should be a good enough template for a start:

#!/usr/bin/perl -w use strict; while (<>) { if (/foo/) { # ... change line : $_ = "This line was changed ($_)"; }; };

Perl also has the facility to edit files in place, which is quite convenient for oneliners. The above could be written as one line like this:

perl -pe '/foo/ and $_ = "This line was changed ($_)"'

I hope that helps you, as I could not really make much out of your question. A module which is really good for manipulating files line by line would be Tie::File - take a look at the documentation, it is worth a read!

perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web

Replies are listed 'Best First'.
Re: Re: Use of grep and foreach
by crouchingpenguin (Priest) on Aug 11, 2003 at 14:24 UTC

    You sure can, see this post for editing in place using grep and Tie::File.


    cp
    ----
    "Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."