Sysop.Fr
Cat├ęgories

apache

awk

bash

charmap

date

echo

encoding

find

grep

hexdump

irc

iso3166

ls

mysql

printf

python

read

sed

seq

smpp

sort

tar

ucp

Articles

Mémo de la commande printf

La commande (et fonction) printf permet de mettre en forme l'affichage des données.

1. Syntaxe

printf FORMAT [ARGUMENT]...
%sstring
%greal
%iinteger
%ddecimal
%ffloat

2. Exemples

Affichage d'un chiffre simple en deux décimales sans retour chariot
root@ubuntu:~# printf '%02d' 3
03root@ubuntu:~#
Affichage d'un chiffre simple en deux décimales avec retour chariot
root@ubuntu:~# printf '%02d\n' 6
06
Affichage HH:MM:SS
root@ubuntu:~# for HH in $(seq 0 5)
> do
> for MM in $(seq 0 59)
> do
> for SS in $(seq 0 59)
> do
> printf "%02d:%02d:%02d\n" ${HH} ${MM} ${SS}
> done
> done
> done
00:00:00
00:00:01
00:00:02
00:00:03
00:00:04
00:00:05
00:00:06
00:00:07
00:00:08
...
Affichage en colonnes
root@ubuntu:~# find /opt/ -type d |while read mydir; do printf '%-45s %6i\n' $mydir `find $mydir -maxdepth 1 -type f 2>/dev/null|wc -l`; done
/opt/                                              0

root@ubuntu:~# root@sd-26764:~# for mydir in $(ls -d /usr/share/doc/*); do printf '%-45s => %6i\n' $mydir `find $mydir -maxdepth 1 -type f 2>/dev/null|wc -l`; done
/usr/share/doc/accountsservice                =>      3
/usr/share/doc/acpid                          =>      4
/usr/share/doc/adduser                        =>      3
/usr/share/doc/apparmor                       =>      2
/usr/share/doc/apport                         =>      6
/usr/share/doc/apport-symptoms                =>      2
/usr/share/doc/apt                            =>      1
/usr/share/doc/aptitude                       =>      6
/usr/share/doc/apt-transport-https            =>      2
/usr/share/doc/apt-utils                      =>      1
/usr/share/doc/apt-xapian-index               =>      4
/usr/share/doc/at                             =>      5
/usr/share/doc/autoconf                       =>      6
/usr/share/doc/automake                       =>      6
/usr/share/doc/autotools-dev                  =>      5
/usr/share/doc/base-files                     =>      4
/usr/share/doc/base-passwd                    =>      5
/usr/share/doc/bash                           =>     11
/usr/share/doc/bash-completion                =>      5
...

root@ubuntu:~# find /tmp -type f -ls 2>/dev/null |awk '{printf "%02d-%s-12\n" , $9 , $8}' |sort |uniq -c
      2 19-Dec-12
      1 23-Nov-12
      1 24-Nov-12