sort linux command alphabetically leave empty cells to the end

Viewed 22

Can someone help me how to sort column alphabetically leaving blank cells to the end. I have gone through some examples already posted here which is of numeric sorting. I have big csv file which has many columns. I wanted to sort based on user which is my second column. My first column may have some blank cells which i want to come at the end. I tried below awk command using sort but didn't work. Can some please help me.

awk '$2 ~ /[0-9]$/' d.txt | sort -k2g && awk '$2 !~ /[0-9]$/' d.txt

My csv file looks like this

enter image description here

1 Answers

one idea is to

  1. set each of the empty cells to 3 consecutive characters of

    • U+10FFFF ( \364\217\277\277 | F4 8F BF BF ), the UTF-8 character with the highest ordinal value,
  2. sort it,

  3. then delete those characters out later

such a sequence is extremely improbable to exist in typical input, while also being fully valid UTF-8 code points, thus avoiding the triggering of any warning or error messages from sorting utilities

Related