Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Replace new line with white space

by fattahsafa (Sexton)
on Mar 09, 2014 at 20:21 UTC ( #1077613=perlquestion: print w/replies, xml ) Need Help??

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

hi, In my application I read text from file into string then perform some string operations. I need to replace each new line by white space. How can I do that? Abed.

Replies are listed 'Best First'.
Re: Replace new line with white space
by Kenosis (Priest) on Mar 09, 2014 at 20:31 UTC

    Help me understand what you're asking. A newline (\n) is a species of whitespace. Do you mean replace each newline with a space character, i.e., " " (\x20)? If so, you can:

    s/\n/ /g;
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Replace new line with white space
by kcott (Archbishop) on Mar 09, 2014 at 23:40 UTC

    G'day Abed,

    Is this what you're after:

    #!/usr/bin/env perl -l use strict; use warnings; my $data_string = do { local $/; <DATA> }; print "Before replacement: '$data_string'"; $data_string =~ y/\n/ /; print "After replacement: '$data_string'"; __DATA__ Line 1 Line 2 Line 3


    Before replacement: 'Line 1 Line 2 Line 3 ' After replacement: 'Line 1 Line 2 Line 3 '

    And, if that doesn't do what you want, please heed earlier advice and indicate exactly what it did produce and how that differed from what you wanted.

    -- Ken

      Thanks alot Ken, you made my day :) I just edited it by adding \r (I'm using Perl on Windows) and it worked ! --Abed.
Re: Replace new line with white space
by wind (Priest) on Mar 09, 2014 at 21:45 UTC
    I suspect that what you really need is chomp, and that you don't need a regex at all.
    while (<$fh>) { chomp; # Line in $_ no longer has trailing \n
    However, without seeing your code we can't confidently advise you either way.
      Thanks for responding. But chomp only trims the new line at the end of the string.
Re: Replace new line with white space
by project129 (Beadle) on Mar 10, 2014 at 04:21 UTC
    Question can be not so simple as it look from the first sign.

    As example not only different OS standards apply but also encoding question can be raised - please fix my comment if I am wrong but (Unicode Standard Annex #14 docs for Unicode 5.1.0) unicode realization try to fix OS new line problem making no differences between CR, LF && CR+LF chars but when see LF+CR (no CR in first place) - think that's 2 newline characters but ASCII interpreter it as one (historically as i understand).

    p.s: please give us you input data example + perl code && environment localization information - so we can check at least you situation does not apply to something exotic like: "Tibetan Line Breaking" unicode new line finding algorithm.

Re: Replace new line with white space
by llancet (Friar) on Mar 10, 2014 at 01:40 UTC
    You don't need regexp. You can just read a line each time, chomp the trailing "\n", then write it with a trailing space.

      It's difficult to know what the OP needs, even after the OP said, "...I read text from file into string...", since the following reads text from a file into a string, but chomp--in this case--will not remove newlines from $record:

      use strict; use warnings; local $/ = \1024; open my $fh, '<', 'records.txt' or die $!; while ( my $record = <$fh> ) { # process 1024-byte, fixed-length, newline-containing record } close $fh;
      Hi Man, great idea :) but I need to read the text from large number of huge files, so reading line by line may be not efficient.
        You probably don't have to worry about that. While your program may be reading line by line (presumably because that is what you need to do in your algorithm), behind the scenes, Perl is actually buffering input and reading larger chunks of data from the disk (possibly 4 or 8 kB at a time, depending on your OS, hardware, etc.). In brief, there is almost no penalty in reading your file line by line. I am using daily Perl programs to read GB or even dozens of GB of data, if it makes sense in the functional context to read data line by line, then just do it.
        No, it won't affect efficiency. Although you attempt to "read" line by line, the underlying library is actually reading block by block, and the <> operator parses the line and give you one line each time.
Re: Replace new line with white space
by Anonymous Monk on Jun 12, 2018 at 10:06 UTC
    From Perl 5.10 onward, character class shortcut \R will match any kind of line break either working in DOS or UNIX based operating systems. Therefore you can do what you intend with the following substitute statement: s/\R/ /g Note: the "g" modifier replaces all the occurrences of the pattern matched.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2023-01-30 22:16 GMT
Find Nodes?
    Voting Booth?

    No recent polls found