[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to format output

Posted on 2004-11-28
8
Medium Priority
?
184 Views
Last Modified: 2010-05-18
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.
0
Comment
Question by:kevin_ma
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 11

Assisted Solution

by:dimitry
dimitry earned 600 total points
ID: 12693017
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
 

Author Comment

by:kevin_ma
ID: 12693414
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
 
LVL 11

Expert Comment

by:dimitry
ID: 12693639
And how do you represent the employee name for example ?
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 11

Expert Comment

by:dimitry
ID: 12693687
If your employee number is unsigned long, same approach will work:
printf("%-35s%-20lu%-15s\n", enamep, enum, jobp);
0
 
LVL 24

Expert Comment

by:fridom
ID: 12693744
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 12693796
are you talking about a C/C++/Fortran/etc. program, or about any scripting language?
0
 

Author Comment

by:kevin_ma
ID: 12697937
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
 
LVL 24

Accepted Solution

by:
fridom earned 1400 total points
ID: 12698533
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

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month20 days, 12 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question