Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

unix shellscript: echo question

  • 20-10-2005 10:35AM
    #1
    Registered Users, Registered Users 2 Posts: 7,498 ✭✭✭


    I'm trying to print an amount of time like so;

    2H 43M
    12H 4M
    25H 25M
    1H 4M

    The problem is i want to have the number of characters in each item the same. So ideally it will look like this:

    02H 43M
    12H 04M
    25H 25M
    01H 04M

    How do i get the script to fill in the spaces with 0s but not let the time exceed a certain number of characters?

    Also when I'm printing echo like so;

    echo -e "$high \t $low \t"

    it can look like this:

    314341 432344
    32 432433
    32423434 342343
    3243 3432434


    the tabs don't seem to want to align the text. any ideas?

    thanks in advance.


Comments

  • Registered Users, Registered Users 2, Paid Member Posts: 2,427 ✭✭✭ressem


    I'd suggest using printf instead of echo.
    http://www.mkssoftware.com/docs/man1/printf.1.asp
    It has pretty much the same formatting functionality as the c version

    E.g.
    printf "%09uH %05uM %03uS \n" 2 43 0
    Outputs
    000000002H 00043M 000S
    The 0 following each % indicates that you want 0 padding, and the next number is the width that the field will fill.

    %u indicates unsigned integers.

    So you'd be using "%02uH %02uM" 2 83


  • Registered Users, Registered Users 2 Posts: 7,498 ✭✭✭quarryman


    thanks ressem that's a great help


Advertisement