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 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.
About Hemanth HM
Hemanth HM is a Sr. Machine Learning Manager at PayPal, Google Developer Expert, TC39 delegate, FOSS advocate, and community leader with a passion for programming, AI, and open-source contributions.