Find the answer to your Linux question:
Results 1 to 7 of 7
Hi there, Can someone let me know how to sort the 2 dimensional array below by column 1 then by column 2? 22 55 2222 2230 33 66 44 58 ...
  1. #1
    Just Joined!
    Join Date
    Apr 2009
    Posts
    12

    sorting 2 dimension array

    Hi there,

    Can someone let me know how to sort the 2 dimensional array below by column 1 then by column 2?

    22 55
    2222 2230
    33 66
    44 58
    222 240
    11 25
    22 60
    33 45

    output:
    11 25
    22 55
    22 60
    33 45
    33 66
    44 58
    222 240
    2222 2230

    Please note that this is a small part in my awk script. the input data is not in file, but it was manipulated in my script.

    Could you suggest a code to solve this?

    Thanks,
    Phoebe

  2. #2
    Just Joined!
    Join Date
    Apr 2009
    Posts
    12
    nobody happen to this kind of problem? please advice.

  3. #3
    Linux Newbie
    Join Date
    Sep 2004
    Location
    UK
    Posts
    160
    man sort


    Code:
    cat datafile | sort -t " " -n -k 1,2 
    
    or 
    
    sort -t " " -n -k 1,2 datafile
    In a world without walls and fences, who needs Windows and Gates?

  4. #4
    Just Joined!
    Join Date
    Apr 2009
    Posts
    12
    unfortunately, as I mentioned in the question, my input data is not a file. The input data was manipulated in the script.

    I wish to sort the array in the awk script.

    Thanks,

  5. #5
    Linux Enthusiast Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    718
    then pipe the output of awk to the sort.

  6. #6
    Just Joined!
    Join Date
    Apr 2009
    Posts
    12
    the two dimension array was generated through a looping for 2000 times. In the end of loop, I would have a table created with first column consists of the array[,1] for each, and second column consist of array[,2] for each.

    1 11,22,22,33,33,44,222,2222 25,55,60,45,66,58,240,2230

    I can't pipe the output in each loop to the sort, right?
    I'm new with awk scripting. Please advice.

  7. #7
    Linux Enthusiast Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    718
    but you can pipe the whole output, right?

    if you have scriptA, you can pipe the output of it to scriptB by doing:

    Code:
    scriptA | scriptB
    or doing (within scriptA):

    Code:
    output="";
    for i in $(seq 5); do output="${output} something"; done;
    echo $output | sort

Posting Permissions

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