The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Sorting : satyam_sat Shell Programming and Scripting 3 04-15-2008 01:30 PM
awk sorting Jaken Shell Programming and Scripting 4 05-21-2006 10:54 AM
Sorting mp3 penguin-friend Shell Programming and Scripting 3 05-13-2005 04:21 AM
Perl: sorting files by whats in'em quantumechanix Shell Programming and Scripting 4 12-14-2003 12:44 AM
Perl: Sorting an associative array tine Shell Programming and Scripting 2 10-29-2003 04:11 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-15-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Question perl sorting

I have many files that I need to sort each week. I know how to do in Unix, but for this task it appears best to do native inside an existing perl program. So, simplified, I have a file similar to the following:

Joe_________12_Main_St__A001________LX
Benny_______5_Spring____A002________LX
Will________2_A_St______A003________LX
Carl________15_X_Way____A004________LX


Fixed record format with
name in position 1
address in position 11
unique id in position 21
code in position 31

So, I already have the file open and about to close in the existing perl script - called $fileot1. I will close the file, and want to sort it.

How would I then write a few lines to sort the file descending on column 21? I have read the
@sorted = sort { lc($a) cmp lc($b) } @not_sorted
but do not know where to begin to define the parameters.

Example code is what I am looking for. Thanks.
(Sometimes, one can only understand a concept after seeing a solution.)
  #2 (permalink)  
Old 02-15-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,763
By parameters do you mean key fields. You cannot do that.
You need a subroutine:
Code:
# sort using explicit subroutine name
    sub sortsub {
	$age{$a} <=> $age{$b};	#  assumed number change this part for your needs
    }
    @sorted = sort sortsub @not_sorted;
  #3 (permalink)  
Old 02-15-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
a unix command would be

>sort rawfile -t'|' -k1.21,1.30 -r
Carl________15_X_Way____A004________LX
Will________2_A_St______A003________LX
Benny_______5_Spring____A002________LX
Joe_________12_Main_St__A001________LX


and that ordered my file reverse by what is in positions 21-30.
I did this by:
(a) specifying the 'tab' or key flag as the '|' which does not exist; thus all is considered field 1
(b) said to sort on k1.21,1.30 which translated to field one positions 21 to 30
(c) and the -r put in reverse order

So in perl, I figure somehow I need to define that I want to use character positions 21-30 as the key field. Then give a command to sort on those positions.
Perhaps this needs to be its own subroutine. I do not know.
  #4 (permalink)  
Old 02-15-2008
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
basic concept, May need fine tuning, I have to run but will check back later today:

Code:
@sorted = map{$_->[0]}
               sort {$b->[1] cmp $a->[1]}
               map {[$_,substr($_,20,10)]} <INPUT>;
<INPUT> is an open filehandle, change it to an array if the data is already in an array.
  #5 (permalink)  
Old 02-15-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Question perl sort read file/output file; fixed record length

Thanks KevinADC -- what you wrote gave the basis for what follows. I know I need to handle errors on file actions. Anything else I should be thinking about here?


#! /usr/bin/perl
# start of perl
# use strict;

# set vars
$source = "rawfile";
$outfile = ">rawsorted";

# open the source file
open(SOURCE,$source);
@rawdata=<SOURCE>;
# open the output file
open(OUTFILE,$outfile);

# sort at position 21 (note the 20) for length of 10
# sort in reverse order, hence the b before a in sort{ cmp } statement
@sorted = map{$_->[0]}
sort {$b->[1] cmp $a->[1]}
map {[$_,substr($_,20,10)]} @rawdata;

# write the sorted file to disk
print OUTFILE @sorted;

close SOURCE;
close OUTFILE;

my rawfile stored on disk =
> cat rawfile
Joe_________12_Main_St__A001________LX
Benny_______5_Spring____A002________LX
Will________2_A_St______A003________LX
Carl________15_X_Way____A004________LX


my final sorted file =
cat rawsorted
Carl________15_X_Way____A004________LX
Will________2_A_St______A003________LX
Benny_______5_Spring____A002________LX
Joe_________12_Main_St__A001________LX


p.s. How do people get their 'code' inserted in such a way to properly indent and format? I don't see a function for that on-screen.
  #6 (permalink)  
Old 02-15-2008
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
Assumes the lines of the file do not need to be filtered or validated before processing:

Code:
#! /usr/bin/perl
# start of perl
use strict;
use warnings;
# set vars
my $source = 'rawfile';
my $outfile = 'rawsorted';

# open the source file
open(SOURCE,$source) or die "Can't open $source: $!";
my @rawdata = <SOURCE>;
close SOURCE;
# open the output file
open(OUTFILE, '>', $outfile) or die "Can't open $outfile: $!";

# sort at position 21 (note the 20) for length of 10
# sort in reverse order, hence the b before a in sort{ cmp } statement
print OUTFILE map{$_->[0]}
              sort {$b->[1] cmp $a->[1]}
              map {[$_,substr($_,20,10)]} @rawdata;

close OUTFILE;
If this is a really big file it may use a lot of memory to store and process which can sometimes be a problem.

Use the code tags to display formatted code, see the FAQ section of this forum for details.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate 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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:44 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0