Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 17
Like Tree1Likes
I have a big file with numbers (tab-separated) for instance: Code: 100 26 36 4 8 56 100 80 36 55 59 78 100 38 96 59 77 66 100 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2012
    Posts
    56

    Sort numbers in tab-separated file line per line


    I have a big file with numbers (tab-separated) for instance:
    Code:
    100  26  36  4  8
    56  100  80   36  55
    59  78  100 38  96
    59  77  66  100  52
    59  59  44 32  100
    I want to get the highest number(s) and its position for each line. Please note that I donít want the number witch is in the same position with the number of line. For example, the highest number of line 1 is 36, and for line5 59.
    The final result for the example file should be (first the number of the position and then the score):
    Code:
    3 36 
    3 80
    5 96
    2 77
    1 59  2 59
    Any comments and help are welcome. I would prefer perl or awk.

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    hi,

    here's how i'd do this one in perl:

    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    my $file = shift || die "Give me the file\n";
    open(FH,'<',$file) or die "can't read '$file': $!\n";
    while(<FH>){
      chomp;
      my @nums = split(' ',$_);
    
      my $big;
      for(my $n = 0;$n<=$#nums;$n++){
        next if($n+1 == $.);
        my $val = $nums[$n];
        if(defined($big)){
          $big = $val if($val > $big);
        }else{
          $big = $val;
        }
      }
      for(my $n = 0;$n<=$#nums;$n++){
        next if($n+1 == $.);
        my $val = $nums[$n];
        if($val == $big){
          print $n+1," $val ";
        }
      }
      print "\n";
    }
    close(FH);

  3. #3
    Just Joined!
    Join Date
    Apr 2012
    Posts
    56
    Thank you very much for your help. It works!!! Could you please tell me how to short the numbers if they are in 123||456 tab 159||456 ... format? In this case I want to sort them according the first part.
    Thanks again for your help.

  4. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by corfuitl View Post
    Could you please tell me how to short the numbers if they are in 123||456 tab 159||456 ... format?
    sry, not sure what you mean there. can you post some example text?

  5. #5
    Just Joined!
    Join Date
    Apr 2012
    Posts
    56
    hi,
    thanks for your reply. Basically, I use a script in order to extract the above file. Its structure looks like 1212||1515 \t 5451||1511 \t .... I have two results for each item separated with || and every item is separated with tab. How can I sort the list using the first number, but at the result I need both.
    If you still cannot understand, no problem. I will modify the original script and I will extract them into two different files.
    Thanks once again for your time!!!

  6. #6
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    so do you mean your text file might look like:

    Code:
    9000||1520      1212||1515      5451||1511      1111||1510      1000||1509
    5600||1508      9000||1507      8000||1506      3600||1505      5500||1504
    5900||1503      5900||1502      9000||1501      3600||1500      2500||1499
    in which case you'd want:

    Code:
    3 5451||1511
    3 8000||1506
    1 5900||1503 2 5900||1502
    Last edited by atreyu; 10-08-2013 at 12:30 AM. Reason: that last "1" should be a "2", i think

  7. #7
    Just Joined!
    Join Date
    Apr 2012
    Posts
    56
    Exactly!!!

  8. #8
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    okay, i just did a split on the double-pipe, but i did have to escape the pipe char, so watch out for that:

    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    my $file = shift || die "Give me the file\n";
    open(FH,'<',$file) or die "can't read '$file': $!\n";
    while(<FH>){
      chomp;
      my @nums = split(' ',$_);
    
      my $big;
      for(my $n = 0;$n<=$#nums;$n++){
        next if($n+1 == $.);
        my($val,$foo) = split('\|\|',$nums[$n]);
    
        if(defined($big)){
          $big = $val if($val > $big);
        }else{
          $big = $val;
        }
      }
      for(my $n = 0;$n<=$#nums;$n++){
        next if($n+1 == $.);
        my($val,$foo) = split('\|\|',$nums[$n]);
        if($val == $big){
          print $n+1,' ',$val,'||',$foo,' ';
        }
      }
      print "\n";
    }
    close(FH);
    corfuitl likes this.

  9. #9
    Just Joined!
    Join Date
    Apr 2012
    Posts
    56
    Thanks a lot for your help! It works perfect!!!!

  10. #10
    Just Joined!
    Join Date
    Apr 2012
    Posts
    56
    hi again,

    The last script works perfect but since it is time consuming (I have a file 1000000x1000000), I would like to embed it in my original perl script.
    Basically, my script is like:

    Code:
    for my $i (0 .. $#array) {
    
    ....
    
    my $var1 = ......
    my $var2 = ......
    
    print "$var1 $var2\t"
    
    }
    I tried to modify it adding some of your code but I cannot. Here is what I did:

    Code:
    for my $i (0 .. $#array) {
    
    ....
    
    my $var1 = ......
    my $var2 = ......
    my $big;
      
        next if($n+1 == $.);
        my($var1,$var2) = split(' ',$array[$i]);
    
     if(defined($big)){
          $big = $var1 if($var1> $big);
        }else{
          $big = $var1;
        }
    
        if($var1== $big){
          print $n+1,' ',$var1,' ',$var2,' ';
    
    }
    Is it also possible to sort the numbers first according the var1 and then the var2 (eg 1550 123 1550 129 1550130)?
    Thank you in advance for your help

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •