Find the answer to your Linux question:
Results 1 to 5 of 5
Hello everbody: Im having problems with this little thing in a C example Im doing. I want to show in the shell like this example: Name Birthday Tomas 13 Adrian ...
  1. #1
    Just Joined!
    Join Date
    Mar 2010
    Posts
    12

    C. How to arrange the results in the shell [Solved]

    Hello everbody:
    Im having problems with this little thing in a C example Im doing.
    I want to show in the shell like this example:
    Name Birthday
    Tomas 13
    Adrian 24
    Nate 15



    Im trying to use the printf function but the names doesnt have the same lenght, so any idea?
    So I cant do it like this:
    printf("Name Bithday\n");
    printf("%s %d\n");

  2. #2
    Just Joined!
    Join Date
    Apr 2010
    Location
    India
    Posts
    3

    Thumbs up

    Quote Originally Posted by Furiwel View Post
    Hello everbody:
    Im having problems with this little thing in a C example Im doing.
    I want to show in the shell like this example:
    Name Birthday
    Tomas 13
    Adrian 24
    Nate 15



    Im trying to use the printf function but the names doesnt have the same lenght, so any idea?
    So I cant do it like this:
    printf("Name Bithday\n");
    printf("%s %d\n");

    Hello,
    You can use simple code to execute in shell :


    printf "Name\t Birthday\n"
    printf "XYZ\t 12-12-2010\n"

    Regards
    Marsh
    Linux Tech. Support Executive
    Last edited by devils casper; 05-11-2010 at 03:18 AM. Reason: Removed website link

  3. #3
    Just Joined!
    Join Date
    Mar 2010
    Posts
    12
    Hello vmandale

    the thing is that if I put it like that it doesnt make right, because the names havent got the same length. Maybe using some options from the shell?

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    When using printf, you actually can specify the length of your data:
    Code:
    printf("%6s %2d\n", name, birthday);
    This will force the name to be six characters, and the birthday to be 2 characters. Spaces will be added to the left of the output if it is not long enough.

    Note that if any of your data is longer than 6 characters, this will not work (it will not shrink output). Therefore, you will want to use the longest width that you have to.
    DISTRO=Arch
    Registered Linux User #388732

  5. #5
    Just Joined!
    Join Date
    Mar 2010
    Posts
    12
    Quote Originally Posted by Cabhan View Post
    When using printf, you actually can specify the length of your data:
    Code:
    printf("%6s %2d\n", name, birthday);
    This will force the name to be six characters, and the birthday to be 2 characters. Spaces will be added to the left of the output if it is not long enough.

    Note that if any of your data is longer than 6 characters, this will not work (it will not shrink output). Therefore, you will want to use the longest width that you have to.
    Thanks man, it seems its working:

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...