Skip to content

csvtk mutate2 ternary with regex of strings #361

@avilella

Description

@avilella

Hi,

I am trying to classify the strings of a column as 3 categories: "pureones" when the whole string is composed of the character 1, of different lengths, the same for "purezeros" and the rest being "mixed".

I have this version below which isn't working fully because I can't check for different lengths of "pureones". For example, if the value is 111 or 1111 or 11111 or 1111111 it won't match it as pureones. How would I do that? Thanks

echo -e "foo\n111111\n1100\n0000\n00000111\n111001000" | csvtk mutate2 -n type -e '$foo == 111111 ? "pureones" : ($foo == 00000 ? "purezeros" : "mixed")'
foo,type
111111,pureones
1100,mixed
0000,purezeros
00000111,mixed
111001000,mixed

Bests,

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions