3

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

4
  • 1
    That's not what echo $PATH gets you. And the list of commands isn't at all related to what echo $PATH gets you. I'm confused. You want to print a display of some set of values in nicely formatted columns? Commented Apr 28, 2015 at 16:14
  • @EtanReisner: it is what you get if you echo $PATH on the fish shell. Commented Apr 28, 2015 at 16:33
  • @cdarke Ah. Fair enough. I missed the fish tag there at the end. Commented Apr 28, 2015 at 16:34
  • @EtanReisner The list of commands was just an example of the format I would like, I’ve removed and replaced it with the real output I would like. Sorry about the confusion. Commented Apr 28, 2015 at 17:09

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

echo $PATH|sed 's/ /\
/g'|column -xt -c5

Note: after the \ make sure there follows a new-line

1 Comment

This just prints the output in one column and multiple rows, which I could have done with a for loop. Thanks anyway, this seems faster than iterating through a list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.