The output you showed does not match the output I get - your s/\s+//g; to remove all whitespace seems to be working fine. So it seems like the code you showed here does not match your input and/or output, please make sure to provide consistent examples. See How do I post a question effectively? and Short, Self-Contained, Correct Example. Also, please use <code> tags to format input and output in addition to your code.

Athanasius has pointed out that you are adding extra newlines to the output. Another important point here is that you are reading the input line-by-line, so that the print OUT "Genome: \n" happens on every line of input. What Athanasius's code is doing instead is printing the "Genome:" once, before all the data, and then printing the data with all newlines removed, which works because print does not add newlines to the output by default (that means print "a"; print "b"; print "c"; writes "abc" to the output one character at a time).

A couple more tips: I strongly recommend you clean up your formatting, that'll make it easier for you and for us to read your code - perltidy can help with that. Also, since I don't see the declaration of $line, make sure that you Use strict and warnings and only declare your variables at the smallest scope necessary (e.g. while ( my $line = <$in_fh> )). And one more nit: "escape" usually means to take a special character and turn it into plaintext, for example turning "\n" into "\\n" such that in the output, instead of a newline, you get two visible characters, \n. I think your question would be better described as "removing all newlines".


In reply to Re: how to escape new line in a string by haukex
in thread how to escape new line in a string by nica

Title:
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, details, 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, summary, 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.