Sort CLI output by line length

Want to sort file contents by each line’s lenght? No problems:

artemn@artemn-laptop:~$ cat /etc/passwd | awk '{print length, $0}' | sort -n | awk '{$1=""; print $0 }'

bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
root:x:0:0:root:/root:/bin/bash
proxy:x:13:13:proxy:/bin:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
ntp:x:110:120::/home/ntp:/bin/false
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
ftp:x:111:65534::/home/ftp:/bin/false
games:x:5:60:games:/usr/games:/bin/sh
klog:x:102:103::/home/klog:/bin/false
man:x:6:12:man:/var/cache/man:/bin/sh
irc:x:39:39:ircd:/var/run/ircd:/bin/sh
dhcp:x:100:101::/nonexistent:/bin/false
news:x:9:9:news:/var/spool/news:/bin/sh
saned:x:120:131::/home/saned:/bin/false
syslog:x:101:102::/home/syslog:/bin/false
uucp:x:10:10:uucp:/var/spool/uucp:/bin/sh
backup:x:34:34:backup:/var/backups:/bin/sh
www-data:x:33:33:www-data:/var/www:/bin/sh
libuuid:x:112:121::/var/lib/libuuid:/bin/sh
messagebus:x:103:109::/var/run/dbus:/bin/false
nobody:x:65534:65534:nobody:/nonexistent:/bin/sh
sshd:x:109:65534::/var/run/sshd:/usr/sbin/nologin
Debian-exim:x:117:128::/var/spool/exim4:/bin/false
artemn:x:1000:1000:artemn,,,:/home/artemn:/bin/bash
list:x:38:38:Mailing List Manager:/var/list:/bin/sh
mysql:x:119:130:MySQL Server,,,:/var/lib/mysql:/bin/false
gdm:x:108:118:Gnome Display Manager:/var/lib/gdm:/bin/false
hplip:x:104:7:HPLIP system user,,,:/var/run/hplip:/bin/false
pulse:x:113:123:PulseAudio daemon,,,:/var/run/pulse:/bin/false
polkituser:x:114:127:PolicyKit,,,:/var/run/PolicyKit:/bin/false
avahi:x:106:114:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false
asterisk:x:118:129:Asterisk PBX daemon,,,:/var/lib/asterisk:/bin/false
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/bin/sh
haldaemon:x:107:116:Hardware abstraction layer,,,:/home/haldaemon:/bin/false
landscape:x:115:65534:Landscape Client Daemon,,,:/var/lib/landscape:/bin/false
avahi-autoipd:x:105:113:Avahi autoip daemon,,,:/var/lib/avahi-autoipd:/bin/false
chipcard:x:116:119:Chipcard-Tools Daemon Account,,,:/var/run/chipcard:/bin/false

For reverse sort, use the following command:

cat /etc/passwd | awk '{print length, $0}' | sort -rn | awk '{$1=""; print $0 }'

Source: www.opennet.ru

Information improvisation: Download high quality http:///642-889.htm to prepare and with oracle certification dumps certification. Also get free demos of http://www.test-king.com/exams/642-437.htm for review of http://www.android.com/ training and more visit http://www.actualtests.com/exam-642-437.htm Best wishes.

SHARE:
nv-author-image

Stefan Durand

My name is Stefan, I'm the admin of LinuxScrew. I am a full-time Linux/Unix sysadmin, a hobby Python programmer, and a part-time blogger. I post useful guides, tips, and tutorials on common Linux and Programming issues. Feel free to reach out in the comment section.

2 thoughts on “Sort CLI output by line length”

  1. You don’t need the cat invocation. Awk can take the file as the standard input.
    So, the following works just as well:

    awk ‘{print length, $0}’ /etc/passwd | sort -n | awk ‘{$1=””; print $0 }’

    You can put the length of the line into an associative array and sort it all from within awk. Interesting associative array play:

    awk ‘{ arr[length $0] = $0 } END { for (len in arr) \
    printf(“%d\t%s\n”, len, arr[len]) | “sort -n” }’ /etc/passwd | cut -f2-

    In script form it’s a bit clearer:

    #!/bin/bash
    # awksort: sort the input by size

    awk ‘{
    arr[length $0] = $0
    }
    END {
    for (len in arr)
    printf(“%d\t%s\n”, len, arr[len]) | “sort -n”
    }’ $* |

    cut -f2-

    Invoked with:

    # awksort /etc/passwd

Leave a Reply

Your email address will not be published. Required fields are marked *