Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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




In reply to Re: Search and replace in file (was Need help with string and file manipulation) by tachyon
in thread Need help with string and file manipulation by Anonymous Monk

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (3)
    As of 2018-06-18 01:10 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (107 votes). Check out past polls.