-
Notifications
You must be signed in to change notification settings - Fork 0
/
Secrets Of printf-1.txt
81 lines (63 loc) · 2.5 KB
/
Secrets Of printf-1.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
My Secrets Of printf
testing
see: Secrets Of printf.pdf
================================================================================
hex & octal get converted to ascii letter
printf "\x41\n" # \xhh in format hex -> ascii
printf "\101\n" # \nnn in format octal -> ascii
first character of string or variable gets converted to d,o,x
printf "%d\n" "'AB" # ' in string integer
printf "%x\n" "'$var" # ' in string hex
printf "%o\n" "'$var" # ' in string octal, used a lot in Linux
================================================================================
BASH SCRIPT
I normally have LC_ALL=C, you need it unset for some thing s to work
unset LC_ALL
n=25 # first column
p="'10" # ' thousands separator
s=123456 # test number
printf "%-${n}s%s\n\n" "printf format" "output"
f="%-${n}s>%${p}d<\n" # right just
printf "$f" "$f" "$s"
echo
f="%-${n}s>%+-${p}d<\n" # left just +/- no leading zero
printf "$f" "$f" "$s"
f="%-${n}s>%+-${p}d<\n"
printf "$f" "$f" "-$s"
echo
f="%-${n}s>%-+${p}d<\n" # left just +/- no leading zero
printf "$f" "$f" "$s"
f="%-${n}s>%-+${p}d<\n"
printf "$f" "$f" "-$s"
echo
f="%-${n}s>% -${p}d<\n" # left just - no leading zero
printf "$f" "$f" "$s"
f="%-${n}s>% -${p}d<\n"
printf "$f" "$f" "-$s"
echo
f="%-${n}s>% +${p}d<\n" # right just +/- leading zero
printf "$f" "$f" "$s" # zeros not thousand separated BUG!
f="%-${n}s>% +${p}d<\n"
printf "$f" "$f" "-$s"
echo
f="%-${n}s>% ${p}d<\n" # right just - leading zero
printf "$f" "$f" "$s" # zeros not thousand separated BUG!
f="%-${n}s>% ${p}d<\n"
printf "$f" "$f" "-$s"
--------------------------------------------------------------------------------
OUTPUT
printf format output
%-25s>%'10d<\n > 123,456< the ' thousands separator
%-25s>%+-'10d<\n >+123,456 < works for floating point too
%-25s>%+-'10d<\n >-123,456 <
%-25s>%-+'10d<\n >+123,456 <
%-25s>%-+'10d<\n >-123,456 <
%-25s>% -'10d<\n > 123,456 <
%-25s>% -'10d<\n >-123,456 <
%-25s>% +'10d<\n > +123,456< I like this
%-25s>% +'10d<\n > -123,456<
%-25s>% '10d<\n > 123,456< and this
%-25s>% '10d<\n > -123,456<
%-25s>% '010d<\n > 00123,456< no zero thousands separator BUG!
%-25s>% '010d<\n >-00123,456<
================================================================================