在 csv 文件中添加新列作为行号

在 csv 文件中添加新列作为行号

我创建了一个 csv 文件,但忘记添加行号作为第一列。对于大文件,最快的方法是什么?

262145,umbrella,umbrella,carrot,carrot,carrot,carrot,person,person,person,person,person,person,person,person,person,person,person,person,umbrella,carrot,carrot,carrot,carrot,carrot,carrot,carrot,carrot,person,umbrella,carrot,person,carrot
262146,person,skis,backpack
524291,dog,dog,person,frisbee
393223,toothbrush,person
393224,tie,person,person,person,person,person
524297,person,person,person,bear
393227,person,skateboard
131084,motorcycle,person
393230,person,skis
524311,chair,person,remote
393241,frisbee,person,person,person,person,person,person,person,person,person,car,car,person,car,car,person,person,person
524314,car,person,person,teddy bear,car,handbag,cell phone,car,handbag,teddy bear
262171,boat,person,person,person,person,person,person,boat,person
131101,umbrella,person,person,backpack,handbag,person
524320,tie,person,person,suitcase,person

我想要这样的东西:

1,262145,umbrella,umbrella,carrot,carrot,carrot,carrot,person,person,person,person,person,person,person,person,person,person,person,person,umbrella,carrot,carrot,carrot,carrot,carrot,carrot,carrot,carrot,person,umbrella,carrot,person,carrot
2,262146,person,skis,backpack

答案1

创建带有行号的新文件并删除旧文件

awk '{printf "%s,%s\n", NR,$0}' input.txt > new.txt

相关内容