Skip to content

adding sequence numbers by group #322

Closed
@shenwei356

Description

@shenwei356

Assume this is the input ledger.csv:

transaction uuid,date,payee,amount
aaa123,2024-03-01,Bill's Garage,50
aaa123,2024-03-01,Bank account,50
456abc,2024-03-05,Solar Power Monopoly,200
456abc,2024-03-05,Credit card,200

and that we define a gawk program, seq.awk, which adds sequence numbers within transaction uuid:

# seq.awk:
BEGIN { OFS = "," }
NR == 1 { print $0, "n" }
NR > 1 { if ($1 == prev) ++n; else n=1; prev = $1; print $0, "payee " n }

Then we can run:

gawk --csv -f seq.awk ledger.csv | csvtk spread -k n -v payee

giving

transaction uuid,date,amount,payee 1,payee 2
aaa123,2024-03-01,50,Bill's Garage,Bank account
456abc,2024-03-05,200,Solar Power Monopoly,Credit card

I didn't notice anything in csvtk for adding sequence numbers by group but if it exists we could use that in place of the gawk program.

Originally posted by @ggrothendieck in #320

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