|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Sorting the csv file in Perl
Hi All
How all are doing today. Just struck in an issue in Perl I have a csv file which contain 32 column, I want to make sorting in that csv file with respect to 26th column. Is it possible to do so without any module being added? Regards Aditya ---------- Post updated at 10:02 AM ---------- Previous update was at 09:55 AM ---------- FYI I am usinf windows Perl |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
You could install cygwin to get the nicer sort. A CSV file with commas in the data is particularly hard to sort, even for the unix sort. I would just read the CSV with PERL libraries and put every row into a sorted container like a tree (not a hash), so I can fetch them in order.
http://perl.plover.com/BTree/ http://search.cpan.org/~bryce/Sort-T...pm#DESCRIPTION http://search.cpan.org/~rsavage/Tree...pm#DESCRIPTION Many Perl containers can be sorted: http://search.cpan.org/~chthorman/Da...ble.pm#SORTING http://search.cpan.org/~uri/Sort-Mak...pm#DESCRIPTION Wading through all this, first you have to avoid one dimensional sorts unless you want to reorder your csv lines with the key first, or copy the key first as N bytes left justified so you can easily remove it. You do not need manually managed parent-child trees. Hash containers have their own hash based sort order, so when they say hash tree, the mean hash map, not tree map. I read an echo of my words here: http://www.perlmonks.org/index.pl?node_id=599204 Last edited by DGPickett; 12-27-2012 at 01:44 PM.. |
| Sponsored Links | ||
|
|
#3
|
||||
|
||||
|
Code:
use warnings;
use strict;
my (%hash, @arr);
open FILE, "< c:/path/to/file.csv";
while (<FILE>) {
chomp;
@arr = split /,/;
$hash{$arr[25]} = join(',',@arr[0..24]) . '|' . join(',',@arr[26..$#arr]);
}
close FILE;
undef @arr;
for (sort keys %hash) {
@arr = split /\|/, $hash{$_};
print "$arr[0],$_,$arr[1]\n";
} |
|
#4
|
||||
|
||||
|
Quote:
http://www.perlmonks.org/?node_id=512942 Last edited by spacebar; 12-27-2012 at 05:05 PM.. Reason: Inorrect link |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
A CSV file with commas in the data is particularly hard to sort, even for the unix sort, e.g.: Code:
1,2,3,"A CSV file with commas in the data is particularly hard to sort, even for the unix sort.",5,6\r\n Luckily, or sadly, many data sets are embedded-comma-free, lulling the unwary into forgetfulness ! Early versions of MS Access had this problem, forcing me to use tab or pipe (|) separated text. |
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
@spacebar while using sort command I am getting an error. Code:
syntax error at C:\Users\aditya\Documents\Perl Prgms\compare.pl line 35, near "3 -t " ---------- Post updated at 01:56 AM ---------- Previous update was at 01:50 AM ---------- @balajesuri getting an error Code:
Use of uninitialized value in join or string at C:\Users\aditya\Documents \Perl Prgms\sort_check.pl line 10, <FILE> line 476. Last edited by Franklin52; 12-28-2012 at 08:09 AM.. Reason: Please use code tags for data end code samples |
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
Well, the link is showing shell (executable /bin/sort), not perl sort() built-in, and seems to have lost some line breaks in the data, too.
|
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Need help in writing a routine for sorting a CSV file | avikaljain | Shell Programming and Scripting | 6 | 04-15-2011 12:25 AM |
| UNIX sorting - csv file | rajani_p | UNIX and Linux Applications | 0 | 08-24-2009 06:31 AM |
| sorting csv file based on column selected | tententen | Shell Programming and Scripting | 4 | 07-05-2009 10:17 PM |
| Help sorting .csv file | Jazmania | Shell Programming and Scripting | 3 | 11-14-2008 10:36 AM |
| CSV File parse help in Perl | lodey | Shell Programming and Scripting | 13 | 03-31-2008 09:06 AM |
|
|