• [^] # Re: pour traiter des colonnes, awk est mieux adapté (et est plus lisible au passage)

    Posté par . En réponse au message SED ^^. Évalué à 2. Dernière modification le 02 mai 2018 à 16:38.

    Le premier paramètre de la fonction sub est une expression régulière. Donc le point est interprété comme étant un caractère quelconque. La substitution se fera donc sur le premier caractère de ton champ. Exemple :

    $ echo "ref1; designation1,abc ; 42,32
    ref2;designation2,def ; 33,25" \| awk '{sub(".","!",4ドル); print}'
    ref1; designation1,abc ; !2,32
    ref2; designation2,def ; !3,25

    Pour pouvoir utiliser le point, il faut le protéger avec un double backslash:

    $ echo "ref1; designation1,abc ; 42,32
    ref2; designation2,def ; 33,25" | awk '{sub("\\.","!",4ドル); print}'
    ref1; designation1,abc ; 42,32
    ref2; designation2,def ; 33,25

    Voici ce que retourne le man de awk sous freebsd :

     sub(r, t, s) Substitutes t for the first occurrence of the regular
     expression r in the string s. If s is not given, 0ドル is
     used. An ampersand (&) in t is replaced in string s
     with regular expression r. A literal ampersand can be
     specified by preceding it with two backslashes (\\).
     A literal backslash can be specified by preceding it
     with another backslash (\\). sub() returns the number
     of replacements.