Difference between revisions of "Printf formatting"

From thelinuxwiki
Jump to: navigation, search
 
Line 20: Line 20:
  
 
  # echo "8.46145e+09" | awk  '{ printf ("%.0f\n",$1) }'     
 
  # echo "8.46145e+09" | awk  '{ printf ("%.0f\n",$1) }'     
8461450000
+
8461450000
  
 
where the %.0f does the work... 0 is for 0 decimal places, f = floating point
 
where the %.0f does the work... 0 is for 0 decimal places, f = floating point

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