gawk - multiple sorting within a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting gawk - multiple sorting within a file
# 1  
Old 06-20-2008
gawk - multiple sorting within a file

Hi,

I am new shell programming and trying work on the follwoing file:

1 rs1
1 rs12
1 rs3
1 rs8
2 rs2
2 rs15
2 rs3
3 rs5
3 rs9
3 rs4

I would like to sort my 2nd column within 1, 2, and 3 separately. The output will be:
1 rs1
1 rs3
1 rs8
1 rs12
2 rs2
2 rs3
2 rs15
3 rs4
3 rs5
3 rs9

Could please help me with this?

Thanks in advance.

Best regards,
Ezhil
# 2  
Old 06-20-2008
Code:
gawk '{ 
  $2 = sprintf("%010s", $2)
  _[$1] = _[$1] ? _[$1] FS $2 : $2 
  }
END {
  for (k in _) {
    split(_[k], t)
    n = asort(t)
    for(i=1; i<=n; i++) {
      sub(/^0+/,"",t[i])
      print k, t[i]
      }
    }
  }
' file

# 3  
Old 06-20-2008
Code:
sort -k1,1n -k2.3n file

# 4  
Old 06-20-2008
Quote:
Originally Posted by shamrock
Code:
sort -k1,1n -k2.3n file

Smilie of course.

I was looking at the title, but if it's all about sorting the OP should use the right tool (the sort command).

It happens to me often to overuse Awk because of its simplicity.

Last edited by radoulov; 06-20-2008 at 11:49 AM..
# 5  
Old 06-20-2008
Quote:
Originally Posted by radoulov
Smilie of course.

I was looking at the title, but if it's all about sorting the OP should use the right tool (the sort command).

It happens to me often to overuse Awk because of its simplicity.
Not a problem...It happens to the best of us!!! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

3. Shell Programming and Scripting

multiple sorting with different order

Hi Guys, I have data like this HOS05 23/12/2008 10AM HOS06 15/12/2008 2PM HOS62 29/12/2008 10AM HOS64 23/12/2008 2PM HOS70 26/12/2008 10AM ZFT01 06/12/2008 10AM HOS73 11/12/2008 2PM MHOS0 05/12/2008 10AM MHOS0 20/12/2008 2PM MHOS0 27/12/2010 2PM MHOS0 11/12/2008 10AM MHOS0 30/12/2009... (1 Reply)
Discussion started by: ckarunprakash
1 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. UNIX for Dummies Questions & Answers

JOINING MULTIPLE LINES IN A TEXT FILE USING GAWK

sir... am having a data file of customer master., containing some important fields as a set one line after another., what i want is to have one set of these fields(rows) one after another in line.........then the second set... and so on... till the last set completed. I WANT THE DATA... (0 Replies)
Discussion started by: KANNI786
0 Replies

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

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

10. Shell Programming and Scripting

Sorting a flat file based on multiple colums(using character position)

Hi, I have an urgent task here. I am required to sort a flat file based on multiple columns which are based on the character position in that line. I am restricted to use the character position instead of the space and sort +1 +2 etc to do the sorting. I understand that there is a previous... (8 Replies)
Discussion started by: cucubird
8 Replies
Login or Register to Ask a Question