Sort on basis of particular field


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

---------- Post updated at 02:09 PM ---------- Previous update was at 02:07 PM ----------

can above formula perform sorting for multiple columns also.
in case yes, can you please let me know the perl script for that also.
the field seperators will be . and : only.
# 9  
Old 07-19-2010
Quote:
Originally Posted by manishma71
[...]
can above formula perform sorting for multiple columns also.
Yes, you'll need to modify the script. To sort by 5 and then by 4 field, for instance, you'll need something like this:


Code:
perl -e'
  print map $_->[0],
        sort { 
          $a->[2] cmp $b->[2] ||
          $a->[1] cmp $b->[1]
          } 
        map {
          [ $_, (split /[.:]/)[3..4] ]
          } <>
          ' infile

Quote:
in case yes, can you please let me know the perl script for that also.
the field seperators will be . and : only.
Yes, just change the first argument passed to split:

Code:
split /./

Code:
split /:/

# 10  
Old 07-19-2010
thanks a lot radoulov... this is really of great help Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

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
Login or Register to Ask a Question