Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: reading/writing line by line

by Biker (Priest)
on May 22, 2002 at 15:14 UTC ( #168463=note: print w/replies, xml ) Need Help??

in reply to reading/writing line by line

I understood that your need is to read a file, line by line, and if the line is not blank, write the line to the output file. Did I understand that correctly?

My code snippet, as you have quoted above, will:

  • Read one line from the input file
  • Remove a potential newline from the end of the line
  • Look if the line is now empty (zero length)
  • If empty, read next line
  • If not empty, write the line and a newline to the output file and then read next line from the input file
  • Repeat until no more lines in input file

What part is not working as expected?

I admit, I missed the words:
"...specifically for finding and replacing all occurences of a string..."

Of course, you're right. ;-) That's what I get for answering a question while on the run to go home. ;-)
while(<IN_FILE>) s/from/to/g; print OUT_FILE; }

should do it.

Everything went worng, just as foreseen.

Replies are listed 'Best First'.
Re: Re: reading/writing line by line
by amarceluk (Beadle) on May 22, 2002 at 15:44 UTC
    Your suggestion did work; thank you! Now I'm trying to apply the same principle to finding and replacing text.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://168463]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2022-05-21 13:51 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (76 votes). Check out past polls.