Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to print?

Status
Not open for further replies.

zshukralla

Programmer
Joined
Aug 11, 2005
Messages
2
Location
BH
What sprintf function does?
How can print left justified and blanks padded with fix length?


Thanks
 
Hi zshukralla,

sprintf is very much like printf, except where printf prints to the screen by default, the output of sprintf is returned as a string.

Example:

formatstring=sprintf("This is a %s string","formatted");
print "formatstring\n";




The output will be: 'This is a formatted string'

The format specifiers for sprintf are the exact same as for printf.

To get a left justification, use the minus (-) modifier.
To get fixed spacing, use a numeric modifier.

Example:

#!/usr/bin/awk -f

BEGIN{
formatstring=sprintf("%5d, %-10d, %s",12345,67890,"Red");
print formatstring;
exit;
}

% test
12345, 67890 , Red

For more info, search for sprintf on this site, or check any awk manual for printf or sprintf. If you don't have an awk manual, check a C manual or a perl manual. The syntax is pretty much identical.

Good luck,
Grant.
(Hope you get an 'A').
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top