Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
08-25-2010, 10:26 AM
|
#1
|
|
LQ Newbie
Registered: Aug 2010
Posts: 2
Rep:
|
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.
|
|
|
|
08-25-2010, 10:29 AM
|
#2
|
|
Senior Member
Registered: Jul 2006
Location: Kolkata, India
Distribution: 64-bit GNU/Linux, Kubuntu64, Fedora QA, Slackware,
Posts: 2,717
Rep: 
|
This is extremely easy to do in R for example
|
|
|
|
08-26-2010, 07:25 PM
|
#3
|
|
Member
Registered: Feb 2007
Posts: 382
Rep:
|
why not using excel or Openoffice to open it. Re-arrange and then save it as csv.
|
|
|
|
08-26-2010, 07:38 PM
|
#4
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,305
|
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
BEGIN{ OFS=FS=","
one=1
two=2
. . .
seven=7
eight=8
}
{ print $(one),$(two),. . .,$(seven),$(eight) }
Now all you would need to do each time there is a change is set the variables in the BEGIN to the order you require.
|
|
|
|
08-27-2010, 04:52 AM
|
#5
|
|
LQ Newbie
Registered: Aug 2010
Posts: 2
Original Poster
Rep:
|
no it through error and want to change in to CSV file column heading and arrange accordingly ..
please help me out.
|
|
|
|
08-27-2010, 04:58 AM
|
#6
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,305
|
Sorry is that directed at me? Assuming it is, you do know you had to fill in the missing parts where I put . . .?
|
|
|
|
08-27-2010, 05:02 AM
|
#7
|
|
LQ Newbie
Registered: Aug 2010
Location: Bangalore, India
Distribution: Debian, Ubuntu
Posts: 3
Rep:
|
Perl can do this easily
perl -ne 'chomp $_; @a = split(/\s+/, $_); print join(",", $a[2], $a[3], $a[4], $a[0], $a[1]) . "\n"'
|
|
|
|
08-27-2010, 08:06 AM
|
#8
|
|
Senior Member
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,051
Rep:
|
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
|
|
|
|
08-27-2010, 09:19 AM
|
#9
|
|
LQ 5k Club
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
|
I don't understand the problem.
|
|
|
|
08-27-2010, 07:15 PM
|
#10
|
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,257
|
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
#
# LQ-govi1234.pl
#
# Usage: LQ-govi1234.pl 31245 csvFileName.dat
#
use strict; # of course...
my $order=shift;
my @columns = split //, $order;
while(<>){
@csv=split /\s+/, $_;
foreach my $column ( @columns ){
print "$csv[$column] ";
}
print "\n";
}
Should get you close.
--- rod.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:51 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|