我有一个脚本,基本上运行tmux ls
:
session1: 3 windows (created Fri Sep 20 13:16:13 2013) [157x56] stuff: 3 windows (created Fri Sep 20 13:25:21 2013) [157x56] asdf: 2 windows (created Sun Sep 29 23:06:33 2013) [77x17] (attached) website: 1 windows (created Tue Sep 24 17:22:14 2013) [157x26]
为了便于阅读,我希望将脚本的输出与冒号alignment。 我知道使用column -t
但它不完全是我想要的(注意双间距,并且冒号实际上并排alignment):
session1: 3 windows (created Fri Sep 20 13:16:13 2013) [157x56] stuff: 3 windows (created Fri Sep 20 13:25:21 2013) [157x56] asdf: 2 windows (created Sun Sep 29 23:06:33 2013) [77x17] (attached) website: 1 windows (created Tue Sep 24 17:22:14 2013) [157x26]
这是我真正想要的输出:
session1 : 3 windows (created Fri Sep 20 13:16:13 2013) [157x56] stuff : 3 windows (created Fri Sep 20 13:25:21 2013) [157x56] asdf : 2 windows (created Sun Sep 29 23:06:33 2013) [77x17] (attached) website : 1 windows (created Tue Sep 24 17:22:14 2013) [157x26]
在Linux shell中实现这个最简单/最好的方法是什么?
编辑 :如果你想testing你的答案,你可以使用curl -s nylen.tv/tmux.txt
而不是tmux ls
。
使用GNU sed
你可以指定匹配的发生,所以s/ +/ /g
是单个空格,整个文件s/ +/ /2g
单个空格整个文件,但在每行的第二个匹配之后:
$ column -t file | sed -re 's/: ( +)/\1: /' -e 's/ +/ /2g' session1 : 3 windows (created Fri Sep 20 13:16:13 2013) [157x56] stuff : 3 windows (created Fri Sep 20 13:25:21 2013) [157x56] asdf : 2 windows (created Sun Sep 29 23:06:33 2013) [77x17] (attached) website : 1 windows (created Tue Sep 24 17:22:14 2013) [157x26]
为了整理第一列间距,我们用s/: ( +)/\1: /
来匹配s/: ( +)/\1: /
中的所有空格,并且在第一个捕获组中存储n-1个空格。 然后,我们用n-1个空格替换:
和所有空格,然后是一个:
后面是一个空格(再次创建n个总空间) 。
这个怎么样?
cat tmux.txt | sed 's/:/ : /' | column -t -o' ' | sed 's/ \+/ /2g' session1 : 3 windows (created Fri Sep 20 13:16:13 2013) [157x56] stuff : 3 windows (created Fri Sep 20 13:25:21 2013) [157x56] asdf : 2 windows (created Sun Sep 29 23:06:33 2013) [77x17] (attached) website : 1 windows (created Tue Sep 24 17:22:14 2013) [157x26]