http://qs321.pair.com?node_id=158811

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hey there monks! I'm what you call a new born Perl baby. I haven't written a code in Perl, but I am reading on how to program Perl. I have an assignment that needs me to know how to program, but I'm falling short on time and I need at least some pointers if not an explanation of how to get it done. Here's my problem...

This project will focus on Perl’s pattern matching and file manipulation features. My objective is to write a Perl program that takes an input file, a search string, and a replacement string, and then generates a new file with all occurrences of the search string in the input file replaced with the replacement string. My program should prompt the user for an input file name, a search string, a replacement string, and an output file name. The program should replace all occurrences of the search pattern with the replacement string, while copying the input file to the output file.

Now if any of you monks, can point me in some correct direction or even feel like giving me some sympathy and even show me how, and I'll follow your sample I'd be forever indebted. If anyone can help please, let me know or email me...papi_rock@yahoo.com Thanks...HELLLLLLLLLP!!!!!

Restored AM's paragraphing - dvergin 2002-04-13

  • Comment on Need help with string and file manipulation

Replies are listed 'Best First'.
Re: Search and replace in file (was Need help with string and file manipulation)
by tachyon (Chancellor) on Apr 13, 2002 at 18:27 UTC

    A key element in learning is RESEARCH. All the answers you want are on this site, in problably 100+ threads. Here is how to find just one of them.....

    There is a feature here called Super Search. You can search for different phrases. I might try 'search and replace in file', 'reading from a file', 'writing to a file' as search strings. The functions you will use are (in order):

    RTFM
    use warnings; to give you hints about dodgy code
    use strict; more hints about dodgy code
    print to print your prompts
    the = assignment and <> input operators
    chomp to remove the newlines from you input data
    quotemeta you will need to quotemeta your search string (just trust me :-)
    open x 2 one file for <reading and one for >writing ( < & > = hint )
    die to make sure your opens worked
    $! to find out why your file opens failed (if they did)
    while to iterate over your input file
    s to do you search and replace, don't forget the /g
    print to print to your output file
    close to close your files

    You can do it in one line in Perl using the 'inplace editing' function. This will do the search and replace inplace on somefile.txt and write a backup of the original to somfile.txt.bak. Unfortunately you will probably get a FAIL if you submit it as your homework answer.

    perl -pi.bak -e 's/this string/that string/g' somefile.txt

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Need help with string and file manipulation
by impossiblerobot (Deacon) on Apr 14, 2002 at 17:44 UTC
    To tachyon's excellent suggestion(s), I would add that you should look at our Tutorials section, which, in addition to providing a solution to essentially the same problem you are proposing, will give you enough of an overview of Perl that you could write this yourself.

    Impossible Robot