• BASH > formater du texte avec printf

       

       

      B.A.BA

      De façon générale : printf <format> <arguments>

      printf "%s" "toto"toto ( ici, %s veut dire string )

       

      % exprime le renvoie à un argument ( Il y a autant d’arguments qu’il y a de renvois ) :

      printf "%s %s" "toto" "mimi" toto mimi

      printf "%s %s" "toto" toto

      LES CARACTÈRES

      Renvoie le premier caractère de la chaine : printf "%c" "abcdef"a

      répéter un caractère

      printf ‘%*s’ 10 | tr " " "-"———-

      ou

      eval printf ‘%.0s-’ {1..10}———-

       

       

      LES CHAINES DE CARACTÈRES

      Renvoie littéralement la chaine (sans interpréter les backslash) : printf "%s" "toto\n" toto\n

       

      Échapper les backslash pour utilisation dans une variable : printf "%q" "toto\n"toto\\n

       

      Interpréter les backslash des arguments : printf "%b" "to\nto" to to

      > équivaut à printf "%s\n" "toto"

       

      Ecrire un texte avec au moins N caractères : printf "%Ns" "$chaine" ou printf "%*s" 3 "abcdef"

       

      Si $chaine a moins de caractères que N, alors autant d’espaces seront créés pour que l’ensemble fasse N caractères. Sinon, la chaine n’a pas d’espaces ajoutés et n’est pas tronquée.

       

      printf "%9s" "abcdef"…abcdef ( ajout de 3 espaces pour former 9 caractères)

      printf "%*s" 9 "abcdef"…abcdef ( ajout de 3 espaces pour former 9 caractères)

       

      printf "%3s" "abcdef" abcdef

      printf "%*s" 3 "abcdef"abcde

       

       

      N’écrire que les N premiers caractères d’une chaine

      printf "%.Ns" "$chaine"

       

      Si N est plus grand que le nombre de caractères de $chaine, aucun espace ne sera ajouté à la fin de $chaine. Sinon, $chaine sera tronquée en N caractères.

       

      printf "%.s" "abcdef" renvoie une chaine vide

      printf "%.1s" "abcdef" a ( équivaut à printf "%c" "abcdef" )

      printf "%.3s" "abcdef" abc

      printf "%.18s" "abcdef" abcdef

       

      Écrire du texte avec le résultat de commandes

      printf "`uname -s` est sur un processeur `uname -m`."Linux est sur un processeur i686.

      LES NOMBRES

      Écrire un entier : printf "%d" 55

       

      Écrire des entiers avec du texte : printf "Affiche %d et %d." 64 1500 Affiche 64 et 1500.

       

      Écrire un nombre à virgule (float) (par défaut, 6 chiffres après la virgule)

      printf "%f" 55.000000

      printf "%.2f" 4.34.30

      printf "%.*f" 2 4.34.30 (l’astérisque veut dire : le premier argument)

       

       

      Écrire avec des variables : printf "il y a $COLUMNS colonnes."il y a 126 colonnes.

       

      Écrire des nombres à 5 chiffes "remplis" de vide : printf "Le nombre %5d." 15Le nombre 15.

       

      Écrire des nombres à 5 chiffres "remplis" de zéros : printf "Le nombre %05d." 15Le nombre 00015.

       

      Écrire une liste de nombres à 3 chiffres allant de 001 à 099

      for (( num=1 ; num<=99 ; num+=1 )); do
         printf "%03d\n" $num;
      done

       

      Convertir un hexadécimal en décimal : printf "%d" 0xF15

       

      Convertir un décimal en Hexadécimal :

      printf "0x%X" 150xF

      printf "0x%x" 150xf

       

      Convertir un décimal en octal : printf "0%o" 8010

       

      Convertir un octal en décimal : printf "%d" 0108

       

       

      Écrit un hexadécimal à virgule au format C99.

      printf "%a" "10″0×1,4p+3

      printf "%A" "10″0×1,4P+3 (en majuscules)

       

      Renvoie l’expression en exponentielle 10 au format <N>±e<N>

      printf "%e" "95″9,500000e+01

      printf "%E" "95″9,500000E+01 ( en majuscules)

      LES DATES

      %(FORMAT)T retourne la date dans le FORMAT donné. L’argument associé est le nombre de secondes Epoch, ou -1 (date actuelle, par défaut) ou -2 (démarrage du shell).

       

      printf ‘Nous sommes la semaine n° %(%U/%Y)T.\n’ -1

      Nous sommes la semaine n° 16/2014.

       

      Lire la man de strftime(3)pour en savoir davantage sur le FORMAT.

       

       

      printf "%-10s |\n" "$txt"

      The %s means to interpret the argument as string, and the -10 tells it to left justify to width 10 (negative numbers mean left justify while positive numbers justify to the right). The \n is required to print a newline, since printf doesn’t add one implicitly.

       

       

       

       

       

      The printf command

      Syntax

       

      printf <FORMAT> <ARGUMENTS...>

      The text format is given in <FORMAT>, while all arguments the formatstring may point to are given after that, here, indicated by <ARGUMENTS…>.

      Thus, a typical printf-call looks like:

      printf "Surname: %s\nName: %s\n" "$SURNAME" "$FIRSTNAME"

      where "Surname: %s\nName: %s\n" is the format specification, and the two variables are passed as arguments, the %s in the formatstring points to (for every format specifier you give, printf awaits one argument!).

       

       

      Options

      -v VAR

      If given, the output is assigned to the variable VAR instead of printed to stdout (comparable to sprintf() in some way)

      The -v Option can’t assign directly to array indexes in Bash versions older than Bash 4.1.

       

      In versions newer than 4.1, one must be careful when performing expansions into the first non-option argument of printf as this opens up the possibility of an easy code injection vulnerability.

      $ var='-vx[$(echo hi >&2)]'; printf "$var" hi; declare -p x
      hi
      declare -a x='([0]="hi")'

      …where the echo can of course be replaced with any arbitrary command. If you must, either specify a hard-coded format string or use -- to signal the end of options. The exact same issue also applies to read, and a similar one to mapfile, though performing expansions into their arguments is less common.

       

       

      Arguments

      Of course in shell-meaning the arguments are just strings, however, the common C-notations plus some additions for number-constants are recognized to give a number-argument to printf:

      Number-Format

      Description

      N A normal decimal number

      0N An octal number

      0xN A hexadecimal number

      "X (a literal double-quote infront of a character): interpreted as number (underlying codeset) don’t forget escaping

      ‘X (a literal single-quote infront of a character): interpreted as number (underlying codeset) don’t forget escaping

      If more arguments than format specifiers are present, then the format string is re-used until the last argument is interpreted. If fewer format specifiers than arguments are present, then number-formats are set to zero, while string-formats are set to null (empty).

      Take care to avoid word splitting, as accidentally passing the wrong number of arguments can produce wildly different and unexpected results. See this article.

       

      Again, attention: When a numerical format expects a number, the internal printf-command will use the common Bash arithmetic rules regarding the base. A command like the following example will throw an error, since 08 is not a valid octal number (00 to 07!):

      printf '%d\n' 08

      Format strings

      The format string interpretion is derived from the C printf() function family. Only format specifiers that end in one of the letters diouxXfeEgGaAcs are recognized.

      To print a literal % (percent-sign), use %% in the format string.

      Again: Every format specifier expects an associated argument provided!

      These specifiers have different names, depending who you ask. But they all mean the same: A placeholder for data with a specified format:

      format placeholder

      conversion specification

      formatting token

       

      Format

      Description

      %b Print the associated argument while interpreting backslash escapes in there

      %q Print the associated argument shell-quoted, reusable as input

      %d Print the associated argument as signed decimal number

      %i Same as %d

      %o Print the associated argument as unsigned octal number

      %u Print the associated argument as unsigned decimal number

      %x Print the associated argument as unsigned hexadecimal number with lower-case hex-digits (a-f)

      %X Same as %x, but with upper-case hex-digits (A-F)

      %f Interpret and print the associated argument as floating point number

      %e Interpret the associated argument as double, and print it in <N>±e<N> format

      %E Same as %e, but with an upper-case E in the printed format

      %g Interprets the associated argument as double, but prints it like %f or %e

      %G Same as %g, but print it like %E

      %c Interprets the associated argument as char: only the first character of a given argument is printed

      %s Interprets the associated argument literally as string

      %n Assigns the number of characters printed so far to the variable named in the corresponding argument. Can’t specify an array index. If the given name is already an array, the value is assigned to the zeroth element.

      %a Interprets the associated argument as double, and prints it in the form of a C99 hexadecimal floating-point literal.

      %A Same as %a, but print it like %E

      %(FORMAT)T output the date-time string resulting from using FORMAT as a format string for strftime(3). The associated argument is the number of seconds since Epoch, or -1 (current time) or -2 (shell startup time). If no corresponding argument is supplies, the current time is used as default

      %% No conversion is done. Produces a % (percent sign)

      Some of the mentioned format specifiers can modify their behaviour by getting a format modifier:

       

       

      Modifiers

      To be more flexible in the output of numbers and strings, the printf command allows format modifiers. These are specified between the introductory % and the character that specifies the format:

      printf "%50s\n" "This field is 50 characters wide..."

      Field and printing modifiers

      Field output format

      <N> Any number: Specifies a minimum field width, if the text to print is shorter, it’s padded with spaces, if the text is longer, the field is expanded

      . The dot: Together with a field width, the field is not expanded when the text is longer, the text is truncated instead. "%.s" is an undocumented equivalent for "%.0s", which will force a field width of zero, effectively hiding the field from output

      * The asterisk: the width is given as argument before the string or number. Usage (the "*" corresponds to the "20″): printf "%*s\n" 20 "test string"

      # "Alternative format" for numbers: see table below

      - Left-bound text printing in the field (standard is right-bound)

      0 Pads numbers with zeros, not spaces

      <space> Pad a positive number with a space, where a minus (-) is for negative numbers

      + Prints all numbers signed (+ for positive, - for negative)

      For decimal conversions, the thousands grouping separator is applied to the integer portion of the output according to the current LC_NUMERIC

      The "alternative format" modifier #:

      Alternative Format

      %#o The octal number is printed with a leading zero, unless it’s zero itself

      %#x, %#X The hex number is printed with a leading "0x"/"0X", unless it’s zero

      %#g, %#G The float number is printed with trailing zeros until the number of digits for the current precision is reached (usually trailing zeros are not printed)

      all number formats except %d, %o, %x, %X

      Always print a decimal point in the output, even if no digits follow it

       

       

      Precision

      The precision for a floating- or double-number can be specified by using .<DIGITS>, where <DIGITS> is the number of digits for precision. If <DIGITS> is an asterisk (*), the precision is read from the argument that precedes the number to print, like (prints 4,3000000000):

       

      printf "%.*f\n" 10 4,3

      The format .*N to specify the N’th argument for precision does not work in Bash.For strings, the precision specifies the maximum number of characters to print (i.e., the maximum field width). For integers, it specifies the number of digits to print (zero-padding!).

      Escape codes

      Backslashes in the escapes: \’, \", and \? are not removed.

      Octal escapes beginning with \0 may contain up to four digits. (POSIX specifies up to three).

      These are also respects in which %b differs from the escapes used by $’…’ style quoting.

      Examples

       

       

      Snipplets

      print the decimal representation of a hexadecimal number (preserve the sign)

      printf "%d\n" 0×41

      printf "%d\n" -0×41

      printf "%+d\n" 0×41

      print the octal representation of a decimal number

      printf "%o\n" 65

      printf "%05o\n" 65 (5 characters width, padded with zeros)

      this prints a 0, since no argument is specified

      printf "%d\n"

      print the code number of the character A

      printf "%d\n" \’A

      printf "%d\n" "‘A"

      Generate a greeting banner and assign it to the variable GREETER

      printf -v GREETER "Hello %s" "$LOGNAME"

      Print a text at the end of the line, using tput to get the current line width

      printf "%*s\n" $(tput cols) "Hello world!"

      Small code table

      This small loop prints all numbers from 0 to 127 in decimal | octal | hex :

       

      for ((x=0; x <= 127; x++)); do
        printf '%3d | %04o | 0x%02x\n' "$x" "$x" "$x"
      done

       

       

      Ensure well-formatted MAC address

      This code here will take a common MAC address and rewrite it into a well-known format (regarding leading zeros or upper/lowercase of the hex digits, …):

       

      the_mac="0:13:ce:7:7a:ad"
      
      # lowercase hex digits
      the_mac="$(printf "%02x:%02x:%02x:%02x:%02x:%02x" 0x${the_mac//:/ 0x})"
      
      # or the uppercase-digits variant
      the_mac="$(printf "%02X:%02X:%02X:%02X:%02X:%02X" 0x${the_mac//:/ 0x})"

      Replacement echo

      This code was found in Solaris manpage for echo(1).

      Solaris version of /usr/bin/echo is equivalent to:

       

      printf "%b\n" "$*"

      Solaris /usr/ucb/echo is equivalent to:

       

      if [ "X$1" = "X-n" ] ; then
           shift
           printf "%s" "$*"
      else
           printf "%s\n" "$*"
      fi

       

       

       

       

       

      prargs Implementation

      Working off the replacement echo, here is a terse implementation of prargs:

       

      printf '"%b"\n' "$0" "$@" | nl -v0 -s": "

      repeating a character (for example to print a line)

      A small trick: Combining printf and parameter expansion to draw a line

       

      length=40
      printf -v line '%*s' "$length"
      echo ${line// /-}

      or:

      length=40
      eval printf -v line '%.0s-' {1..$length}

      Replacement for some calls to date(1)

      The %(…)T format string is a direct interface to strftime(3).

       

      $ printf 'This is week %(%U/%Y)T.\n' -1
      This is week 52/2010.

      Please read the manpage of strftime(3) to get more information about the supported formats.

      Using printf inside of awk

      Here’s the gotcha:

       

      $ printf "%s\n" "Foo"
      Foo
      
      $ echo "Foo" | awk '{ printf "%s\n" $1 }'
      awk: (FILENAME=- FNR=1) fatal: not enough arguments to satisfy format string
      	`%s
      Foo'
      	 ^ ran out for this one

      One fix is to use commas to separate the format from the arguments:

       

      $ echo "Foo" | awk '{ printf "%s\n", $1 }'
      Foo

      Or, use printf the way that awk wants you to:

       

      $ echo "Foo" | awk '{ printf $1 "\n" }'
      Foo

      But then you lose the ability to pad numbers, set field widths, etc. that printf has.

       

      Assigning to variables: The printf -v way is slightly different to the way using command-substitution. Command substitution removes trailing newlines before substituting the text, printf -v preserves all output.

       

      Code snip: Print a horizontal line uses some printf examples

       

       

 

Aucun commentaire

 

Laissez un commentaire