Search This Blog

Monday, March 7, 2011

How to sort data on more then one column with Linux sort command

You caught always yourself looking for old recipes you did years ago. It is time to document this once and for all.

This will produce a list of services maintained by the upstart init daemon on the Linux system that:
  • 1st is sorted alphabetically base on the status description
  • 2th is sorted numerically base on the pid value
  • 3th is sorted alphabetically base on the service name

$ initctl  list | sed 's/ (/(/g'  | sort -k2,2d -k4,4n -k1,1d | column -t  
bridge-network-interface(eth0)    start/running
bridge-network-interface(lo)      start/running
bridge-network-interface(pan0)    start/running
bridge-network-interface(virbr0)  start/running
bridge-network-interface(wlan0)   start/running
network-interface(eth0)           start/running
network-interface(lo)             start/running
network-interface(pan0)           start/running
network-interface-security        start/running
network-interface(virbr0)         start/running
network-interface(wlan0)          start/running
ufw                               start/running
upstart-udev-bridge               start/running,  process  358
udev                              start/running,  process  366
smbd                              start/running,  process  870
rsyslog                           start/running,  process  889
dbus                              start/running,  process  891
ssh                               start/running,  process  897
gdm                               start/running,  process  904
network-manager                   start/running,  process  905
avahi-daemon                      start/running,  process  912
tty4                              start/running,  process  1046
tty5                              start/running,  process  1051
tty2                              start/running,  process  1060
tty3                              start/running,  process  1062
tty6                              start/running,  process  1065
acpid                             start/running,  process  1070
cron                              start/running,  process  1084
atd                               start/running,  process  1085
libvirt-bin                       start/running,  process  1096
tty1                              start/running,  process  1627
nmbd                              start/running,  process  2156
alsa-mixer-save                   stop/waiting
anacron                           stop/waiting
apport                            stop/waiting
console-setup                     stop/waiting
control-alt-delete                stop/waiting
dmesg                             stop/waiting
failsafe-x                        stop/waiting
hostname                          stop/waiting
hwclock                           stop/waiting
hwclock-save                      stop/waiting
irqbalance                        stop/waiting
module-init-tools                 stop/waiting
mountall                          stop/waiting
mountall-net                      stop/waiting
mountall-reboot                   stop/waiting
mountall-shell                    stop/waiting
mounted-dev                       stop/waiting
mounted-tmp                       stop/waiting
mounted-varrun                    stop/waiting
networking                        stop/waiting
plymouth                          stop/waiting
plymouth-log                      stop/waiting
plymouth-splash                   stop/waiting
plymouth-stop                     stop/waiting
procps                            stop/waiting
rc                                stop/waiting
rcS                               stop/waiting
rc-sysinit                        stop/waiting
screen-cleanup                    stop/waiting
udev-finish                       stop/waiting
udevmonitor                       stop/waiting
udevtrigger                       stop/waiting
ureadahead                        stop/waiting
ureadahead-other                  stop/waiting
usplash                           stop/waiting

Google and system documentation is your friend.

$ man sort
$ info coreutils 'sort invocation'

Sorting based on Multiple columns

Sort Files Like A Master With The Linux Sort Command

No comments:

Post a Comment