Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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


Comment on Re: Use of grep and foreach
Select or Download Code
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."

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://282858]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (11)
As of 2014-10-01 12:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (13 votes), past polls