2

i have problem with sorting in variable.

I work with Bash and with NSH console.

I load to variable, biggest folders in my OS:

vdirList=$(nexec -e "find /etc -type d -size +1k -print0 | xargs -0 du -h --max-depth 3 --time --time-style +'%F %T %z' 2>/dev/null| sort -h -k1 2>/dev/null| tail -7 | sed -n '$ ! p' | uniq -c | tac | sed 's/^/BAORES: /'") vdirList+="\n" vdirList+=$(nexec -e "find /usr -type d -size +1k -print0 | xargs -0 du -h --max-depth 3 --time --time-style +'%F %T %z' 2>/dev/null| sort -h -k1 2>/dev/null| tail -7 | sed -n '$ ! p' | uniq -c | tac | sed 's/^/BAORES: /'") vdirList+="\n" 

This is output:

BAORES: 3 22M 2017-05-05 14:53:20 +0300 /etc/selinux/targeted BAORES: 2 22M 2017-05-05 14:53:20 +0300 /etc/selinux BAORES: 1 13M 2017-05-05 14:53:20 +0300 /etc/selinux/targeted/modules BAORES: 2 1.9G 2018-12-20 05:49:04 +0200 /usr/lib BAORES: 3 1.3G 2018-12-20 05:48:55 +0200 /usr/lib/x86_64-linux-gnu BAORES: 1 445M 2018-12-20 05:49:04 +0200 /usr/lib/i386-linux-gnu 

I want sort this files by 3 column i use this command:

dirList=$(nexec -e "echo \"$vdirList\" | sort -hrk3 | head -n 10") 

But my output is:

BAORES: 3 22M 2017-05-05 14:53:20 +0300 /etc/selinux/targeted BAORES: 3 1.3G 2018-12-20 05:48:55 +0200 /usr/lib/x86_64-linux-gnu BAORES: 2 22M 2017-05-05 14:53:20 +0300 /etc/selinux BAORES: 1 445M 2018-12-20 05:49:04 +0200 /usr/lib/i386-linux-gnu BAORES: 1 13M 2017-05-05 14:53:20 +0300 /etc/selinux/targeted/modules BAORES: 2 1.9G 2018-12-20 05:49:04 +0200 /usr/lib 

Can someone help with this? Thanks.

1 Answer 1

2

I can confirm the weird behaviour with LC_ALL=en_US.UTF-8. Under the C locale, it seems to work correctly:

$ input='BAORES: 3... ... .../i386-linux-gnu' $ LC_ALL=C sort -k3,3hr <<< "$input" BAORES: 2 1.9G 2018-12-20 05:49:04 +0200 /usr/lib BAORES: 3 1.3G 2018-12-20 05:48:55 +0200 /usr/lib/x86_64-linux-gnu BAORES: 1 445M 2018-12-20 05:49:04 +0200 /usr/lib/i386-linux-gnu BAORES: 2 22M 2017-05-05 14:53:20 +0300 /etc/selinux BAORES: 3 22M 2017-05-05 14:53:20 +0300 /etc/selinux/targeted BAORES: 1 13M 2017-05-05 14:53:20 +0300 /etc/selinux/targeted/modules 

What also worked was to specify the separator explicitly:

sort -t' ' -k3,3hr 

but I find the locale setting more reliable.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, it work, if it have this output in file, but i need work with this in variable.
LC_ALL=C sort -k3,3hr <<< "$variable"
Still same output :-/
@Destrem: Updated to show exactly the steps that work for me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.