Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Use of grep and foreach

by monaLisa (Initiate)
on Aug 11, 2003 at 13:19 UTC ( #282853=perlquestion: print w/replies, xml ) Need Help??
monaLisa has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I want to use grep to look for all line starting with "myLine" and to make changes in only some parts of only these lines in a file Can you please help?

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

    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

      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."
Re: Use of grep and foreach
by cfreak (Chaplain) on Aug 11, 2003 at 13:23 UTC

    I don't mean to be rude but a good place to start would be the documentation. There are examples of how to use grep there. If you don't understand the docs then come and ask questions. Perlmonks is a place where you have to put some effort into finding the answer yourself, rather than copying the answer from someone else. Its better to learn something than to copy it :)

    Lobster Aliens Are attacking the world!
Re: Use of grep and foreach
by larsen (Parson) on Aug 11, 2003 at 13:29 UTC
    monaLisa, I've already downvoted one of your previous questions. Even if nobody cares about XP, here, ( ;-) ) and I'm sure you do the same, receiving tons of --s could discourage you, and could make you fail to perceive and obtain the benefits this site can give to you. So, here some advices on how to ask a question, in order to obtain the maximus of help and achievement: How to RTFM.

    For what concerns your specific problem, you'll find useful informations in perlfunc, perlop and perlre.
Re: Use of grep and foreach
by monktim (Friar) on Aug 11, 2003 at 13:34 UTC
    It sounds like you're just staring out. You might want to pick up a copy of Learning Perl or Programming Perl these are excellent books published by O'Reilly. If you don't have your books handy there is plently of on line documentation. You also have documentation on you machine. They this

    perldoc -h
    perldoc -f grep

    Good luck.
Re: Use of grep and foreach
by crouchingpenguin (Priest) on Aug 11, 2003 at 14:22 UTC

    Give this a try:

    #!/usr/bin/perl use strict; use warnings; use Tie::File; my @file; tie @file, 'Tie::File', '/tmp/filename' or die "Failed to tie file: $!"; foreach my $matching_line ( grep { m/^myLine/ } @file ){ $matching_line =~ s/^myLine/this_was_a_match/; }

    "Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."
Re: Use of grep and foreach
by tcf22 (Priest) on Aug 11, 2003 at 13:24 UTC
    How about using map.
    use strict; open(INFILE, "/path/to/file"); my @lines = map {s/oldthing/newthing/ if(m/myLine/);$_;} <INFILE>; close(INFILE); open(OUTFILE, ">/path/to/file"); print OUTFILE $_ foreach(@lines); close(OUTFILE);

    UPDATE: Changed print to filehandle(OUTFILE) instead of STDOUT
      I am using this, but does not do want I want:
      if(/^otsort/) { $myLine =~ s|(/apost.*) f\(fix,(\d{4}\).*$|"$1,record\($2\)"|g; print "$myLine\n"; print outFile "$endUnixLine\n"; }
Re: Use of grep and foreach
by CombatSquirrel (Hermit) on Aug 11, 2003 at 13:28 UTC
    Be more specific. What changes? In which parts of the lines? How do the lines look like?
    Please, be more specific in your posts. I have told you before (282802, 282854) and others have told you to be more specific. Please read our posts again and try to follow our suggestions.
    Also, it seems as if you were working on a large project. It would be helpful just to have one post, and provide us with more detaisl about that project.
Re: Use of grep and foreach
by artist (Parson) on Aug 11, 2003 at 14:09 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://282853]
Approved by Corion
[erix]: my favorit scientist Darwin-reply was a good sly move to keep the chattering going ;)
[erix]: I've drawn several continents into the quicksand if I'm not mistaken

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2017-12-15 12:56 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (432 votes). Check out past polls.