我正在尝试对齐这些列
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
我尝试过使用awk
但没有运气。首选格式如下
super+Shift+d mode $mode_launcher
super+alt+c ~/bin/editinvim
super+alt+b ranger
答案1
或者,该命令column -t
可用于设置列中文本的格式。
column -t file
默认的列分隔符是" "
。在示例中,行有多个空格,但只有第一个分隔列:我们可以用“:”替换第一个并将其用作分隔符。
$ sed 's/ /:/1' file | column -s ':' -t
super+t sticky toggle
super+Shift+space floating toggle
super+Shift+r restart
super+Shift+d mode $mode_launcher
...
答案2
您可以重新打印第一列,在适当宽的字段中左对齐:
$ 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
如果你想根据第1列的长度自动选择合适的宽度,那么:
awk '
NR==FNR {w = length($1) > w ? length($1) : w; next}
{$1 = sprintf("%-*s", w+4, $1)}
1
' file file