Solved

Tabs in Formatted Output Vary in Length

Posted on 2003-11-06
4
340 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
work on ods spreadsheet with perl in ubuntu 4 83
compress files in RAR using perl 13 94
Using Perl to parse rows 7 108
Perl Write to Specific line in a file 15 125
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

752 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