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

 
 
» You might also be interested in the following articles:
FAQ: How to scrollback in GNU SCREEN?
Print files in reverse with coreutils ‘tac’ and ‘rev’ commands
Linux and Unix fmt utility as a simple text formatter
Linux Terminal Performance Comparison
Ping range of IP addresses in parallel with fping



» Want to stay up to date? Subscribe to our E-MAIL or RSS feed!

13 Responses to “Sort CLI output by line length”


  1. 1 Maxim Vuets

    Nice. Just few tips in addition.

    The last “awk ‘{$1=”"; print $0 }’” may be replaced with lighter “cut -d ‘ ‘ -f 2-”.

    And a dumb Perl one-liner:

    $ perl -e ‘print sort { length $a length $b } ‘ </etc/passwd

  2. 2 FitzgeraldsCasinoTunica

    Great tips both.

  3. 3 LakeHavasuSpringbreak

    lol “Want some… no problem” I like that.

  4. 4 LotoQuebecResults

    Awesome, thanks for the info.

  5. 5 LottoZheroGravity

    Interesting, thanks for the post.

  6. 6 LouisianaPowerballNumbers

    Very good

  7. 7 MGMGrandDetroit

    Very interesting, thanks for the information.

  8. 8 Phil

    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

  9. 9 Phil

    Well, it was clearer ’till the tabs all went away.

    ;>)

  10. 10 ubuntuverse

    ruby hackers way :)

    ruby -e “puts File.open(‘/etc/passwd’).sort { |a,b| a.length b.length }”

  11. 11 Pierre Relation

    Oh, don’t worry about the tabs going away. We figured it out just as well. It works indeed like that so thanks for sharing!

  12. 12 WARLOCKMAN

    ????… ??????? ??????? ????????! ??? ??? ??? ???????…? ????????!

  1. 1 Sort by Line Length | txt

Leave a Reply




Friendly Sites:Who is behind Linux Screw?
Aspiring Sysadmin | GeekyBits³ | Bash Cures Cancer | TOTMS
Linux Operating System | Small Linux Deployments | My SysAd Blog
The Danesh Project | ZEPY | Linux config Wiki | Planet Sysadmin
The Sys Admin | {buhay sysad} | a non-geek's linux notes
Linux HOWTOs, Tutorials & Projects with Adam Palmer | LinuxAlt.Com
My name is Artem Nosulchik (artiomix AT gmail DOT com) and I'm Linux/Unix, Cisco systems engineer. The main idea of Linux Screw is to share relevant knowledge, skills and observations over The Web. Here you can find a lot of information related to different Linux distributions, FreeBSD, IOS as well as a other Open Source around staff. Read more ››