Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Need help with string and file manipulation

by Anonymous Monk
on Apr 13, 2002 at 17:52 UTC ( #158811=perlquestion: print w/replies, xml ) Need Help??

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 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):

    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




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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://158811]
Approved by tachyon
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2020-09-25 20:58 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (141 votes). Check out past polls.