0

This question already has an answer here:

I have a file1 with the following:

1,joe,jim,bob,sally,mark,sue,etc,etc
2,julie,barbra,joe,jim,bob,sally,mark,sue,etc,etc
1,clarie,mike,joe,jim,bob,sally,,etc,etc
2,matt,alivia,joe,jim,bob,mark,sue,etc,etc
1,julie,claire,joe,jim,bob,sally,mark,sue,etc,etc
2,sophia,joe,jim,bob,sally,mark,sue,etc,etc

I would like to change it so column 1 was in numerical order and write to file2:

1,joe,jim,bob,sally,mark,sue,etc,etc
2,julie,barbra,joe,jim,bob,sally,mark,sue,etc,etc
3,clarie,mike,joe,jim,bob,sally,,etc,etc
4,matt,alivia,joe,jim,bob,mark,sue,etc,etc
5,julie,claire,joe,jim,bob,sally,mark,sue,etc,etc
6,sophia,joe,jim,bob,sally,mark,sue,etc,etc

my file1 has over 100,000 lines and has 65-90 columns per line.

marked as duplicate by sam, slm Dec 27 '16 at 16:48

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I say this often but I'll say it again. Unless your data is very well-formed (no special characters, no delimiters in quoted fields, etc.), don't use Unix CLI tools for CSV processing. Use a programming language with a real CSV parser like Perl or Python, or even a good old GUI spreadsheet application. Also you didn't actually ask a question here... – shadowtalker Dec 27 '16 at 15:45
  • It's actually a duplicate of Re-number a list – don_crissti Dec 27 '16 at 17:14
  • @don_crissti Is it a duplicate if no solution presented there works without more than trivial changes? – phk Dec 27 '16 at 17:26
  • @don_crissti Oh if there were such solutions where just such replacements would be necessary (this is what I meant with trivial, I should've been more clear) then I must have missed them, sorry. Concerning your 2nd point: Different parameters mean a different solution so yes, unless it was a very obvious just a typo parts that was fixed, the meaning/intent got changed in that case and those changes and are IMHO not right. But better have that discussion in a chat room, what do you think? – phk Dec 27 '16 at 19:52
  • @don_crissti Oh and I had no problem with your comment, it was really quite a genuine question. I am still not entirely sure what to mark as a dupe and what not (same goes for that one external project for training that AI [?] to better detect dupes). I thought that maybe this also plays a role, e.g. a more specific case is only a dupe of the general case if a good solution for the general case can be easily adapted for the specific one. In this case here concerning how exactly to change a column. – phk Dec 27 '16 at 19:58
1

Possible solution, assuming there are no escaped commas:

awk 'BEGIN { OFS=FS="," } { $1=NR; print }' input.csv > output.csv

Not the answer you're looking for? Browse other questions tagged or ask your own question.