0

Hi All,
I have no background in scripting in any of the languages.I am trying to write one awk script which can actually read a csv file and stable sort each column and return the output in pivoted form.
So, for the sample csv file below:

Akhtar,MNNIT,INDIA
Azhar,LU,PAKISTAN
Bob,MIT,USA
Raj,Purdue,INDIA
Ahmed,Illionis,USA
Akhtar,IITK,INDIA
Ramesh,IITB,INDIA
Bob,stanford,USA

I'll have the output as below:

<columnId,RowId,Value>

so the output will be:

0,0,Akhtar
0,1,Akhtar
0,2,Bob
0,7,Bob
........
........
2,0,INDIA
2,3,INDIA
2,5,INDIA
----------
and so on

I could try upto this:

sort --stable -t"," -k2 Lymphography.csv | awk -F, '{print 0","$1","$2}' > output.csv

but it does not return stable sort results. Also, I do not know how to write this as a program which reads input from file and sorts each column and print as mentioned above

Edited by mike_2000_17: Fixed formatting

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by thekashyap
0
# What you need is to look into the "Built-in Variables" section of "man awk"
sort --stable -t"," -k2 in.csv | awk -F, '{print 0 "," NR-1 "," $1}'
# NR - 1 as you want row num to start from 0 whereas awk does it from 1.
# Add more prints for $2, $3,...

Edited by thekashyap: Explain NR - 1

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.