in reply to Re^2: splinting a line of text by comma (updated)
in thread splinting a line of text by comma

That is printing lines with commas. How about this with just a small tweak to allow commas in the first and/or last field.

#!/usr/bin/perl # https://perlmonks.org/?node_id=1226189 use strict; use warnings; while( <DATA> ) { my @queue = $_; while( @queue ) { local $_ = shift @queue; if( / [^|,\n]* (?: , [^|,\n]* )+ /x ) { push @queue, map "$`$_$'", split /,/, $&; } else { print; } } } __DATA__ some text foo | some text BAR | oh , no , commas | some text BAZ | som +e text QUX some text Oof | some, text, BAR | some , text , BAZ | some text QUX some,text,FOO | some text BAR | one two | some , text , QUX

Outputs:

some text foo | some text BAR | oh | some text BAZ | some text QUX some text foo | some text BAR | no | some text BAZ | some text QUX some text foo | some text BAR | commas | some text BAZ | some text QUX some text Oof | some| some | some text QUX some text Oof | some| text | some text QUX some text Oof | some| BAZ | some text QUX some text Oof | text| some | some text QUX some text Oof | text| text | some text QUX some text Oof | text| BAZ | some text QUX some text Oof | BAR | some | some text QUX some text Oof | BAR | text | some text QUX some text Oof | BAR | BAZ | some text QUX some| some text BAR | one two | some some| some text BAR | one two | text some| some text BAR | one two | QUX text| some text BAR | one two | some text| some text BAR | one two | text text| some text BAR | one two | QUX FOO | some text BAR | one two | some FOO | some text BAR | one two | text FOO | some text BAR | one two | QUX

Replies are listed 'Best First'.
Re^4: splinting a line of text by comma
by AnomalousMonk (Archbishop) on Nov 23, 2018 at 04:11 UTC
    ... one of the columns has multiple values separated with a comma like an array. ... unwind the array so each value of the array is on a separate line and all other fields are duplicated or carried with it ... [from the OP]

    Admittedly not the clearest specification. I was focusing on the "... all other fields are duplicated or carried with it ..." part, which I took to mean that some other fields with commas might be printed.

    ... mongo documentation of $unwind does what I'd like ...

    I don't understand what this means. Is "mongo" a thing?


    Give a man a fish:  <%-{-{-{-<

      I saw the "one of the columns" also and submitted my first solution. Then I thought "why not over design it", since the future is infinite and sometime in there someone will want multiple columns :)