Sort file numerically and preserve blank lines between entries in Bash

619 views Asked by At

I am currently struggling at sorting data. I searched online and never saw any topics mentioning my issue...

I have files with unordered data like:

1
blank line
3
blank line
2

Which have a blank line in between values. When I use my script, it effectively sorts data but blank lines are on top and values on bottom, like :

blank line
blank line
1
2
3

I would like to have an output like:

1
blank line
2
blank line
3

which preserves the structure of the input.

The command I use is: sort -nk1 filename > newfile

How can I preserve the blank lines in the right places?

1

There are 1 answers

0
Walter A On BEST ANSWER

Remove the empty lines, sort and add the empty lines again:

grep . filename | sort -nk1 | sed 's/$/\n/' > newfile

You can combine grep and sed

sort -nk1 filename | sed -n '/./ s/$/\n/p' > newfile

When you don't have an empty line after each data-line, you need to add some marker temporarily

tr '\n' '\r' < filename | 
  sed -r 's/([^\r]+)\r\r/\1\a\r/g;s/\r/\n/g' | 
  sort -nk1 | sed 's/\a/\n/g' > newfile