Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Tabs in Formatted Output Vary in Length

Posted on 2003-11-06
4
Medium Priority
?
350 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 600 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Six Sigma Control Plans

719 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