Rearrange the column in CSV file..
Hi All,
I have the csv file with the below column, Gender Modifier LastNameMiddleName FirstName Female Nurse AASER L ROCHELLE Female Nurse AASER L ROCHELLE Female Nurse ABBOTT L MARY Female Nurse ABBOTT L MARY I need a script to rearrange the column but i can do like below, awk -F"," '{ print $1",",$4",",$5",",$6",",$3",",$8",",$7",",$2 }' file2.csv > file3.csv But the column postion will be change everytime, may be next time will be like this LastNameMiddleName FirstName Gender Modifier AASER L ROCHELLE Female Nurse AASER L ROCHELLE Female Nurse ABBOTT L MARY Female Nurse ABBOTT L MARY Female Nurse Please provide the scripts. |
This is extremely easy to do in R for example
|
why not using excel or Openoffice to open it. Re-arrange and then save it as csv.
|
Well I am not sure there is an easy way if it is going to be constantly changing, but the following idea could work:
Code:
#!/usr/bin/awk -f |
no it through error and want to change in to CSV file column heading and arrange accordingly ..
please help me out. |
Sorry is that directed at me? Assuming it is, you do know you had to fill in the missing parts where I put . . .?
|
Perl can do this easily
perl -ne 'chomp $_; @a = split(/\s+/, $_); print join(",", $a[2], $a[3], $a[4], $a[0], $a[1]) . "\n"'
|
what I'd do in perl is grab the first line and split it, insert that into a hash with the position in the split as the value, then when you split every line after that you just print the string you want using the label
chomp; @data = split(' ') print join(",", $data[$pos{'LastNameMiddleName'}], $data[$pos{'FirstName'}], $data ... then it's irrelevant the order the data comes in |
I don't understand the problem.
|
You have a 'CSV' (where are the 'C's?), with 5 columns of data. So there are a fairly small number of possible orders for the columns. Use an input string that contains the column numbers, ordered from 0 of course. Untested...
Code:
#! /usr/bin/perl -w --- rod. |
All times are GMT -5. The time now is 04:57 AM. |