Help in sorting multiple columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help in sorting multiple columns
# 1  
Old 07-10-2012
Help in sorting multiple columns

Hello all,

I am using printf to print the sorted o/p in my script.I am trying to sort in following way but doesn't work.


Code:
printf "%13s %2s  UDP %15s:%s Program %4s HD: %23s HD: %23s %10s %s %s %3s days %3s hours\n" $encoder $i "${ipaddr[$i]}" ${portno[$i]} ${progno[$i]} ${inres[$i]} ${outres[$i]} ${inrate[i]} ${enable[i]} ${run[$i]} ${edate1[$i]} ${etime1[$i]} |  sort -k 13,13n -k 14,14n -k 15,15n -k 16,16n -k 17,17n

I am trying to sort 13,14,15,16 and 17th column of the o/p sorted.

Is this the right method or something else can be done?

Thank you.

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 07-10-2012 at 03:43 PM.. Reason: code tags, see PM!
# 2  
Old 07-10-2012
You kidding us? Why in the world don't you just try it out?
That's what I do not understand.
# 3  
Old 07-10-2012
After you try it out for yourself, as zaxxon suggests, if it doesn't work as expected, at the very least provide the input data, the output your code generated, and the output you expected.

Regards,
Alister
# 4  
Old 07-10-2012
hey guys i did try it out and it didn't work that's why i posted,let me make my question more clear.

I execute the following code

Code:
for i in {1..12};do  printf "%s %s\n" "${edate1[$i]}" "${etime1[$i]}"

(I retrieve the values of edate1 and etime1 from my database and store it in an array which works fine.)

I receive the o/p as:

Code:
97 16
97 16
97 12
107 16
97 16
97 16
97 16
97 16
97 16
97 16
97 16
100 15

I want to sort both these columns using the sort command.Please help me out.

Thanks.

---------- Post updated at 04:50 PM ---------- Previous update was at 04:43 PM ----------

Expected O/P:

Code:
107 16
100 15
97 16
97 16
97 16
97 16
97 16
97 16
97 16
97 16
97 16
97 12


Last edited by zaxxon; 07-11-2012 at 03:24 AM.. Reason: code tags
# 5  
Old 07-11-2012
Code:
$sort -rn -k1.1 -k2.2 infile
107 16
100 15
97 16
97 16
97 16
97 16
97 16
97 16
97 16
97 16
97 16
97 12

This User Gave Thanks to zaxxon For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sorting by columns

Hi, I have a tab delimited columnar file where I want to remove lines wherever two particular columns match. so for this file, I want to toss the lines where columns 1 and 2 match: a a 1 3 a b 2 4 b b 3 5 because there are matches column 1 and 2 in lines 1 and 3, I would like a script to... (2 Replies)
Discussion started by: mikey11415
2 Replies

2. Shell Programming and Scripting

CSV Sorting on only particular columns

Hello! So ive been presented with this comma-delimited file: I need a print to look as below " lastname, phone_number, zip for every person with a last name starting with the letter H, I only with a 650-area code phone number. output should be sorted by reverse ZIP code " I only have... (5 Replies)
Discussion started by: strangemachine
5 Replies

3. UNIX for Dummies Questions & Answers

Sorting by Multiple Columns

Hi I have a text file that has four columns (Logonid,First Name,Last Name,Status) Logonid First Name Last Name Status abc2 Fred Mercury Inactive abc1 John Deacon Active abc3 Roger Taylor Active abc4 Brian ... (2 Replies)
Discussion started by: theref
2 Replies

4. Shell Programming and Scripting

Sorting problem: Multiple delimiters, multiple keys

Hello If you wanted to sort a .csv file that was filled with lines like this: <Ticker>,<Date as YYYYMMDD>,<Time as H:M:S>,<Volume>,<Corr> (H : , M, S: ) by date, does anybody know of a better solution than to turn the 3rd and 4th colons of every line into commas, sorting on four keys,... (20 Replies)
Discussion started by: Ryan.
20 Replies

5. UNIX for Dummies Questions & Answers

Sorting by multiple columns

I have a space delimited text file that I would like to sort by multiple columns. First I want to sort by column 1, then by column 2. Both columns are numerical. Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

6. Shell Programming and Scripting

Sorting multiple columns

Hi, We have a requirement of need to sort a file based on fields 1,3 and 4. I tried with sort command however it is not giving expected output, can we achieve any other way? Please let me know ASAP. File a e w a a b a a a a d g a a h h c d a e a a a w Output a b a a a a a w a a d... (4 Replies)
Discussion started by: Nagapandi
4 Replies

7. Shell Programming and Scripting

Sorting based on Multiple columns

Hi, I have a requirement whereby I have to sort a flat file based on Multiple Columns (similar to ORDER BY Clause of Oracle). I am getting 10 columns in the flat file and I want the file to be sorted on 1st, 3rd, 4th, 7th and 9th columns in ascending order. The flat file is pipe seperated. Any... (15 Replies)
Discussion started by: dharmesht
15 Replies

8. UNIX for Advanced & Expert Users

sorting of varchar columns

Hi , I need to sort a file based on multiple columns All the columns are of varchar type can any one give me the command to sort for varchar columns? Thanks (3 Replies)
Discussion started by: laxmi131
3 Replies

9. Shell Programming and Scripting

Sorting based on columns

Hi, I want a list of entries in 3 space delimited columns. I want to sort entries based on the very first column. Rows can't be changed. For example: If I have... Abc Abc Acc Bca Bda Bdd Cab Cab Cbc Dbc Dca Dda Abc Abc Acc the output should be... Abc Abc Acc Abc Abc Acc Bca... (7 Replies)
Discussion started by: MobileUser
7 Replies

10. UNIX for Dummies Questions & Answers

sorting a file with multiple columns

I have a file with several columns: Column1 Column2 Column3. . .Column6 I want to sort the data from Column6. Could I do that through sort even if there are spaces in between fields? Much thanks! outta. (3 Replies)
Discussion started by: outtacontrol
3 Replies
Login or Register to Ask a Question