Visit Our UNIX and Linux User Community


Sort on basis of particular field


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Sort on basis of particular field
# 1  
Old 07-19-2010
Sort on basis of particular field

Code:
05.50.25:AIRE.S:RESTRICTED:S2:
05.50.25:ANDR.VI:RESTRICTED:S2:
05.50.25:BASF.MI:RESTRICTED:N:
05.50.25:BMWG.DE:RESTRICTED:N:
05.50.25:BORE.ST:RESTRICTED:N:

I can sort of the basis of second field using " sort -t: -k2,2 "
but what i want to sort the data is on the basis its extension e.g.: .S .VI .MI .DE .ST

is there any one liner for this. awk etc will do

Thanks a lot in advance

Last edited by radoulov; 07-19-2010 at 04:26 AM.. Reason: Please use code tags!
# 2  
Old 07-19-2010
may be this is what you require ?

Code:
 
sort -t: -k2,2 t | sort -t . -k 4,4
05.50.25:BMWG.DE:RESTRICTED:N:
05.50.25:BASF.MI:RESTRICTED:N:
05.50.25:AIRE.S:RESTRICTED:S2:
05.50.25:BORE.ST:RESTRICTED:N:
05.50.25:ANDR.VI:RESTRICTED:S2:

# 3  
Old 07-19-2010
Quote:
Originally Posted by manishma71
[...]
I can sort of the basis of second field using " sort -t: -k2,2 "
but what i want to sort the data is on the basis its extension e.g.: .S .VI .MI .DE .ST
And how the desired output should look like?
# 4  
Old 07-19-2010
Code:
05.50.25:BMWG.DE:RESTRICTED:N:
05.50.25:BASF.MI:RESTRICTED:N:
05.50.25:AIRE.S:RESTRICTED:S2:
05.50.25:BORE.ST:RESTRICTED:N:
05.50.25:ANDR.VI:RESTRICTED:S2:

this is how it should look like

Last edited by radoulov; 07-19-2010 at 04:32 AM.. Reason: Code tags, please!
# 5  
Old 07-19-2010
Code:
sort -t. -k4 infile

# 6  
Old 07-19-2010
I want a command which will sort on the basis of value between . and :

The solution provided might have issues if there are more dots in the input line after the : which makes this solution prone to error
# 7  
Old 07-19-2010
Could you post an example where the sort command will fail?


Anyway, it's trivial with Perl (not in awk):
Code:
perl -e'
  print map $_->[1], 
        sort { 
          $a->[0] cmp $b->[0] 
          } 
        map {
          [ (split/[:.]/)[4], $_ ]
          } <>
          ' infile

The code above will sort the data based on the 5th field.

Previous Thread | Next Thread
Test Your Knowledge in Computers #535
Difficulty: Medium
In C, the Boolean type false is associated with zero while any non-zero value is associated with true.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Sort array elements from same field

Hi, input: line1|error_type_a@15 line1|error_type_c@10 line1|error_type_b@5 line2|error_type_f@3 line2|error_type_a@1 I would need to place all the second fields with common first field on the same line, BUT with sorted error position number: line1|error_type_b@5; error_type_c@10;... (5 Replies)
Discussion started by: beca123456
5 Replies

2. Shell Programming and Scripting

Sort field and uniq

I have a flatfile A.txt 2012/12/04 14:06:07 |trees|Boards 2, 3|denver|mekong|mekong12 2012/12/04 17:07:22 |trees|Boards 2, 3|denver|mekong|mekong12 2012/12/04 17:13:27 |trees|Boards 2, 3|denver|mekong|mekong12 2012/12/04 14:07:39 |rain|Boards 1|tampa|merced|merced11 How do i sort and get... (3 Replies)
Discussion started by: sabercats
3 Replies

3. Shell Programming and Scripting

Sort help on non numeric field

Hi, I am unable to sort data on the first field $cat t Jim,212121,Seattle Bill,404404,Seattle Steve,246810,Nevada Scott,212277,LosAngeles Jim,212121,Ohio sort -t"," -k1,2 t Bill,404404,Seattle Jim,212121,Ohio Jim,212121,Seattle Scott,212277,LosAngeles Steve,246810,Nevada (7 Replies)
Discussion started by: Shivdatta
7 Replies

4. Shell Programming and Scripting

Comparing two huge files on field basis.

Hi all, I have two large files and i want a field by field comparison for each record in it. All fields are tab seperated. file1: Email SELVAKUMAR RAMACHANDRAN Email SHILPA SAHU Web NIYATI SONI Web NIYATI SONI Email VIINII DOSHI Web RAJNISH KUMAR Web ... (4 Replies)
Discussion started by: Suman Singh
4 Replies

5. Shell Programming and Scripting

Replace field when only "-" occurs on a random basis

I have a file in which "-"(by itself and nothing else) occurs in different fields in each record(sometimes in the 3 field, sometime in the 20th field, some time in the 100th field....etc). Also there are field of the format "abc-def". How do I replace fields that have just "-" with number zero. sed... (4 Replies)
Discussion started by: akshaykr2
4 Replies

6. Shell Programming and Scripting

Sort two columns in a field, one of them being a date

Hi, I have a set of columns in a csv file, my first row being an integer and 2nd being a date. I want to first sort it using the first column and then by the second. for e.g. i have , 1234,09/05/2009,hi 5678,01/01/2008,hi 1234,11/03/2006,hello 5678,28/07/2010,hello i tried this... (5 Replies)
Discussion started by: sweta_doshi
5 Replies

7. Shell Programming and Scripting

Sort alpha on 1st field, numerical on 2nd field (sci notation)

I want to sort alphabetically on the first field and sort in descending numerical order on the 2nd field. With a normal "sort -r -n" it does this: abc ||| 5e-05 ||| bla abc ||| 3 ||| ble def ||| 1 ||| abc def ||| 0.2 ||| def As you can see it ignores the fact that 5e-05 is actually 0.00005... (1 Reply)
Discussion started by: FrancoisCN
1 Replies

8. Shell Programming and Scripting

How to Sort files on date field

:cool: Hi all, I have a pecular issue in sorting these files in Solaris environment. All the below files are modified on November 4th, but I want to sort these files as per date column (eg: 01May07_1623 = ddmmmyy_hhmm) Nov 4 18:27 SONYELEC00.GI22973.01May07_1623.gpg Nov 4 18:27... (4 Replies)
Discussion started by: shivaastrogun
4 Replies

9. Shell Programming and Scripting

sort columns by field

hello, i have a table contain many columns delimited by blank. i want to sort this table by the 2 columns and 3 one and i want to keep the first line inchanged? how can i do using the sort command? thanks table like : field1 field2 field3 field4 x y z b t h r n .. (4 Replies)
Discussion started by: kamel.seg
4 Replies

10. UNIX for Dummies Questions & Answers

Sort - only one field appears in output?

I'm running against a file with 1008 records like this, all beginning '4760 Slave': 4760 Slave,7,3607 ,GL ,200605,11320024 , ,GBP ,X00033 ,AI80190 ... (1 Reply)
Discussion started by: miwinter
1 Replies

Featured Tech Videos