Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2016-10-27 21:25 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (371 votes). Check out past polls.