Skip to content
Advertisement

Add an index column to a csv using awk

How can I add an index to a csv file using awk? For example lets assume I have a file

data.txt

JavaScript

I would like to add another column, which is the index. Basically I would like an output of

JavaScript

I was trying to use awk '{for (i=1; i<=NF; i++) print $i}' but it does not seem to be working right. And what is the best way to just add a comma for the first line but add incrementing number and a comma to the rest of the lines?

Advertisement

Answer

You may use this awk solution:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement