Skip to main content
2 of 5
added 23 characters in body
jubilatious1
  • 3.9k
  • 10
  • 20

Using Raku (formerly known as Perl_6)

~$ raku -ne '.put unless grep {.chars == 0}, .split(",")[0..*-2]'  file

OR:

~$ raku -ne 'given .split(",", :skip-empty) {.join(",").put if .elems > 3}'  file

The first answer above is very similar to the Perl answer given by @StéphaneChazelas (Thank you, Stéphane!). Since the lines end with a comma (which can be interpreted as a trailing blank column), the index [0..*-2] is used which drops this last (empty) element. Note, *-1 is the last element of an array-like structure in Raku, while something like [0..*] also works (means 'give me everything'). Raku has "Zen slices" as well, so .split(",")[] is valid syntax.

In fact Raku's split routine has a :skip-empty parameter (i.e. "adverb") which can take the place of grepping for .chars == 0. So you are left with deciding how many columns are required in the output. Here .elems > 3 asks for a minimum of 4 columns.

Technically, the second answer is the correct answer, because the first answer leaves the trailing comma in place, while the second answer removes it.

Sample Input:

aaaa,bbbb,cccc,dddd,
,bbbb,cccc,dddd,
aaaa,,cccc,dddd,
aaaa,,,dddd,
,,,dddd,

Sample Output (first answer):

aaaa,bbbb,cccc,dddd,

Sample Output (second answer):

aaaa,bbbb,cccc,dddd

https://docs.raku.org
https://raku.org

jubilatious1
  • 3.9k
  • 10
  • 20