How to format output

Lets say you have a table, how do you format your output so it aligns with the column headings?  For example Employee Name would start on character 1, Employee Number would start on 35 and Job would start on 55.  So when you print out the data it'll line up flush with the headings.

Employee Name                 Employee Number                   Job
==========                 ============                  ====
John Smith                        111111111111111                   Manager
Don Wood                         222222222222222                   Clerical

Thanks.
kevin_maAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dimitryCommented:
The simplest way is to do the next:
Let's say, enamep - is pointer to Employee name, enump - is pointer to string number, jobp is pointer to job name, then
  printf("%-25s%-20s%-15s\n", enamep, enump, jobp);
will give you this:
John Smith                        111111111111111                   Manager

25, 20 and 15 are constant sizes for your strings.
'-' means to start from the left and add additional blanks from the right.
0
kevin_maAuthor Commented:
That doesn't sound too bad, tho I'm not familiar with using pointers in Linux, so I'd like to have a solution using variables.
0
dimitryCommented:
And how do you represent the employee name for example ?
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

dimitryCommented:
If your employee number is unsigned long, same approach will work:
printf("%-35s%-20lu%-15s\n", enamep, enum, jobp);
0
fridomCEO/ProgrammerCommented:
Well you can use Perls report facilities, or any other tools capable of doing formatted output. The primary tool for that on Linux is probably awk. However you have not told us from where you'll get the stuff to output. And in which language or form it is available.

Regards
Friedrich
0
ahoffmannCommented:
are you talking about a C/C++/Fortran/etc. program, or about any scripting language?
0
kevin_maAuthor Commented:
I'm doing it by using shell commands only.  I get the data from another file, I already know how to grab the data and store it in variables.  Now I want to print out those variables aligned correctly.

for the variable names, lets say...

the employee name is $empname
the employee number is $empnum
the employee job is $empjob

Thanks.
0
fridomCEO/ProgrammerCommented:
Then use awk, I assuem the following file format
Empname;empno;empjob then this

BEGIN {FS=";"; format="%25s%5d%25s\n"}
{printf(format, $1, $2, $3)}

with this data
foo bar;1;Foo writer
foo foo;2;Foo foo writer

will print:
 awk -f print_formatted_with_awk.awk addresses.txt
                  foo bar    1               Foo writer
                  foo foo    2           Foo foo writer
You can change the justification of strings with the - sign before the number in the %s directives.

For that to understand you have to know that awk puts a loop around the lines in a file so it reads a line, and then it takes some action on that line after that it implicitly reads the next line, awk is one of the underestimated tools on Unix....

Regards
Friedrich
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.