Print a comma except on the last line in Awk

前端 未结 6 1881
余生分开走
余生分开走 2021-01-02 00:30

I have the following script

awk \'{printf \"%s\", $1\"-\"$2\", \"}\' $a >> positions;

where $a stores the name of the fi

6条回答
  •  一个人的身影
    2021-01-02 01:16

    Single pass approach:

    cat "$a" | # look, I can use this in a pipeline! 
      awk 'NR > 1 { printf(", ") } { printf("%s-%s", $1, $2) }'
    

    Note that I've also simplified the string formatting.

提交回复
热议问题