Perl function to sort a file based on key fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl function to sort a file based on key fields
# 1  
Old 02-03-2009
Perl function to sort a file based on key fields

Hi,
I am new to PERL.I want to sort all the lines in a file based on 1,2 and 4th filelds.

Can U suggest me a command/function in perl for this operation..
# 2  
Old 02-03-2009
Sorting data with perl is data type sensitive. Are you sorting numbers or text or what? You will still need to use the sort function but if you sort numbers you use the '<=>' operator, if you sort text or mixed data you will most likely use the "cmp" operator. Can you post some sample lines of data and explain how you want to sort them? Are all fields sorted in the same order (ascending/descending) or are some sorted one way and others sorted another way?

Or you can try and figure it out yourself:

http://perldoc.perl.org/functions/sort.html
# 3  
Old 02-03-2009
sample data

The sample data looks like

/usr/apps|gen_run_script.ksh|#START|Jan 28th 01:16|/home/admin
/usr/apps|foreach_script.ksh|#START|Jan 28th 05:19|/home/admin
/usr/apps|splitting_files.ksh|#START|Jan 28th 09:10|/home/admin
.
.
.
.
. file contains hundreds of lines as shown

'|' is the delimiter..I want to sort each line of file based on 1,2 and 4th filed.only 4th filed is mixed(numeric,as it contains date)..


Can u tell the sort method in PERL..
# 4  
Old 02-03-2009
Not having a year in the date makes it difficult to sort the data by date. The sort routine will assume they are all in the same year.

Code:
use strict;
use warnings;
use Time::Local;
open (IN, '<', 'c:/perl_test/testlog.txt') or die "$!";
my @sorted = map {$_->[0]}
             sort {$a->[1] cmp $b->[1] || $a->[2] cmp $a->[2] || epoch($a->[3]) <=> epoch($b->[3]) }
             map {chomp; [ $_,(split(/\|/))[0,1,3] ];} <IN>;
close (IN);
print "$_\n" for @sorted;


{
   my %months = (Jan=>0,Feb=>1,Mar=>2,Apr=>3,May=>4,Jun=>5,Jul=>6,Aug=>7,Sep=>8,Oct=>9,Nov=>10,Dec=>11);
   sub epoch {
      my ($date) = $_[0];
      my ($mon,$mday,$hour,$min) = split(/[\s:]/,$date);
      $mday =~ tr/a-zA-Z//d;
      return timegm(0,$min,$hour,$mday,$months{$mon},0);
   }
}

# 5  
Old 02-03-2009
hi, not very clear about your requirement.

Anyway, pls try below and hope can help you some.
Code:
open FH,"<a.txt";
my @arr=<FH>;
sub usersort{
	my @t1=split(" ",$_[0]);
	my @t2=split(" ",$_[1]);
	my $a=sprintf("%s%s%s",$t1[0],$t1[1],$t1[3]);
	my $b=sprintf("%s%s%s",$t2[0],$t2[1],$t2[3]);
	return $a cmp $b;
}
print sort {usersort($a,$b)} @arr;

# 6  
Old 02-04-2009
That will not sort the dates and could be very inefficient since it calculates the sort keys over and over. The use of the cached key sort (Schwartzian Transform) I posted avoids that problem although the epoch() function could potentially be called more than once for the same sort key.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

EBCDIC File Split Based On Record Key

I was wondering if anyone could explain to me how to split a variable length EBCDIC file into seperate files based on the record key. I have the COBOL layout, and so I need to split the file into 13 different EBCDIC files so that I can run each one through a C++ converter I have, and get the... (11 Replies)
Discussion started by: hanshot1stx
11 Replies

2. Shell Programming and Scripting

How to Modify a file content in UNIX and sort for only required fields ?

I have the below contents in a file after making the below curl call curl ... | grep -E "state|Rno" | paste -sd',\n' | grep "Disconnected" > test "state" : "Disconnected",, "Rno" : "5554f1d2" "state" : "Disconnected",, "Rno" : "10587563" "state" : "Disconnected",, "Rno" :... (2 Replies)
Discussion started by: Vaibhav H
2 Replies

3. Shell Programming and Scripting

Fetch the values based on a Key using awk from single file

Hi, Please help to fetch the values for a key from below data format in linux. Sample Input Data Format 11055005|PurchaseCondition|GiftQuantity|1 11055005|PurchaseCondition|MinimumPurchase|400 11055005|GiftCatalogEntryIdentifier|Id|207328014 11429510|PurchaseCondition|GiftQuantity|1... (2 Replies)
Discussion started by: mohanalakshmi
2 Replies

4. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

5. Homework & Coursework Questions

Shell script calling Perl function, sort and find data, write to new files

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I must write a shell script that calls two external Perl functions--one of which sorts the data in a file, and... (6 Replies)
Discussion started by: kowit010
6 Replies

6. Shell Programming and Scripting

Merge two files based on a 3rd key file

Hi, I want to merge the two files based on the key file's columns. The key file: DATE~DATE HOUSE~IN_HOUSE CUST~IN_CUST PRODUCT~PRODUCT ADDRESS~CUST_ADDR BASIS_POINTS~BASIS_POINTS ... The other 2 files are From_file & To_file - The From_file: DATE|date/time|29|9 ... (9 Replies)
Discussion started by: dips_ag
9 Replies

7. UNIX for Dummies Questions & Answers

Sort the fields in a comma delimited file

Hi, I have a comma delimited file. I want to sort the fields alphabetically and again store them in a comma delimited file. For example, My file looks like this. abc,aaa,xyz,xxx,def pqr,ggg,eee,iii,qqq zyx,lmo,pqr,abc,fff and I want my output to look like this, all fields sorted... (3 Replies)
Discussion started by: swethapatil
3 Replies

8. Shell Programming and Scripting

sort function in perl

Hi, here is my perl script.This script creates an array and is sorting it using the in-built sort function in perl. #!/usr/local/bin/perl my number=6; my @num_arr=(1,2,3,4,5); my @array=(23,"$number","Hello",2.345,@num_arr); #printing the array print... (2 Replies)
Discussion started by: DILEEP410
2 Replies

9. Shell Programming and Scripting

how to replace a text inside a file based on a xml key

<c-param> <param-name>Number</param-name> <param-value>22</param-value> <description>my house number</description> </c-param> <c-param> <param-name>Address</param-name> ... (4 Replies)
Discussion started by: reldb
4 Replies

10. Shell Programming and Scripting

Need a Help with sort a text file with some fields

Ive got a file called listacdrs with this structure: 01/09/2006 12:13 p.m. 1.046.528 CF0155.DAT 01/09/2006 12:13 p.m. 1.046.528 CF0156.DAT 01/09/2006 12:13 p.m. 1.046.528 CF0157.DAT 01/09/2006 12:13 p.m. 1.046.528 CF0158.DAT 01/09/2006 12:14 p.m. ... (3 Replies)
Discussion started by: alexcol
3 Replies
Login or Register to Ask a Question