1

i would to know how can i edit my csv file to add first column with name 'linenumber' and iterate through each line and insert the number of the line in this column ?

Something like that

TEST TEST2 TEST3 valu value value 

To that

linenumber TEST TEST2 TEST3 1 valu value value 

Thanks in advance

Stoufiler

2 Answers 2

1

Following awk should help you on same.

awk 'FNR==1{print "linenumber",$0;next} {print FNR-1,$0}' Input_file 

In case you need to edit the Input_file itself then following may help you.

awk 'FNR==1{print "linenumber",$0;next} {print FNR-1,$0}' OFS="," Input_file > temp_file && mv temp_file Input_file 
Sign up to request clarification or add additional context in comments.

9 Comments

it's work well but it's not a new column in fact they add the number and the valu :/
@Stoufiler, could you please explain your statement above, sorry didn't get it.
sorry i'm not english :) the problem is now the file contain the same number of column but the first one is concatenate so the title of the first column is linenumber TEST and no linenumber | TEST
@RavinderSingth13 Thanks, I'm going to try to explain myself correctly, Actually when I use the command you passed me the file is well modified but the linenumer column is not added, it is added to the first one so that the first one column equals linenumber + titleofthenextcolumn
@RavinderSingth13 It doesn't it make the same thing, i publish a codeshare for showing you y problem [link]codeshare.io/Gbw3xV
|
1

i found the answer, it's that

awk 'FNR==1{print "linenumber,"$0;next} {print FNR-1,$0}' OFS="," input.csv > output.csv 

thanks for all

3 Comments

@RavinderSingh13 i'm sorry to come back a new time but i would like to know how i can inverse the process so remove the first column ? :)
Sure, please edit your post or open a thread and let me know then on same,
@RavinderSingh13 it's ok this is the new post stackoverflow.com/questions/46904805/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.