Solved

Tabs in Formatted Output Vary in Length

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

920 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

13 Experts available now in Live!

Get 1:1 Help Now