---
title: "Sort Text Blocks"
description: "I recently came across a simple task, wherein i had to sort a massive text file which was an output of a heavy data in JS."
url: "https://h3manth.com/scribe/sort-text-blocks/"
canonical_url: "https://h3manth.com/scribe/sort-text-blocks/"
markdown_url: "https://h3manth.com/scribe/blog/sort-text-blocks.md"
publishDate: "2010-07-16T00:00:00.000Z"
author: "Hemanth HM"
category: "linux"
tags: ["bash"]
word_count: 164
reading_time_minutes: 1
x_markdown_tokens: 330
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# Sort Text Blocks

> I recently came across a simple task, wherein i had to sort a massive text file which was an output of a heavy data in JS.

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.
