Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print space-separated list in columns

Tags:

linux

bash

unix

I have this Makefile that has a variables named "MODULES" which lists all the modules I have activated in my build.

This list is separated by spaces, so it looks like this when I do echo $(MODULES):

module1 module2 module3 module4 mod5 mod6 mod7 module8 module9

What I would like to do is present this list in some columns that would be displayed at compilation time.

Like this:

Modules activated:
module1 module2 module3 
module4 mod5    mod6 
mod7    module8 module9

Ideally, the column withs would adjust to the width of the largest module in the column (see mod7); and the number of columns would be adjusted according to the width of the current terminal.

Now, I found some unix utilities that seem to do that, like column, but I can't make it work with my set.

Do you have some trick that would allow me to do that?

edit:

With the answer chosen below, I finally cracked this command in my Makefile:

@printf '%s\n' $(MODULES) | sort | column
like image 984
Gui13 Avatar asked Feb 20 '23 10:02

Gui13


1 Answers

printf '%-12s%-12s%s\n' $modules

This consumes the contents of the variable for the number of times a placeholder appears in the format string and repeats until all the contents are consumed.

The column utility will automatically produce columns for you:

column <<< "$(printf '%s\n' $module)"

That's column-first. If you want row-first:

column -x <<< "$(printf '%s\n' $module)"
like image 142
Dennis Williamson Avatar answered Feb 22 '23 22:02

Dennis Williamson