LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

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

Rep: Reputation: 0
sorting data


Hi

I have a large number of text files:

trialdata_1.txt
trialdata_2.txt
.
.
.

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

#!/usr/bin/perl
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.

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

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

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


Cheers,
Tink

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


Reply

Tags
perl


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 08:50 PM.

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