Sorting by Multiple Columns


 
Thread Tools Search this Thread
# 1  
Sorting by Multiple Columns

Hi

I have a text file that has four columns (Logonid,First Name,Last Name,Status)

Code:
Logonid    First Name   Last Name  Status
abc2        Fred           Mercury     Inactive
abc1        John           Deacon      Active
abc3        Roger         Taylor        Active
abc4        Brian          May           Inactive

In the above scenario I would want the following order (ie sorted alphabetically on 1st and 4th columns)...

Code:
Logonid    First Name   Last Name  Status
abc1        John           Deacon      Active
 abc3        Roger         Taylor        Active
abc2        Fred           Mercury     Inactive
 abc4        Brian          May           Inactive

I am trying to use the following (which is not working!)..

Code:
cat inputfile.csv | sort -t',' -k1 -k4  >> outputfile-$var_date.csv

Any help always appreciated.

Last edited by radoulov; 05-18-2012 at 06:40 AM.. Reason: Additional code tags.
# 2  
You sort command should work fine outside the fact it tells to use a colon as field separator while the file you are showing isn't using them but spaces.
# 3  
I think you need to re-read the sort man page; you've got the idea but are confused. Here's how I'd do it:
Code:
head -1 $input > $output
tail -n +2 $input | sort -k 4,4 -k 1,1 >>$output

If "tail -n +2" returns an error, use sed:
Code:
sed -n '2,$p' $input | ...

The order you specify the -k sort keys options dictates the precedence of the sorting. In this case you wanted the 4th field sorted and THEN the 1st field.

Last edited by otheus; 05-18-2012 at 07:20 AM.. Reason: fixed sort order.
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Science: Mathematics
Difficulty: Medium
The proof for the Chinese Remainder Theorem used in Number Theory was NOT developed by its first publisher, Sun Tzu.
True or False?

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. Shell Programming and Scripting

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. printf "%13s %2s UDP %15s:%s Program %4s HD: %23s HD: %23s %10s %s %s %3s days %3s hours\n" $encoder $i "${ipaddr}" ${portno} ${progno} ${inres} ${outres} ${inrate}... (4 Replies)
Discussion started by: ramman
4 Replies

4. Shell Programming and Scripting

sorting and adding columns

i have a file with two columns, and i want to uniquely sort the values in fist column and add the corresponding values in the second columns eg file a contents tom 200 john 300 sow 500 tom 800 james 50 sow 300 output shpould be in file b as tom 1000 john 300 sow 800 james 50 (0 Replies)
Discussion started by: dealerso
0 Replies

5. 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

6. 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

7. 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

8. UNIX for Dummies Questions & Answers

Vertical sorting of columns

Please help to sort columns in this file: a b d f c e 1 4 10 16 7 13 2 5 11 17 8 14 3 6 12 18 9 15 I need to sort COLUMNS (so sort command doesn't work) like this: a b c d e f 1 4 7 10 13 16 2 5 8 11 14 17 3 6 9 12 15 18 I know sed can do it but don't know how... :( (7 Replies)
Discussion started by: coppuca
7 Replies

9. 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

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

Featured Tech Videos