Find the answer to your Linux question:
Results 1 to 6 of 6
Hi all, Does anyone can help me the following question? In the following input file, each number in "start" is paired with numbers in column "end". No StartEnd A 22,222,33,22,1233,3232,44 ...
  1. #1
    Just Joined!
    Join Date
    Apr 2009
    Posts
    12

    sorting

    Hi all,

    Does anyone can help me the following question?
    In the following input file, each number in "start" is paired with numbers in column "end".


    No StartEnd
    A 22,222,33,22,1233,3232,44 555,333,222,55,1235,3235,66
    B 33,333,22,44 66,340,44,55
    C 66,55,555 75,58,560

    For each row, I would like to sort those pair of number by "start" then by "end".
    For example, the first row has 7 pairs of numbers, they are in the following order,
    22,555
    222,333
    33,222
    22,55
    1233,1235
    3232,3235
    44,66
    After sorting, they should be in the following order

    22,55
    22,555
    33,222
    44,66
    222,333
    1233,1235
    3232,3235

    In the end the the output after being sorted is follow,

    No Start End
    A 22,22,33,44,222,1233,3232 55,555,222,66,333,1235,3235
    B 22,33,44,333 44,66,55,340
    C 55,66,555 58,75,560



    Your advice is much appreciated.

    Thanks,
    Phoebe

  2. #2
    Just Joined!
    Join Date
    Aug 2010
    Posts
    8
    From what you wrote it seems like you just need to replace a space character with a couple of end-of-line characters and perhaps some tabs. Did I miss the point?

    I did not notice where you were sorting. Did you try using a sed command?

    Otto.

  3. #3
    Just Joined!
    Join Date
    Apr 2009
    Posts
    12
    sorry, I modified my question, hope it is clearer now.

  4. #4
    Linux User twoHats's Avatar
    Join Date
    Jan 2005
    Location
    NH, USA
    Posts
    276
    Your question needs to be clearer - not many will reverse engineer your question to help you. You may need to use a code tag so that formatting of columns etc won't get lost?
    - Clouds don't crash - Bertrand Meyer

    registered Linux user 393557

    finally - hw to brag about - but next year it will look pitifully quaint:
    Athlon64 X2 3800 - 1G PC3200 - 250G SATA - ati radeon x300
    circa 2006

  5. #5
    Just Joined!
    Join Date
    Jul 2010
    Posts
    53
    the awk script below will separate your file and uses the sort utility for numeric sorting. numeric sort inside of awk is bit of a pain.

    simplest way to run it is if you save the awk script as pairs.awk and your input file is pairs.txt then:
    Code:
    awk -f pairs.awk pairs.txt

    Code:
    { pairs[$1]=$2 " " $3; }
    END {
      n = asorti(pairs,ids);
      for (i=1; i<=n; i++) {
        id=ids[i];
        split(pairs[id],set);
        printf("%s [%s] [%s]\n",id,set[1],set[2]);
        nidx=split(set[1],idx,",");
        split(set[1],seta,",");
        split(set[2],setb,",");
        cmd="sort -n ";
        for(j=1; j<=nidx; j++) {
          printf("%s %s\n",seta[j],setb[j]) | cmd;
        }
        close(cmd);
      }
    }
    Last edited by chaosless; 08-17-2010 at 12:54 AM. Reason: added the sort bits

  6. #6
    Just Joined!
    Join Date
    Jul 2010
    Posts
    53
    this will let you read back in the sorted results if you needed to do further processing:

    Code:
    { pairs[$1]=$2 " " $3; }
    END {
      n = asorti(pairs,ids);
      for (i=1; i<=n; i++) {
        id=ids[i];
        split(pairs[id],set);
        printf("%s [%s] [%s]\n",id,set[1],set[2]);
        nidx=split(set[1],idx,",");
        split(set[1],seta,",");
        split(set[2],setb,",");
        cmd="sort -n ";
        for(j=1; j<=nidx; j++) {
          printf("%s %s\n",seta[j],setb[j]) |& cmd;
        }
        close(cmd, "to");
        while ((cmd |& getline line) > 0) {
          split(line,pair);
          printf("%s,%s\n", pair[1], pair[2]);
        }
        close(cmd)
      }
    }

Posting Permissions

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