Sorting the csv file in Perl | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Sorting the csv file in Perl

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-27-2012
adisky123 adisky123 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 23 December 2013, 5:57 AM EST
Posts: 58
Thanks: 5
Thanked 0 Times in 0 Posts
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  
Old 12-27-2012
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 8 July 2014, 12:19 PM EDT
Location: Southern NJ, USA (Nord)
Posts: 4,378
Thanks: 8
Thanked 535 Times in 514 Posts
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  
Old 12-27-2012
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 21 July 2014, 11:56 PM EDT
Location: India
Posts: 1,816
Thanks: 24
Thanked 519 Times in 498 Posts

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  
Old 12-27-2012
spacebar's Avatar
spacebar spacebar is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 2 June 2014, 3:23 PM EDT
Location: spaceBAR Central
Posts: 440
Thanks: 0
Thanked 70 Times in 70 Posts
Quote:
Originally Posted by adisky123 View Post
I have a csv file which contain 32 column, I want to make sorting in that csv file with respect to 26th column.
Check out this info:
http://www.perlmonks.org/?node_id=512942

Last edited by spacebar; 12-27-2012 at 05:05 PM.. Reason: Inorrect link
Sponsored Links
    #5  
Old 12-27-2012
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 8 July 2014, 12:19 PM EDT
Location: Southern NJ, USA (Nord)
Posts: 4,378
Thanks: 8
Thanked 535 Times in 514 Posts
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  
Old 12-28-2012
adisky123 adisky123 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 23 December 2013, 5:57 AM EST
Posts: 58
Thanks: 5
Thanked 0 Times in 0 Posts
@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  
Old 12-28-2012
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 8 July 2014, 12:19 PM EDT
Location: Southern NJ, USA (Nord)
Posts: 4,378
Thanks: 8
Thanked 535 Times in 514 Posts
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
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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



All times are GMT -4. The time now is 10:36 AM.