i/o format with printf in bash-shell

7,416

Try this:

printf "%-20s %-30s" $i $est >>tmp
Share:
7,416

Related videos on Youtube

BaRud
Author by

BaRud

Updated on September 18, 2022

Comments

  • BaRud
    BaRud over 1 year

    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)

    • Maksim Levshin
      Maksim Levshin almost 11 years
      2 is not enough for "4.0", try with printf "%-20s %-20s\n" $i $est >>tmp