Difference between revisions of "Printf formatting"

From thelinuxwiki
Jump to: navigation, search
(Pushed from thelinuxwiki.com.)
 
 
(3 intermediate revisions by one user not shown)
Line 1: Line 1:
just go here...
+
if you want information overload...go here [http://wiki.bash-hackers.org/commands/builtin/printf http://wiki.bash-hackers.org/commands/builtin/printf]
 +
 
 +
 
 +
'''simple column width formatting + justification'''
 +
 
 +
$ printf "%10s%10s\n" "foo" "bar"                                                                                                           
 +
      foo      bar
 +
 
 +
$ printf "%-10s%10s\n" "foo" "bar"            kkkj                                                                                             
 +
foo              bar
 +
 
 +
$ printf "%10s%-10s\n" "foo" "bar"                                                                                                         
 +
      foobar     
 +
 
 +
$ printf "%-10s%-10s\n" "foo" "bar"                                                                                                         
 +
foo      bar     
 +
 
 +
 
 +
== scientific notation to decimal conversion ==
 +
 
 +
# echo "8.46145e+09" | awk  '{ printf ("%.0f\n",$1) }'   
 +
8461450000
 +
 
 +
where the %.0f does the work... 0 is for 0 decimal places, f = floating point
  
http://wiki.bash-hackers.org/commands/builtin/printf
 
  
 
[[category:bash]]
 
[[category:bash]]

Latest revision as of 05:14, 10 January 2014

if you want information overload...go here http://wiki.bash-hackers.org/commands/builtin/printf


simple column width formatting + justification

$ printf "%10s%10s\n" "foo" "bar"                                                                                                            
      foo       bar
$ printf "%-10s%10s\n" "foo" "bar"            kkkj                                                                                               
foo              bar
$ printf "%10s%-10s\n" "foo" "bar"                                                                                                           
      foobar       
$ printf "%-10s%-10s\n" "foo" "bar"                                                                                                          
foo       bar       


scientific notation to decimal conversion

# echo "8.46145e+09" | awk  '{ printf ("%.0f\n",$1) }'    
8461450000

where the %.0f does the work... 0 is for 0 decimal places, f = floating point