Skip to content

How to print the heading of a tsv when using -H argument? #245

@AnabasisXu

Description

@AnabasisXu

First, thank you for developing 'GoAWK' with great csv/tsv support!

I am trying to implement the filter command of Miller with the -H argument of GoAWK:

mlr --icsv --opprint filter '$color == "red"' example.csv
color shape flag k index quantity rate
red square true 2 15 79.2778 0.0130
red circle true 3 16 13.8103 2.9010
red square false 4 48 77.5542 7.4670
red square false 6 64 77.1991 9.5310

https://miller.readthedocs.io/en/latest/10min/

With -H argument, NR==1 means the second line in GoAWK.

❯ goawk -itsv -H 'NR == 1 {print $0} ' 3.tsv
珊瑚    5

❯ cat 3.tsv
品种    价格
珊瑚    5
沙漠王  3

Using FIELDS does not seem to make it easier:

❯ goawk -i tsv -H '{ for (i=1; i in FIELDS; i++) printf "%s%s", FIELDS[i], (i < length(FIELDS) ? OFS : ORS); exit }' 3.tsv
品种 价格

Maybe just combine head -n 1 3.tsv with goawk -itsv -H 'NR == 1 {print $0} ' 3.tsv? It would mean two processes instead of one.

My current working function that implements the filter command of mlr:

function gtf() {
  # not sure how to do it with -H
  goawk -itsv 'NR == 1 {print $0}' "$3" 
  # filter lines
  goawk -itsv -H "@\"$1\" == $2 {print \$0}" "$3"
}

❯ gtf 价格 5 3.tsv
品种    价格
珊瑚    5
达尔奥索        5

❯ mlr --itsv --opprint filter '$价格==5' 3.tsv
品种   价格
珊瑚   5
达尔奥索 5

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