Sort flat file by 3rd column in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort flat file by 3rd column in perl
# 1  
Old 11-14-2011
Sort flat file by 3rd column in perl

Hello Guys

I want to sort a flat file by the third column (numeric ) and store it in some other name

I/P

Code:
 
9924873|20111114|00000000000013013|130|13|10/15/2010 12:36:22|W860944|N|00
9924873|20111114|00000000000013009|130|09|10/15/2010 12:36:22|W860944|N|00
9924873|20111114|00000000000029207|292|07|05/29/2001 10:35:32|DADS_JAMESA|N|00

O/P

Code:
 
9924873|20111114|00000000000013009|130|09|10/15/2010 12:36:22|W860944|N|00
9924873|20111114|00000000000013013|130|13|10/15/2010 12:36:22|W860944|N|00
9924873|20111114|00000000000029207|292|07|05/29/2001 10:35:32|DADS_JAMESA|N|00

Thanks a lot guys!!!!!!!!!!!
# 2  
Old 11-14-2011
Show any tries, thanks.
# 3  
Old 11-14-2011
Following on from zaxxon's reasonable request try coding up the following and coming back with specific questions that are causing you issues.
  • open the file
  • slurp records into an array
  • call Perl's sort with a sort function which uses the numeric comparison operator on $a and $b 3rd field
  • write the sorted array to the new file.
It's much more rewarding if you learn something in the process Smilie
# 4  
Old 11-14-2011
You guys are absolutely correct
Here what I come up with ...I dont have any working knowledge in perl

Code:
 
#!/usr/bin/env perl
use strict;
use warnings;
my %link_strengths;
while (<>) {
chomp;
my @rec = split /\|/; 
my $key = $rec[2]; 
# my ($link, $strength) = split /\s+/;
$link_strengths{$key} = $_;
}
my @sorted = sort {
$link_strengths{$a} <=> $link_strengths{$b}
} keys %link_strengths;
for my $link (@sorted) {
print "$link: $link_strengths{$link}\n";
}

I am confused how to store the third value and also how can I use the numeric comparison operator on $a and $b 3rd field .

Please help ,if you send some url it will be of gr8 help!!!!!!!!!
# 5  
Old 11-14-2011
In shell ..
Code:
$ sort -t\| +2n < infile

# 6  
Old 11-14-2011
Thanks but I need it in perl!!
# 7  
Old 11-14-2011
Code:
#!/usr/bin/perl
use strict;
use warnings;

open (my $data , '<', $ARGV[0])|| die "could not open $ARGV[0]:\n$!";
my @array=(<$data>);
my @sorted=sort {(split(/\|/,$a))[2]<=>(split(/\|/,$b))[2]} @array;
print @sorted;

The "magic" line here is the sort routine, we use split to create an unnamed temporary array and then the numeric comparison operator ( <=>, also known as the spaceship operator ), to provide a numeric comparison. Perl's sort function is extremely powerful precisely because one of the arguments can be a function, (have a look at perldoc -f sort).

The method you are trying to use above works for this case, however it depends on the third field being unique to each record.
Code:
#!/usr/bin/perl
use strict;
use warnings;

my (%link_strength);
open (my $data , '<', $ARGV[0])|| die "could not open $ARGV[0]:\n$!";
my @array=(<$data>);
for (@array){
    $link_strength{$1}=$_  if /(?:[^|]+\|){2}([^|]+)/;
}
print $link_strength{$_} for (sort {$a<=>$b} keys %link_strength);


Last edited by Skrynesaver; 11-14-2011 at 11:12 AM..
This User Gave Thanks to Skrynesaver For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Solution for replacement of 4th column with 3rd column in a file using awk/sed preserving delimters

input "A","B","C,D","E","F" "S","T","U,V","W","X" "AA","BB","CC,DD","EEEE","FFF" required output: "A","B","C,D","C,D","F" "S", T","U,V","U,V","X" "AA","BB","CC,DD","CC,DD","FFF" tried using awk but double quotes not preserving for every field. any help to solve this is much... (5 Replies)
Discussion started by: khblts
5 Replies

2. Shell Programming and Scripting

Sort based on positions in flat file

Hello, For example: 12........6789101112..............20212223242526..................50 ( Positions) LName FName DOB (Lastname starts from 1 to 6 , FName from 8 to 15 and date of birth from 21 to29) CURTIS KENNETH ... (5 Replies)
Discussion started by: duplicate
5 Replies

3. UNIX for Dummies Questions & Answers

sort a unix file by 3rd column

Hi, Can anybody tell me how to sort a unix file by 3rd column and not by ltr? Please help Thanks in advance (2 Replies)
Discussion started by: vinnyvirk
2 Replies

4. Shell Programming and Scripting

Summation of column value in flat file

Hello Guys Please find my below requirement I have a flat file with column headers in first line and data The structure like below col1 col2 col3 A 1 2 B 3 4 C 5 6 Say I have to take the summation of col2 (that will depend on the... (2 Replies)
Discussion started by: Pratik4891
2 Replies

5. UNIX for Dummies Questions & Answers

Sort after 2. column in array in Perl

Hey How do I sort an array numerically after the second column? My values are integers like 1, 2, 3, 4... and they are not unique, so I can't just reverse my hash and sort by keys. I wanna sort my file/array so that I get the lines with the highest value in the top - that is descending. ... (2 Replies)
Discussion started by: Banni
2 Replies

6. Shell Programming and Scripting

How to read the first column in a flat file with ~ as delimiter

I have one flat file like below id1~col~batch1 id2~col2~batch2 id3~col3~batch3 I need to read the first column one by one and I need to write one db2 query based on that column1 Like for (i=0;i<=10;i++) do insert into table column (con_id) values (select column from table where... (4 Replies)
Discussion started by: siri_886
4 Replies

7. Shell Programming and Scripting

Flat File column manipulation

Hi All, I have a tab delimited input file with say 4 fields (columns) as below : 0000443 1AGPR061 2006 Daiml 0002198 1B3XG0K2 1989 Chdds 0002199 1Bd64J0L 1990 Ch34s 0002275 1B3s4J0K 1989 Chadys 0002276 1B465302 2002 Dageml 0002290 1B45430K 1989 Cays I want the 2nd column in file to... (5 Replies)
Discussion started by: net
5 Replies

8. Shell Programming and Scripting

Converting Column to Rows in a Flat file

Hi, Request To guide me in writing a shell program for the following requirement: Example:if the Input File contains the follwing data Input File Data: 80723240029,12,323,443,88,98,7,98,67,87 80723240030,12,56,6,,,3,12,56,6,7,2,3,12,56,6,7,2,3,88,98,7,98,67,87... (5 Replies)
Discussion started by: srinikal
5 Replies

9. UNIX for Dummies Questions & Answers

Trim String in 3rd Column in Tab Delimited File...SED/PERL/AWK?

Hey Everybody, I am having much trouble figuring this out, as I am not really a programmer..:mad: Datafile.txt Column0 Column1 Column2 ABC DEF xxxGHI I am running using WGET on a cronjob to grab a datafile, but I need to cut the first three characters from... (6 Replies)
Discussion started by: rickdini
6 Replies

10. Shell Programming and Scripting

Look up column in a flat file

Here is on more go ! Need a shortcut for my problem ! problem is i have a look_update with fixed sequence of column that is : MANDT:SERAIL:SERSCHA:SEREX:EQTYP:BSTVP I will be getting data in a flat file having same number of column but the sequence could be different in each... (5 Replies)
Discussion started by: jambesh
5 Replies
Login or Register to Ask a Question