Difference between revisions of "Printf formatting"

From thelinuxwiki
Jump to: navigation, search
 
(One intermediate revision by one user not shown)
Line 7: Line 7:
 
       foo      bar
 
       foo      bar
  
  $ printf "%-10s%10s\n" "foo" "bar"                                                                                                          
+
  $ printf "%-10s%10s\n" "foo" "bar"           kkkj                                                                                             
 
  foo              bar
 
  foo              bar
  
Line 16: Line 16:
 
  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
  
  
 
[[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