tags:

views:

32

answers:

2

I need to print the follwoing: * need smart way by printf to print this example

param1 ............... value1

param2 ............... value2

param3 ............... value1

param4 ............... value2

THX

A: 
for i in 1 2 3 4
do
   printf "param%d ................. value%d\n" $i $i 
done
jim mcnamara
+1  A: 

This works in ksh93. I don't know about earlier versions.

This will print the data in columns with up to n dots between them

n=10; printf "%s %s %s\n" $column1 $(printf '.%.0s' {1..$(($n - ${#column1}))}) $column2

Here's a demonstration:

n=10; j=8; for i in a ab abc abcd; do printf "%s %s %3d\n" $i $(printf '.%.0s' {1..$((10 - ${#i}))}) $((j++)); done

And the output:

a .........   8
ab ........   9
abc .......  10
abcd ......  11

A little more complicated and it will do magic tricks:

n=20
string="mnopqrstuvw"
strl=${#string}
k=0
for i in a ab abc abcd abcde abcd abc ab a
do
    j=${string: -$((strl-(k++)))}
    printf "%s %s %s\n" $i $(printf '.%.0s' {1..$((n - ${#i} - ${#j}))}) $j
done 

Output:

a ........ mnopqrstuvw
ab ........ nopqrstuvw
abc ........ opqrstuvw
abcd ........ pqrstuvw
abcde ........ qrstuvw
abcd .......... rstuvw
abc ............ stuvw
ab .............. tuvw
a ................ uvw
Dennis Williamson

related questions