Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


Search this Thread
Old 02-23-2011, 05:22 PM   #1
LQ Newbie
Registered: Feb 2011
Posts: 10

Rep: Reputation: 0
sorting data


I have a large number of text files:


each with three columns of numbers

In each file I want to order the rows according to the first column.

So data looking like

1 2 2
2 4 5
8 8 6
6 5 7
9 10 8

would become

1 2 2
2 4 5
6 5 7
8 8 6
9 10 8

my code right now is

my $Input = trialdata_1.txt;
open (Data, "<$Input");
my @data1 = <Data>;
my @data2 = sort {$a <=> $b } @data1;
print @data2;

the two problems I'm having are:

1. How do I adjust the datafile instead of having the desired data printed in my terminal

2. How do I make the code work on multiple files.

Any help would be much appreciated.

Old 02-23-2011, 07:54 PM   #2
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,974
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
Silly question, why don't you use "sort" on the commandline?

for i in trialdata*txt; 
    sort -k1,1n $i > ${i}.tmp && mv ${i}.tmp $i


Last edited by Tinkster; 02-23-2011 at 07:58 PM.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Sorting your data with msort LXer Syndicated Linux News 0 05-19-2008 11:00 AM
sorting 2D data elsheikhmh Programming 10 07-04-2007 09:02 AM
What software for sorting/grouping data from Excel SML Linux - Software 3 05-21-2007 07:43 AM
Sorting data. unreal128 Linux - Software 1 09-20-2005 06:31 PM
Sorting data in output file lluciano Linux - General 2 08-19-2005 10:37 AM

All times are GMT -5. The time now is 07:31 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration