in reply to Re: splinting a line of text by comma
in thread splinting a line of text by comma

Still very much not recommended :)

#!/usr/bin/perl # https://perlmonks.org/?node_id=1226189 use strict; use warnings; print glob qq("$_") =~ s/[^|\n]+/{$&}/gr while <DATA>; __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^3: splinting a line of text by comma
by LanX (Saint) on Nov 23, 2018 at 17:02 UTC
    yeah, better.

    I was already in bed half sleeping and was surprised about whitespace handling in glob.

    > Still very much not recommended :)

    Well if you find a way to escape curlies and other "wildcard characters" from the input... :)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      if you find a way to escape curlies and other "wildcard characters"

      Sure, just move them out of the way :)

      #!/usr/bin/perl # https://perlmonks.org/?node_id=1226189 use strict; use warnings; #print glob qq("$_") =~ s/[^|\n]+/{$&}/gr while <DATA>; print map tr/\1-\7/{}[]"*?/r, glob +('"' . tr/{}[]"*?/\1-\7/r . '"') =~ s/[^|\n]+/{$&}/gr while <DATA>; __DATA__ some text foo | some text BAR | oh , no , commas | some text BAZ | som +e text QUX some,text,FOO{3} | 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| 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{3} | some text BAR*? | "one" two | some FOO{3} | some text BAR*? | "one" two | text FOO{3} | some text BAR*? | "one" two | QUX
        I had the same idea° but that's not really "escaping".

        And for a general purpose solution you would need to escape \1..\7 too. I'm afraid that's not feasible with tr.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        °) using this technique in my Wiki-nodelet-hack already