Reputation: 2234
I have list/array, for example the $PATH env variable, when you do echo $PATH
it prints
/usr/local/bin /usr/bin /bin /usr/sbin /sbin /opt/X11/bin /usr/local/MacGPG2/bin /usr/texbin /usr/local/go/bin
I would like this to be formatted in columns like below, so don’t have to scroll up down much. The $PATH
variable is just an example, I would like to way to be able to do it with any list.
/usr/local/bin /usr/sbin /usr/local/MacGPG2/bin
/usr/bin /sbin /usr/texbin
/bin /opt/X11/bin /usr/local/go/bin
I can print each item in the list in new row using a for loop but can’t figure out a way to do multiple columns. I know there is a column command but that doesn’t seem to do any thing. I’ve tried all the options like echo $PATH | column -c 5
Upvotes: 3
Views: 427
Reputation: 247042
Here you go: pass in a variable name and the number of columns you want
$ function columnize -a listvarname -a ncols
test (count $ncols) -eq 1; or set ncols 1
printf "%s\n" $$listvarname | \
eval paste (yes - | head -n $ncols | tr '\n' " ") | \
column -t
end
A demo using a non-special list name.
$ set list /usr/local/bin /usr/bin /bin /usr/sbin /sbin /opt/X11/bin /usr/local/MacGPG2/bin /usr/texbin /usr/local/go/bin
$ columnize list 3
/usr/local/bin /usr/bin /bin
/usr/sbin /sbin /opt/X11/bin
/usr/local/MacGPG2/bin /usr/texbin /usr/local/go/bin
$ columnize list 5
/usr/local/bin /usr/bin /bin /usr/sbin /sbin
/opt/X11/bin /usr/local/MacGPG2/bin /usr/texbin /usr/local/go/bin
$ columnize list
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
/opt/X11/bin
/usr/local/MacGPG2/bin
/usr/texbin
/usr/local/go/bin
Upvotes: 2
Reputation: 44394
Try this:
echo $PATH|sed 's/ /\
/g'|column -xt -c5
Note: after the \ make sure there follows a new-line
Upvotes: 0