Sorting in groups

Hi,

I am looking at a slightly different sorting problem and I am not sure how to do it in bash.

I have the following input:

0                                                                                                                                                                                                                                                                              
0.181017                                                                                                                                                                                                                                                                       
0.181017                                                                                                                                                                                                                                                                       
0.0409129                                                                                                                                                                                                                                                                      
0.214896                                                                                                                                                                                                                                                                       
0.214896                                                                                                                                                                                                                                                                       
0.781925                                                                                                                                                                                                                                                                       
1.05696                                                                                                                                                                                                                                                                        
1.05696                                                                                                                                                                                                                                                                        
0.791026                                                                                                                                                                                                                                                                       
1.10494                                                                                                                                                                             
1.10494                                                                                                                                                                             
2.04486                                                                                                                                                                             
9.37513                                                                                                                                                                             
9.37513
1.29588
15.6751
15.6751
15.873
19.2972
19.2972

I want to sort this, but in groups of three elements with the first one being the sorted value. For example line 16 is lesser than line 13 in the input and it must be re-sorted as the following. The two lines following the sorted value must be moved along with it.

0                                                                                                                                                                                                                                                                              
0.181017                                                                                                                                                                                                                                                                       
0.181017                                                                                                                                                                                                                                                                       
0.0409129                                                                                                                                                                                                                                                                      
0.214896                                                                                                                                                                                                                                                                       
0.214896                                                                                                                                                                                                                                                                       
0.781925                                                                                                                                                                                                                                                                       
1.05696                                                                                                                                                                                                                                                                        
1.05696                                                                                                                                                                                                                                                                        
0.791026                                                                                                                                                                                                                                                                       
1.10494                                                                                                                                                                             
1.10494                                                                                                                                                                             
1.29588
15.6751
15.6751
2.04486                                                                                                                                                                             
9.37513                                                                                                                                                                             
9.37513
15.873
19.2972
19.2972

Any pointers to do this is much appreciated.

Thanks!
Jamie

Any attempts from your side?

Hi,

I can print every third line and sort that very easily, using

awk 'NR%3==1' | sort -n 

But I am not sure how to track the other two lines and add them to this sorted list..I am trying to figure a way out.

Try

paste -s -d"\t\t\n" file2 | LC_ALL=C sort -n | tr '\t' '\n'
0
0.181017
0.181017
0.0409129
0.214896
0.214896
0.781925
1.05696
1.05696
0.791026
1.10494
1.10494
1.29588
15.6751
15.6751
2.04486
9.37513
9.37513
15.873
19.2972
19.2972

Hopefully the delimiters for paste and parameters for tr are accepted in this form. The LC_ALL=C is needed only if your locale setting has an undesirable influence on the sort command.

1 Like

Assuming you don't care about preserving the hundreds of spaces at the ends of some of your input lines (since the spacing in your sample output lines does not match the spacing in your sample input lines), you could also try:

#!/bin/bash
while read a
do	read b
	read c
	printf '%s %s %s\n' "$a" "$b" "$c"
done < file | sort -n | while read a b c
do	printf '%s\n%s\n%s\n' "$a" "$b" "$c"
done

which strips off all of the trailing spaces.

1 Like

Hi,

Thank you for the response. I am a bit confused on the intended usage. Does file2 mean another file which stores the unsorted entries? If I run it on the base file, the entries are not sorted at all.

Thanks.

You didn't give us the name of your input file. So the filename used in responses to your request for help ( file , file2 , your_input_file , or RanDomStrIng ) shouldn't matter. We assume that you know enough about shell scripting to be able to change the input file used in the sample solutions provided to be the name of your input file.

The suggestions RudiC and I provided do not modify your input file. The output sorted as you requested appears on standard output. You can pipe that output to another script or redirect that output to any other file that you want. Just do not redirect the output to your input file (or you will end up with an empty input file).

Hi,

Sorry about that. I tried RudiC's response without the LC_ALL part, which did not work, leading me to think that the input is different. However, I must say that both the solutions work.

Thanks!!

Sorry for the confusion and the "typo". I have a set of temp files to play with, and sometimes it escapes me to edit the file name in the post. As Don Cragun said, replace that temp file name with your input file's.

1 Like