Solved

Tabs in Formatted Output Vary in Length

Posted on 2003-11-06
4
344 Views
Last Modified: 2010-03-04
I'm using printf in order to make my output formatted neatly, but the tabs are coming out with variable length. In other words, the "tab" between field 1 and field 2 might be a total of 5 spaces, whereas the "tab" between fields 8 and 9 might be 2.

I thought one of the core ideas of printf was that you could avoid issues like this.

Anybody have any ideas of how I can get the output intended, i.e. fixed number of spaces in tabs?

Here is an example of my code: printf "$var1\t$var2\t$var3...\n"\            etc. etc.

Note the trailing backslash. I have to continue several lines.

Thanks!
0
Comment
Question by:pdouglas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 1

Expert Comment

by:hemaraj123
ID: 9693711
hai,

if my understanding is correct:

use can use print command rather than printf. or else fixed number of spaces instead of tabs '\t'.

If u have any queries, please ask.

Good Luck,

Hemraj
0
 
LVL 28

Accepted Solution

by:
FishMonger earned 150 total points
ID: 9693804
You're using the wrong syntax for printf.  Here's an example of the proper syntax.

printf "%-10s %-20s %-10s %-8d\n", $var1, $var2, $var3, $var4;  # fileds are left justified
0
 
LVL 2

Expert Comment

by:rootkiddy
ID: 9696628
If you are interested you can also look into "format" and call write instead.  An example code snippet is below.

format TEST_FORMAT_TOP =
Colum1             Column2              Column3              Column4
---------------------------------------------------------------------------------
.

format TEST_FORMAT =
@<<<<<<<<< @<<<<<<<<<< @<<<<<<<<<< @<<<<<<<<<
$val1,$val2,$val3,$val4
.

$val1 = "this";
$val2 = "is";
$val3 = "test";
$val4 = "data;

# Now select the format that we want;
$~ = "TEST_FORMAT";
$^ = "TEST_FORMAT_TOP";

# Now call write.  if we had an array which is where this is most useful then it could print out a nice report.
# This is not limited to single lines either.  Its left to your imagination.
write();

0
 
LVL 2

Expert Comment

by:rootkiddy
ID: 9696649
I forgot to past the answer to the question as the above was just an alternative.  FishMonger showed the correct way of using printf for formatting.  To answer the question you are basically printing a variable of random width and preceding it with a tab.  Therefore you are never guaranteed that the format will be as desired unless you check and or pad the data before calling printf.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

617 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