I am trying to output the results of my shell script using printf. To keep the results aligned in column, I am using tab as:

printf "%s\t %s\t %s\t %s\t %s\t %s\t %s\n" $i $est $var_m $jk $mst $var_M  $Tes >>tmp

But the problem is, for some cases, the results are bit long, as a output of say,

var_M=`echo "scale=8;(-1/$jk + (e(2*$jk)+1)/(e(2*$jk)-1))"|bc -l `

So, it will be better for me, if I can format print to the column, something like:

printf "%s *goto column 20* %s *go to column 30* " $i $est >>tmp

is this possible?

I have checked this thread and also the man pages, but failed to find it. Is this possible?

NB: I do not have any special reason to stick to printf. so any other suitable command that can do the thing (easily) is also welcome.

EDIT: tried: printf "%-2s %-8s\n" $i $est >>tmp which is not clearly working:

4.0 0.17169E-02
5. 0.17156E-02
10. 0.17129E-02

(The 2nd row/2nd column is not aligned, just like the case of list-formatted output)

  • 2 is not enough for "4.0", try with printf "%-20s %-20s\n" $i $est >>tmp – damphat Aug 13 '13 at 9:17
  • You should always quote your shell variable references (e.g., "$i" and "$est") unless you have a good reason not to, and you’re sure you know what you’re doing. – G-Man Jul 29 '16 at 4:17

Try this:

printf "%-20s %-30s" $i $est >>tmp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.