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

Hi..

Actually I want to convert new line to be ",". As example:-

Input data:- 1111 2222 3333 4444 5555 6666 and the output will be:- 1111,2222,3333 4444,5555,6666
I have tried using s/\n/,/g, but its look not working as i want to be. Could somebody help me ?

Thank you,

Replies are listed 'Best First'.
Re: how to convert new line to ","
by moritz (Cardinal) on Jul 24, 2008 at 09:29 UTC
    In principle your regex works, but probably your string on which you want to substitute doesn't contain what you think it does.

    Here's an example of reading the data in paragraph mode (see $/ for an explanation) and then applying your regex:

    use strict; use warnings; local $/ = ""; while (<DATA>){ chomp; s/\n/,/g; print "$_\n"; } __DATA__ 1111 2222 3333 4444 5555 6666
      The shell command-line equivalent:
      cat data.txt | perl -00pe 's/\\n/,/g'
      Update: Oops! My mistake. I needed to add "-l" argument and also be careful of how the shell interprets '\n':
      cat data.txt | perl -l -00pe 's/\n/,/g'
        Did you actually test that?

        When I tried it did replace newlines with commas, but also the double newlines that bh_perl doesn't want to have replaced.

Re: how to convert new line to ","
by eye (Chaplain) on Jul 24, 2008 at 10:41 UTC
    Rather than convert the new lines, we can think of a single new line as a field separator and a pair of new lines as a record separator. With that view, we can accumulate the fields and then display them in the desired (comma delimited) format once we reach the end of the record.
    #!/usr/bin/perl use warnings; use strict; my(@accumulate) = (); while (<DATA>){ chomp; if (/^\s*$/) { print join(',', @accumulate), "\n"; @accumulate = (); } else {push(@accumulate, $_)} } if (@accumulate) {print join(',', @accumulate), "\n"} __DATA__ 1111 2222 3333 4444 5555 6666
Re: how to convert new line to ","
by poolpi (Hermit) on Jul 24, 2008 at 14:13 UTC


    #!/usr/bin/perl -w use strict; { local $/; my $line = <DATA>; $line =~ s/\b\n\b/, /g; $line =~ s/\n{2,}/\n/g; print $line; } __DATA__ 1,111 22,22 33,33 4,444 5,555 66,66 45,54 4,554 4,654 ----------- Output: 1,111, 22,22, 33,33 4,444, 5,555, 66,66 45,54, 4,554, 4,654

    Well seen moritz !
    Update:

    #!/usr/bin/perl -w use strict; { local $/; my $line = <DATA>; $line =~ s/\n(?!\n)/, /g; $line =~ s/\n{2,}/\n/g; print $line; } __DATA__ !1,111 22,22? .33,33// 4,444!! ::5,555 &&66,66 45,54@@ 4,554<< ``4,654{ --- Output: !1,111, 22,22?, .33,33// , 4,444!!, ::5,555, &&66,66 , 45,54@@, 4,554<<, ``4,654{

    hth,
    PooLpi

    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb
      Won't work for lines that begin or end in non-word-characters.

      Why make assumptions on the data format without any good reasons, when you can do work just as well without these assumptions?

Re: how to convert new line to ","
by pjotrik (Friar) on Jul 24, 2008 at 09:37 UTC
    #!/usr/bin/perl use warnings; use strict; my $x = do {local $/; <DATA>}; $x =~ s/\n/,/g; $x =~ s/,,+/\n/g; print $x; __DATA__ 1111 2222 3333 4444 5555 6666
      That won't work if the data is allowed to contain commas natively.

      That's a general problem if you change data in a non-reversible way: you have to do it right in the first pass.

        Well, yes, should the original data contain commas, it's gonna be a mess anyway