Sorting blocks of lines

sorttext processing

I have a file that contains 4n lines. Here is an excerpt from it containing 8 lines

6115 8.88443
6116 6.61875
6118 16.5949
6117 19.4129
6116 6.619 
6117 16.5979 
6118 19.4111
6115 8.88433  

What I want to do is sort a block, where each block consists of 4 lines based on the first column. The output for the excerpt should look as shown below.

6115 8.88443
6116 6.61875
6117 19.4129
6118 16.5949
6115 8.88433 
6116 6.619 
6117 16.5979 
6118 19.4111 

Best Answer

One options is to use to add an initial serial number prefix every N lines (N=4 in your case). Then feed the prefix as the primary sorting column into sort.

Example with N=4:

awk '{print int((NR-1)/4), $0}' file.txt | sort -n -k1,1 -k2,2 | cut -f2- -d' '
Related Question