Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.3 Field Widths with printf

Another useful feature of printf conversion (the % directives) lies in the area of field widths. These are values included in the % conversion directive to specify the width of a field and are especially useful in lining up columns of data. The field width value is placed between the % and the conversion character such as:

printf "%7d\n", 12345; printf "%7d\n", 123;
will create right justified output:

12345 123
Negative values can be used to create left justified fixed width data fields. This is of particular use when creating multi-column data output:

printf "%-10d", 12345; printf "%-10d\n", 123; printf "%-10d", 1; printf "%-10d\n", 123456;
This will output the data in left justified columns:

12345 123 1 123456
The conversion value can also be used to control the number of decimal places displayed for floating point (%f) numbers:

printf "%15.3f\n", 53/9; printf "%15.4f\n", 53/9; printf "%15.7f\n", 53/9;
This will output right justified data with varying numbers of digits after the decimal point as defined in the conversions:

5.889 5.8889 5.8888889



 
 
  Published under the terms of the GNU General Public License Design by Interspire