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: Check out our latest 70-620 dumps & ccda certification written by our 646-046 certified teams to help you in pass real HP2-Z16 exam & NS0-153 dumps.

 

15 Comments

 

  1. April 14, 2009  3:33 pm by Maxim Vuets Reply

    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. Pingback : Sort by Line Length | txt

  3. June 2, 2009  5:37 am by Phil Reply

    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

  4. June 2, 2009  5:38 am by Phil Reply

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

    ;>)

  5. June 18, 2009  2:56 pm by ubuntuverse Reply

    ruby hackers way :)

    ruby -e "puts File.open('/etc/passwd').sort { |a,b| a.length b.length }"

  6. June 23, 2009  12:35 pm by Pierre Relation Reply

    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!

  7. October 19, 2009  3:40 pm by WARLOCKMAN Reply

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

  8. Pingback : Trying to write a script to sort pairs of lines by the length of the 2nd in the pair

  9. April 27, 2011  7:25 pm by squash Reply

    Good write-up. I am a normal visitor of your blog and appreciate you taking the time to maintain the nice site. I'll be a regular visitor for a really long time.

Leave a reply

 

Your email address will not be published.