Solved

Tabs in Formatted Output Vary in Length

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now