http://www.perlmonks.org?node_id=11101425

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

INPUT

ORIGIN

1 ccctaaaccc taaaccctaa accctaaacc ctaaacccta aaccctaaac cctaaaccct

61 aaaccctaaa ccctaaaccc taaaccctaa accctaaacc ctaaacccta aaccctaaac

121 cctaaaccct aaaccctaaa cgatgcatta ctactcacac gaacgagtga atgaaacaca

//
if ($line =~ /^$begin/) #$begin="ORIGIN"; { &body;} sub body { while ($line = <IN>) { chomp ($line); if ($line !~ /^$end/) { #$end =" \/\/" $line =~ s/[0-9]//g; # $line =~ s/\s+//g; # $line =~ s/\n//g; chomp ($line); #$line =~ /([atcgn]+)/g; print OUT "Genome: \n $line\n"; } else { last; } } }
OUTPUT:

Genome: ccctaaaccc taaaccctaa accctaaacc ctaaacccta aaccctaaac cctaaaccct

Genome: aaaccctaaa ccctaaaccc taaaccctaa accctaaacc ctaaacccta aaccctaaac

Genome: cctaaaccct aaaccctaaa cgatgcatta ctactcacac gaacgagtga atgaaacaca

EXPECTED:

Genome: ccctaaaccctaaaccctaaaccctaaaccctaaaccctaaaccctaaaccctaaaccctaaaccctaaaccctaaaccctaaaccctaaaccctaaaccctaaaccctaaaccctaaaccctaaaccct aaaccctaaacgatgcattactactcacacgaacgagtgaatgaaacaca

QUESTION: how could I escape new line? I tried \n substitution, \W.. or whatever else, but anyway it does not disappear.

Replies are listed 'Best First'.
Re: how to escape new line in a string
by Athanasius (Archbishop) on Jun 16, 2019 at 08:37 UTC

    Hello nica,

    If you look carefully at this line:

    print OUT "Genome: \n $line\n"; # ^^ ^^

    you will see that you are adding the newlines yourself! Here is one approach that does what you want:

    use strict; use warnings; print 'Genome: '; while (my $line = <DATA>) { $line =~ s/^[0-9]+//; # Remove initial digits $line =~ s/\s+//g; # Remove all whitespace (including newline +s) print $line if $line =~ /[acgt]+/; } print "\n"; __DATA__ INPUT ORIGIN 1 ccctaaaccc taaaccctaa accctaaacc ctaaacccta aaccctaaac cctaaaccct 61 aaaccctaaa ccctaaaccc taaaccctaa accctaaacc ctaaacccta aaccctaaac 121 cctaaaccct aaaccctaaa cgatgcatta ctactcacac gaacgagtga atgaaacaca

    Output:

    18:35 >perl 2007_SoPW.pl Genome: ccctaaaccctaaaccctaaaccctaaaccctaaaccctaaaccctaaaccctaaaccctaa +accctaaaccctaaaccctaaaccctaaaccctaaaccctaaaccctaaaccctaaaccctaaacccta +aaccctaaacgatgcattactactcacacgaacgagtgaatgaaacaca 18:35 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: how to escape new line in a string
by haukex (Archbishop) on Jun 16, 2019 at 08:51 UTC

    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".

Re: how to escape new line in a string
by james28909 (Deacon) on Jun 16, 2019 at 15:53 UTC
    Like this?
    #using activestate perl 5.16.3 use strict; use warnings; s/^INPUT|^ORIGIN|\d|\s//g && print while(<DATA>); # '\s' finds newline +s as well __DATA__ INPUT ORIGIN 1 ccctaaaccc taaaccctaa accctaaacc ctaaacccta aaccctaaac cctaaaccct 61 aaaccctaaa ccctaaaccc taaaccctaa accctaaacc ctaaacccta aaccctaaac 121 cctaaaccct aaaccctaaa cgatgcatta ctactcacac gaacgagtga atgaaacaca
    OUTPUT: C:\Users\James\Desktop\pm\>pm.pl ccctaaaccctaaaccctaaaccctaaaccctaaaccctaaaccctaaaccctaaaccctaaaccctaaa +ccctaaaccctaaaccctaaaccctaaaccctaaaccctaaaccctaaaccctaaaccctaaaccctaa +acgatgcattactactcacacgaacgagtgaatgaaacaca
Re: how to escape new line in a string
by karlgoethebier (Abbot) on Jun 16, 2019 at 19:00 UTC

    I just wondered if the data you provided is really the original format. And has this format a name/standard? And printing "Genome" in the filtered output might be redundant. Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: how to escape new line in a string
by betmatt (Scribe) on Jun 20, 2019 at 09:17 UTC
    Hi, You have substituted a new line character (\n) with nothing. Then why do you need to chomp? Chomp is set to the new line character by default.


    Update________________________

    It might be worth mentioning if the files are being transferred across from Windows to Linux. There is sometimes extra hidden characters in that situation.