awk csv文件附加数字

awk csv文件附加数字

我有以下 CSV 文件

vol123,12/05/2016,ABC
vol123,12/05/2016,ABC
vol123,12/05/2016,ABC
vol123,14/05/2016,ABC
vol123,13/05/2016,ABC
vol123,12/05/2016,ABC
vol123,12/05/2016,ABC
vol123,18/05/2016,ABC
vol123,12/05/2016,ABC
vol123,12/05/2016,ABC
vol123,12/05/2016,ABC
vol123,12/05/2016,ABC
vol123,19/05/2016,ABC
vol123,12/05/2016,ABC
vol123,12/05/2016,ABC
vol123,20/05/2016,ABC
vol123,12/05/2016,ABC
vol123,12/05/2016,ABC
vol123,22/05/2016,ABC
vol123,12/05/2016,ABC
vol123,12/05/2016,ABC

我想将其更改如下,

vol123,12/05/2016,ABC1
vol123,12/05/2016,ABC2
vol123,12/05/2016,ABC3
vol123,14/05/2016,ABC4
vol123,13/05/2016,ABC5
vol123,12/05/2016,ABC6
vol123,12/05/2016,ABC7
vol123,18/05/2016,ABC8
vol123,12/05/2016,ABC9
vol123,12/05/2016,ABC10
vol123,12/05/2016,ABC1
vol123,12/05/2016,ABC2
vol123,19/05/2016,ABC3
vol123,12/05/2016,ABC4
vol123,12/05/2016,ABC5
vol123,20/05/2016,ABC6
vol123,12/05/2016,ABC7
vol123,12/05/2016,ABC8
vol123,22/05/2016,ABC9
vol123,12/05/2016,ABC10
vol123,12/05/2016,ABC1

我想将数字附加到最后一列。数字范围是 1 到 10。我想在第 11 条记录上再次使用 1,因此追加应该在循环中工作。

请帮助我,因为我无法使用 awk 命令得到这个。

答案1

您可以使用awk以下方法将行号 ( NR) 附加到每行:

awk '{print $0 NR}' file

如果要打印行号除以 10 后的余数:

awk '{print $0 NR%10}' file

这将打印您想要的内容,除了它打印0而不是10.如果您需要10,您可以计算(NR - 1)%10 + 1

awk '{print $0 (NR - 1)%10 + 1}' file

结果:

vol123,12/05/2016,ABC1
vol123,12/05/2016,ABC2
vol123,12/05/2016,ABC3
vol123,14/05/2016,ABC4
vol123,13/05/2016,ABC5
vol123,12/05/2016,ABC6
vol123,12/05/2016,ABC7
vol123,18/05/2016,ABC8
vol123,12/05/2016,ABC9
vol123,12/05/2016,ABC10
vol123,12/05/2016,ABC1
vol123,12/05/2016,ABC2
vol123,19/05/2016,ABC3
vol123,12/05/2016,ABC4
vol123,12/05/2016,ABC5
vol123,20/05/2016,ABC6
vol123,12/05/2016,ABC7
vol123,12/05/2016,ABC8
vol123,22/05/2016,ABC9
vol123,12/05/2016,ABC10
vol123,12/05/2016,ABC1

相关内容