• # un pipe de six commandes shell et s'est plié

    Posté par . En réponse au journal Analyse de texte. Évalué à 6. Dernière modification le 15 décembre 2015 à 13:44.

    Dans le livre Classic Schell Scripting de chez O'reilly [»], il y a un exemple d'analyse fréquentielle des mots d'un texte pour montrer la puissance des pipes. En six commandes et c'est plié.

    #! /bin/sh
    # Read a text stream on standard input, and output a list of
    # the n (default: 25) most frequently occurring words and
    # their frequency counts, in order of descending counts, on
    # standard output.
    #
    # Usage:
    # wf [n]
    tr -cs A-Za-z\' '\n' | # Replace nonletters with newlines
     tr A-Z a-z | # Map uppercase to lowercase
     sort | # Sort the words in ascending order
     uniq -c | # Eliminate duplicates, showing their counts
     sort -k1,1nr -k2 | # Sort by descending count, and then by ascending word
     sed ${1:-25}q # Print only the first n (default: 25) lines; see Chapter 3