Sponsored Content
Full Discussion: perl sorting
Top Forums Shell Programming and Scripting perl sorting Post 302167801 by joeyg on Friday 15th of February 2008 01:32:36 PM
Old 02-15-2008
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.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl: sorting files by whats in'em

i'm halfway into my script and realize i may need to use an associative array (a hash, i guess they are called in Perl). i'm fairly new to Perl and don't know how to use them correctly. i have some files in a directory. each file contains a number in a specific place in it's header. what i would... (4 Replies)
Discussion started by: quantumechanix
4 Replies

2. Shell Programming and Scripting

PERL data - sorting

Hello, I have a page where multiple fields and their values are displayed. But I am able to sort only a few fields. When I looked into the issue, it is seen that the for each row of info , an unique id is generated and id.txt is generated and saved. Only those fields which are inside that id.txt... (3 Replies)
Discussion started by: eagercyber
3 Replies

3. Shell Programming and Scripting

Perl: sorting by string

I have an array full of string values that need to be sorted, but if a value starts with (regex) 0^ it should be at the beginning of the array. Otherwise the array should be sorted normally using ascii sort. Please help me create the sub to pass to the sort function. (7 Replies)
Discussion started by: dangral
7 Replies

4. Shell Programming and Scripting

perl array sorting

Hi All, I have an array in perl as @match = (201001,201002,201001,201002); I am trying to sort this array as @match = sort(@match); print "@match"; I dont see the output sorted any answers I also tried another way, but still the results are not sorted foreach my $match (sort { $a... (2 Replies)
Discussion started by: bsdeepu
2 Replies

5. Shell Programming and Scripting

Perl sorting

Hi, I have a file in this format: a b c d e a b c d e a b c d e i need perl script to sort 2nd column in alphabatical order The script i use is #!/usr/bin/perl my @words=<>; foreach(sort mysort @words) { print; (4 Replies)
Discussion started by: Learnerabc
4 Replies

6. Shell Programming and Scripting

perl sorting variables

Good morning!! Im trying to practice withe Perl and sorting variables is not going good at all! #!/usr/bin/perl $username = $ENV {'LOGNAME'}; print "Hello, $username\n"; I want to add sort and 'mail' and 'home'. This below is what I have came up with,but of course its not working. ... (5 Replies)
Discussion started by: bigben1220
5 Replies

7. Shell Programming and Scripting

Sorting a particular column in PERL

I have a file abc.txt which contains data in th following format abc,23 hgfh,87 tweg,89 jdfjn,74 I want to sort on the basis of column (the second one). It should be numerical sort. output should be abc,23 jdfjn,74 hgfh,87 tweg,89 I know how to do it in unix. I need a PERL code (1 Reply)
Discussion started by: centurion_13
1 Replies

8. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

9. Shell Programming and Scripting

Sorting dates in Perl

I have a directory of backup files. named like this: ldap.data.04-06-2012.tar ldap.data.03-06-2012.tar ldap.data.02-06-2012.tar ldap.data.01-06-2012.tar ldap.data.31-05-2012.tar ldap.data.30-05-2012.tar ldap.data.29-05-2012.tar ldap.data.28-05-2012.tar ldap.data.27-05-2012.tar... (6 Replies)
Discussion started by: robsonde
6 Replies

10. Shell Programming and Scripting

Perl Sorting Help

Hey guys, I have started to learn perl recently because of a position I took. They want me to master perl and I've been reading books and practicing myself. Basically I,m having my perl script run through a text pad and give the output in a special way e.g input deviceconfig { ... (5 Replies)
Discussion started by: zee3b
5 Replies
Net::LDAP::Control::SortResult(3)			User Contributed Perl Documentation			 Net::LDAP::Control::SortResult(3)

NAME
Net::LDAP::Control::SortResult - Server Side Sort (SSS) result control object SYNOPSIS
use Net::LDAP::Control::Sort; use Net::LDAP::Constant qw(LDAP_CONTROL_SORTRESULT); use Net::LDAP::Util qw(ldap_error_name); $sort = Net::LDAP::Control::Sort->new( order => "cn -age" ); $mesg = $ldap->search( @args, control => [ $sort ]); ($resp) = $mesg->control( LDAP_CONTROL_SORTRESULT ); if ($resp) { if ($resp->result) { my $attr = $resp->attr; print "Problem sorting, ",ldap_error_name($resp->result); print " ($attr)" if $attr; print " "; } else { print "Results are sorted "; } } else { print "Server does not support sorting "; } DESCRIPTION
"Net::LDAP::Control::SortResult" is a sub-class of Net::LDAP::Control. It provides a class for manipulating the LDAP sort request control 1.2.840.113556.1.4.474 as defined in RFC-2891 A sort result control will be returned by the server in response to a search with a Server Side Sort control. If a sort result control is not returned then the user may assume that the server does not support sorting and the results are not sorted. CONSTRUCTOR ARGUMENTS
attr If "result" indicates that there was a problem with sorting and that problem was due to one of the attributes specified in the sort control. "attr" is set to the name of the attribute causing the problem. result This is the result code that describes if the sort operation was sucessful. If will be one of the result codes describes below. METHODS
As with Net::LDAP::Control each constructor argument described above is also available as a method on the object which will return the current value for the attribute if called without an argument, and set a new value for the attribute if called with an argument. RESULT CODES
Possible results from a sort request are listed below. See Net::LDAP::Constant for a definition of each. LDAP_SUCCESS LDAP_OPERATIONS_ERROR LDAP_TIMELIMIT_EXCEEDED LDAP_STRONG_AUTH_REQUIRED LDAP_ADMIN_LIMIT_EXCEEDED LDAP_NO_SUCH_ATTRIBUTE LDAP_INAPPROPRIATE_MATCHING LDAP_INSUFFICIENT_ACCESS LDAP_BUSY LDAP_UNWILLING_TO_PERFORM LDAP_OTHER SEE ALSO
Net::LDAP, Net::LDAP::Control::Sort, Net::LDAP::Control, http://ww.ietf.org/rfc/rfc2891.txt AUTHOR
Graham Barr <gbarr@pobox.com> Please report any bugs, or post any suggestions, to the perl-ldap mailing list <perl-ldap@perl.org> COPYRIGHT
Copyright (c) 1999-2004 Graham Barr. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.16.3 2013-06-07 Net::LDAP::Control::SortResult(3)
All times are GMT -4. The time now is 12:52 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy