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

Re: Use of grep and foreach

by Corion (Pope)
on Aug 11, 2003 at 13:24 UTC ( #282858=note: print w/replies, xml ) Need Help??

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.

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://282858]
[LanX]: ellipse aka ... aka yadda yadda is a new command
[LanX]: sopw!
[1nickt]: Sorry kgb can't help there, not a perltidy user.
[marto]: evening all

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2017-11-18 18:23 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (277 votes). Check out past polls.