Lum – Aligning columns using awk

awkcolumns

I am trying to align these columns

super+t sticky toggle
super+Shift+space floating toggle
super+Shift+r restart
super+Shift+d mode $mode_launcher
super+Shift+c reload
super+r mode resize
super+Return i3-sensible-terminal
super+q kill
super+n Nautilus scratchpad show
super+m neomutt scratchpad show
super+minus scratchpad show
super+f fullscreen toggle
super+c bar mode toggle
super+button2 kill
super+alt+x systemctl -i suspend
super+alt+v cmus
super+alt+m neomutt
super+alt+c ~/bin/editinvim
super+alt+b ranger

I have tried to use awk but no luck there.
The preferred format is like

super+Shift+d     mode $mode_launcher     
super+alt+c       ~/bin/editinvim
super+alt+b       ranger

Best Answer

You could re-print the first column, left-justified in a suitably wide field:

$ awk '{$1 = sprintf("%-30s", $1)} 1' file
super+t                        sticky toggle
super+Shift+space              floating toggle
super+Shift+r                  restart
super+Shift+d                  mode $mode_launcher
super+Shift+c                  reload
super+r                        mode resize
super+Return                   i3-sensible-terminal
super+q                        kill
super+n                        Nautilus scratchpad show
super+m                        neomutt scratchpad show
super+minus                    scratchpad show
super+f                        fullscreen toggle
super+c                        bar mode toggle
super+button2                  kill
super+alt+x                    systemctl -i suspend
super+alt+v                    cmus
super+alt+m                    neomutt
super+alt+c                    ~/bin/editinvim
super+alt+b                    ranger

If you want to choose a suitable width automatically based on the length of column 1, then:

awk '
  NR==FNR {w = length($1) > w ? length($1) : w; next} 
  {$1 = sprintf("%-*s", w+4, $1)} 
  1
' file file
Related Question