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: 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.

 
 
» You might also be interested in the following articles:
Print files in reverse with coreutils ‘tac’ and ‘rev’ commands
FAQ: How to scrollback in GNU SCREEN?
Apache web server benchmark tool ab
Linux and Unix fmt utility as a simple text formatter
Linux Terminal Performance Comparison



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

0 Responses to “Sort CLI output by line length”


  1. No Comments

Leave a Reply




Friendly Sites:Who is behind Linux Screw?
GeekyBits³ | Bash Cures Cancer | OMG! Ubuntu!
My SysAd Blog | Web Upd8
ZEPY | Linux config Wiki | Planet Sysadmin
a non-geek's linux notes | Linux Today
TuxArena: The arena of Tux | LinuxAlt.Com
My name is Artem N. (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 ››