Sunday, May 18, 2014

Unix Shell Standard Output: printf

1. Escape sequences
 #! /bin/bash  

 #\n means newline operator  
 printf "Hello \n world\n"  
 #output:  
 #Hello  
 # world!  

 #\t means horizontal tab  
 printf "Hello\tworld\n"  
 #output: Hello  world  

 #\v means vertical tab  
 printf "Hello\vworld\n"  
 #output:  
 #Hello  
 #   world
  
 #\ddd represents character with ascii code "ddd", which  
 #is octal value. 141 is "97"(decimal), which is the code  
 #of 'a'  
 printf "\141\n"  
 #output: a  

 #\\ represents a literal \
 printf "\\ \n"
 #output: \

2. format specifiers
 #! /bin/bash  

 #%s means replacing with string, and '\n' and other escape sequences  
 #will just be taken as normal string  
 printf "<%s>\n" "Hello\nworld!"  
 #output:  
 #<Hello\world!>"  

 #%b will take "\n" as the newline operator in a string, instead of  
 #part of string  
 printf "<%b>\n" "Hello\nworld!"  
 #output:  
 #<Hello\  
 #world!>  

 #%c means ascii character, if provided parameter is a string, then  
 #only first character of string will be taken.  
 printf "<%c>\n" "Hello"  
 #output:  
 #<H>  

 #%d %i means integer, the provided parameter must be integer  
 printf "<%d %i>\n" 19 20  
 #output:  
 #<19 20>  

 #%e means float, with format ###e[+-]###  
 printf "<%e>\n" 0.99999999999999  
 #output:  
 #<1.000000e+00>  

 #%E means float, with format ###E[+-]###  
 #the only difference from %e is, it is uppercase  
 printf "<%E>\n" 0.99999999999999  
 #output:  
 #<1.000000E+00>  

 #%f means float, with format ###.####  
 printf "<%f>\n" 0.99999999999999  
 #output:  
 #<1.000000>  

 #%g means: %f or %e, which one is shorter get output  
 printf "<%g>\n" 0.88888888888888  
 #output:  
 #<0.888889>  

 #%G means: %f or %E, which one is greater get output  
 printf "<%G>\n" 0.88888888888888  
 #output:  
 #<0.888889>  

 #%o means unsigned octal value, if provided parameter is minus  
 #number, the value is undefined  
 printf "<%o>\n" 97  
 #output:  
 #<141>  (octal value of 97)  

 #%x means unsigned hexadecimal number, use a-f. If provided parameter  
 #is minus number, the value is undefined  
 printf "<%x>\n" 15  
 #output:  
 #<f>  

 #%X means unsigned hexadecimal number, use A-F. If provided parameter  
 #is minus number, the value is undefined. The only difference from %x  
 #is: it used A-F(uppercase) instead of a-f(lowercase)  
 printf "<%X>\n" 15  
 #output:  
 #<F>  

 #%% is a literal %  
 printf "<%%>\n"  
 #output:  
 #<%>  

3. printf flags
 #! /bin/bash  
 #"10" means the width is 10, "-10" means width is 10 while   
 # left-justify it, without "-"(minus) shell will right-justify  
 #the element  
 printf "|%10s|\n" Hello  
 #output:  
 #|   Hello|  

 printf "|%-10s|\n" Hello  
 #output:  
 #|Hello   |  

 printf "|%10d|\n" 100  
 #output:  
 #|    100|  

 printf "|%-10d|\n" 100  
 #output:  
 #|100    |  

 #space before d means, prefix positive number with space,   
 #for negative number, prefix a space and minus sign  
 printf "|% d|| %d|\n" 100 -100  
 #output:  
 #| 100|| -100|  

 printf "|% f|| %f|\n" 0.88888 -0.88888  
 #output:  
 #| 0.888880|| -0.888880|  

 printf "|% e|| %e|\n" 0.888888888 -0.88888888  
 #output:  
 #| 8.888889e-01|| -8.888889e-01|  

 #"+" sign means prefix "+" for positive number, prefix "-" for  
 #negative number  
 printf "|%+d||%+d|\n" 100 -100  
 #output:  
 #|+100||-100|  

 #"#" will prefix octal number with a 0  
 #"#" will prefix hex number with a "0x"  
 #"#" will make %f %e %E %g %G always have decimal point  
 printf "|%#o||%o||%#x||%x||%#g||%g||%#G||%G|\n" 8 8 15 15 15 15 15 15  
 #output:  
 #|010||10||0xf||f||15.0000||15||15.0000||15|  
 #"0" will pad output with 0s(not effictive on %s)  

 printf "|%010s||%10s||%05d||%5d||%010f||%10f|\n" Hello Hello 5 5 5 5  
 #output:  
 #|   Hello||   Hello||00005||  5||005.000000|| 5.000000|  

4. Precision
 #! /bin/bash  
 #precision applying on %s means: maximum number of characters  
 #that could be shown. If provided string doesn't have enough  
 #characters, then it has no effect.  
 printf "%.3s %.5s %.10s\n" Hello Hello Hello  
 #output:  
 #Hel Hello Hello  

 #precision applying on %d means: specified number of digits must be  
 #shown, if not enough, pad with 0, if number itself has more  
 #digits, then it has no effect  
 printf "%.2d %.5d %.5d\n" 100 100 -100  
 #output:  
 #100 00100 -00100  

 #%.2d ask show 2 digits but 100 already has 3 digits, then it has no  
 #effect in this case  
 #%i same as %d  
 printf "%.2i %.5i %.5i\n" 100 100 -100  
 #output:   
 #100 00100 -00100  

 #precision applying on %o means: specified number of digits must be shown  
 #if number doesn't have enough digits, it will be padded with 0  
 #if number has more digits, then it has no effect. %o doesn't support  
 #negative number, it would be undefined.  
 printf "%.2o %.5o %.5o\n" 100 100 -100  
 #output:  
 #144 00144 1777777777777777777634  

 #precision applying on %u(unsigned decimal): specified number of digits must  
 #be shown. If number doesn't have enough digits, it will be padded with 0  
 #if number has more digits, then it has not effect.  
 printf "%.2u %.5u\n" 100 100  
 #output:  
 #100 00100  

 #precision applying on %x: specified number of digits must be shown. If number  
 #doesn't have enough digits, it will be padded with 0. If number has more digits,  
 #then it has no effect.  
 printf "%.1x %.2x %.5x\n" 15 15 15  
 #output:  
 #f 0f 0000f  

 #precision applying on %e: specified number of digits must be shown after the  
 #decimal point. If number has more digits after decimal point, then it will  
 #be rounded. If number doesn't have enough digits after decimal point, then  
 #it will be padded with 0 afterwards.  
 printf "%.2e %.5e %.10e\n" 10.88888 10.88888 10.88888  
 #output:  
 #1.09e+01 1.08889e+01 1.0888880000e+01  

 #precision applying on %f: specified number of digits must be shown after the  
 #decimal point. If number has more digits after decimal point, then it will  
 #be rounded. If number doesn't have enough digits after decimal point, then  
 #it will be padded with 0 afterwords.  
 printf "%.2f %.5f %.10f\n" 10.88888 10.88888 10.88888  
 #output:  
 #10.89 10.88888 10.8888800000  

 #precision on g means: the specified number of significant digit  
 #If number doesn't have enough digits, then it won't be padded with 0, it  
 #just show its original number of digits  
 #If number has more digits, it will be rounded accordingly.  
 #Note the %.1g result, it is transformed to scientific format to satisfy   
 #"1" siginificant digit requirement.  
 printf "%.1g %.2g %.5g %.10g\n" 10.88888 10.88888 10.88888 10.88888  
 #output:  
 #1e+01 11 10.889 10.88888  

5. ASCII Code Character
 #! /bin/bash  
 #if using "'"(single quote) to prefix string character,  
 #it means we are trying to get the ASCII code  
 printf "%c %d\n" a "'a"  
 #output:  
 #a 97  

No comments:

Post a Comment