List separated by spaces in columns

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

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 will be displayed at compile time.

Like this:

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

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

Now I have found some unix utilities that seem to do this, such as column , but I cannot get it to work with my set.

Do you have any trick that would allow me to do this?

change

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

 @printf '%s\n' $(MODULES) | sort | column 
+4
source share
4 answers
 printf '%-12s%-12s%s\n' $modules 

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

The column utility will automatically create columns for you:

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

This column is the first. If you need a line-first:

 column -x <<< "$(printf '%s\n' $module)" 
+5
source

Using the answer to this question , try something like this:

 echo "Modules activated:" for item in $modules; do printf "%-8s\n" "${item}" done | column 

Potentially adding -x to the column command if you want to wrap the output.

This should be sensitive to terms in terms of the number of columns.

+2
source

Compatible with column and fold :

 echo $modules | column -t | fold | column -t 
+1
source

You can use fold and tab to get approximate formatting:

 echo "module1 module2 module3 module4 mod5 mod6 mod7 module8 module9" | sed 's/ /\t/g' | fold -s -18module1 module2 module3 module4 mod5 mod6 mod7 module8 module9 

but it will not work properly if some module names are longer than 8 characters and some are shorter.

0
source

Source: https://habr.com/ru/post/1413533/


All Articles