LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-17-2013, 08:37 AM   #1
jv61
LQ Newbie
 
Registered: May 2012
Posts: 24

Rep: Reputation: Disabled
How to sort lines from left to right?


Hello everyone,

I have a file which looks like this
Code:
c1       2:2     3:1     1:1     4:2
c2       3:2     1:1     4:1     2:2
c3       2:1     1:0     4:0     3:1
I want to sort each lines from left to right in ascending order from second column onwards. Any advice on how to do that?

My output would look like this:
Code:
c1       1:1   2:2   3:1    4:2
c2       1:1   2:2   3:2    4:1
c3       1:0   2:1   3:1    4:0
Thanks
 
Old 09-17-2013, 08:42 AM   #2
cbtshare
Member
 
Registered: Jul 2009
Posts: 645

Rep: Reputation: 42
have you done anything so far? Show us and we'll help you out.
 
Old 09-17-2013, 08:47 AM   #3
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
#!/usr/bin/perl -w

sub cb {
my $aa="";
my $bb="";
if ($a =~ /^(\d+)\D/) {$aa=$1;}
if ($b =~ /^(\d+)\D/) {$bb=$1;}
return ($aa <=> $bb);
}

while (<>) {
    chomp();
    @q=split(/\s+/);
    printf("%s ", $q[0]);
    shift @q;
    printf("%s\n", join(" ", sort cb @q));
}
 
1 members found this post helpful.
Old 09-17-2013, 08:52 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
A bash solution:
Code:
#!/bin/bash

while read LINE
do
  echo $LINE | tr "[:blank:]" "\n" | sort -g | tr "\n" "\t"
  echo
done < infile
I'm not sure if those are spaces or tabs separating the fields, adjust "\t" to " " if spaces are used
 
1 members found this post helpful.
Old 09-17-2013, 09:07 AM   #5
jv61
LQ Newbie
 
Registered: May 2012
Posts: 24

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by cbtshare View Post
have you done anything so far? Show us and we'll help you out.
I tried few different options.
First, I tried with perl
Code:
#!/usr/bin/perl -w

use strict;
use warnings;

open(IN,"test")or die;

while(my @INL =<IN>){

my @sorted = sort {$b cmp $a }(@INL);

    print @sorted,"\n";
}
Then I tried to transpose the file and use sort function to sort on multiple fields
Code:
sort -nk 1,1 -nk 2,2 t_test
Both didn't work.
 
Old 09-17-2013, 09:18 AM   #6
jv61
LQ Newbie
 
Registered: May 2012
Posts: 24

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by linosaurusroot View Post
Code:
#!/usr/bin/perl -w

sub cb {
my $aa="";
my $bb="";
if ($a =~ /^(\d+)\D/) {$aa=$1;}
if ($b =~ /^(\d+)\D/) {$bb=$1;}
return ($aa <=> $bb);
}

while (<>) {
    chomp();
    @q=split(/\s+/);
    printf("%s ", $q[0]);
    shift @q;
    printf("%s\n", join(" ", sort cb @q));
}
Thank you, that gives me desired results. Could you please explain the code a bit,that will help me to have a better understanding. Thanks again
 
Old 09-17-2013, 09:22 AM   #7
jv61
LQ Newbie
 
Registered: May 2012
Posts: 24

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
A bash solution:
Code:
#!/bin/bash

while read LINE
do
  echo $LINE | tr "[:blank:]" "\n" | sort -g | tr "\n" "\t"
  echo
done < infile
I'm not sure if those are spaces or tabs separating the fields, adjust "\t" to " " if spaces are used
Awesome,thank you.
 
1 members found this post helpful.
Old 09-17-2013, 09:41 AM   #8
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
If $a starts with numbers and then becomes something else set $aa to just the numbers.

Code:
if ($a =~ /^(\d+)\D/) {$aa=$1;}
That means data 1:0 9:0 10:0 will sort numerically into that order. Otherwise you might get 10 before 9 because 1 is before 9.

If you wanted you could try with http://en.wikipedia.org/wiki/Schwartzian_transform
 
1 members found this post helpful.
Old 09-17-2013, 12:51 PM   #9
jv61
LQ Newbie
 
Registered: May 2012
Posts: 24

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by linosaurusroot View Post
If $a starts with numbers and then becomes something else set $aa to just the numbers.

Code:
if ($a =~ /^(\d+)\D/) {$aa=$1;}
That means data 1:0 9:0 10:0 will sort numerically into that order. Otherwise you might get 10 before 9 because 1 is before 9.

If you wanted you could try with http://en.wikipedia.org/wiki/Schwartzian_transform
Thanks very much. I will look into that
 
  


Reply



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
[SOLVED] bash: sort lines in 2 files so that equal lines are at the same line number... masavini Programming 10 06-21-2012 01:58 PM
Sort Big files, /tmp/sortA3aLjF: No space left on device arvinarvin Linux - Newbie 3 08-03-2010 07:27 PM
Delete Duplicate Lines in a file, leaving only the unique lines left xmrkite Linux - Software 6 01-14-2010 06:18 PM
Is there a line limit with the sort utility? Trying to sort 130 million lines of text gruffy Linux - General 4 08-10-2006 08:40 PM
How can I sort the lines in a file? windhair Linux - Software 2 11-17-2005 08:37 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:51 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration