Sort text blocks

16 Jul 2010

by hemanth

I recently came across a simple task, wherein i had to sort a massive text file which was an output of a heavy data crunching code.
In pursuit of the short and easy way to do a block sort on each text blocks separated by a new line character, the first think that appeared to me for solving this was indeed BASH.

The output of the program was something like:

# Block-1
....
....

# Block-2
....
....

# Block-N
....
....

Had to sort each block, the advantage was that it was separated by a new line. The below is what i did in BASH and awk to solve the problem.

BASH version:

buffer=; 
{ while read -r; 
do if [[ $REPLY ]]; then
      buffer+="$REPLY"$'\n'; 
   else 
      sort <<<"$buffer"; 
      buffer=; 
   fi; 
done; sort <<<"$buffer"; } < unsorted > sorted

The code is a simple play and sheer power of bash redirections.
The read -r does not allow backslashes to escape any characters.

Share this