in reply to Re: replacing in file
in thread replacing in file

Hey skyknight
The problem with the one liner is that it's actually part of a bigger script. I tried calling the one liner perl program in a larger program, but it doesnt do the job. For some oddball reason, I wanted to replace the line "To: <list of email addys>" to just "To: me@home.com". The regex I used only got rid of the To: and left everything else. Here's the one liner in my perl script:
`perl -p -e 's/^To: [\.\@\D\,]*/To:me@home.com/' Mailbox`;
Can you give me any other specifics?

Replies are listed 'Best First'.
Re: Re: Re: replacing in file
by sgifford (Prior) on Aug 01, 2003 at 18:13 UTC
    Here's (roughly) what Perl does when you use the -pi.bak flag on the file filename (and what you'll have to emulate):
    1. Open the file "filename" for read, with handle READ
    2. Rename "filename" to "filename.bak"
    3. Open the file "filename" for write+truncate with handle WRITE
    4. Read through all the lines from READ, modify them, then print them to WRITE
Re: Re: Re: replacing in file
by skyknight (Hermit) on Aug 01, 2003 at 18:16 UTC

    It's not working because you lost the -i switch, which specifies edit-in-place. Do "perl --help" to get a list of command line switches. You really shouldn't invoke an external process, though, if you don't have to do so. Just follow the other idiom I mentioned. To elaborate...

    open(OLD, "<old.txt") or die "can't open original file: $!"; open(NEW, ">new.txt") or die "can't open new file: $!"; while(<OLD>) { s/whatever/somethingelse/; print NEW; } close(OLD); close(NEW) rename("new.txt", "old.txt");