Reputation: 13334
I have a script that basically runs 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]
I want the output of this script to be aligned at the colon, for readability. I know to use column -t
but it doesn't quite do what I want (note the double spacing, and that the colons aren't actually aligned):
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]
Here's the output I really want:
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]
What's the easiest/best way to achieve this in the Linux shell?
Edit: If you want to test your answer, you can use curl -s nylen.tv/tmux.txt
instead of tmux ls
.
Upvotes: 3
Views: 2268
Reputation: 580
How about this?
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]
Upvotes: 2
Reputation: 85785
With GNU sed
you can specify the occurrence of the match so where s/ +/ /g
would single space the whole file s/ +/ /2g
single spaces the whole file but after the second match on each line:
$ 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]
To sort out the first columns spacing we use s/: ( +)/\1: /
to match all the spaces from the :
and we store n-1 spaces in the first capture group. We then replace the :
and all the spaces with n-1 spaces followed by a :
followed by a space (making n total space again).
Upvotes: 5