Partage
  • Partager sur Facebook
  • Partager sur Twitter

sort classe bizarrement (ls -l /usr/bin | sort -nr

    3 janvier 2021 à 17:18:34

    Bonjour,

    quand je lance en bash la commande

    ls -l /usr/bin | sort -nrk 5 | head
    

    je n'obtiens pas ce que j'attends, et je ne sais pas bien pourquoi. Je m'attends à avoir le résultat de 'ls' classé par ordre numérique (-n), selon le champs N°5 (-k 5) et par ordre décroissant (-r).

    Mais le résultat est:

    thierry@localhost:~> ls -l /usr/bin | sort -nrk 5 | head
    -rwxr-xr-x 1 root root    15182904 12 mai    2020 flashplayer
    -rwxr-xr-x 1 root root     9441608 25 juin   2020 gimp-2.10
    -rwxr-xr-x 1 root root     6323384 16 mai    2020 librecad
    -rwxr-xr-x 1 root root     3625200 15 juin   2020 kleopatra
    -rwxr-xr-x 1 root root     3589528 25 juin   2020 gimp-console-2.10
    -rwxr-xr-x 1 root root     3188584 16 mai    2020 konversation
    -rwxr-xr-x 1 root root     3159680 28 oct.  14:01 zypper
    -rwxr-xr-x 1 root root     2992656 23 nov.  23:59 vim-nox11
    -rwxr-xr-x 1 root root     2496112 10 oct.  02:57 Xvnc
    -rwxr-xr-x 1 root root    22878400  2 déc.  15:13 mariabackup
    

    Pourquoi le fichier 'mariabackup' est en dernière position, alors qu'il a 22878400 octets ?

    Merci pour votre aide.

    PS: Opensuse Leap 15.2 d'origine.

    Edit:

    En cherchant j'ai observé que la date du fichier 'mariabackup' ne possède qu'un chiffre. Je relance la même commande, mais cette fois-ci avec 30 observations au lieu de 10. L'anomalie ressort, et à deux reprises cette fois-ci: pour 'mariabackup' et pour 'gdb' également.

    Pourquoi ?

    thierry@localhost:~> ls -l /usr/bin/ | sort -nrk 5 | head -n 30
    -rwxr-xr-x 1 root root    15182904 12 mai    2020 flashplayer
    -rwxr-xr-x 1 root root     9441608 25 juin   2020 gimp-2.10
    -rwxr-xr-x 1 root root     6323384 16 mai    2020 librecad
    -rwxr-xr-x 1 root root     3625200 15 juin   2020 kleopatra
    -rwxr-xr-x 1 root root     3589528 25 juin   2020 gimp-console-2.10
    -rwxr-xr-x 1 root root     3188584 16 mai    2020 konversation
    -rwxr-xr-x 1 root root     3159680 28 oct.  14:01 zypper
    -rwxr-xr-x 1 root root     2992656 23 nov.  23:59 vim-nox11
    -rwxr-xr-x 1 root root     2496112 10 oct.  02:57 Xvnc
    -rwxr-xr-x 1 root root    22878400  2 déc.  15:13 mariabackup
    -rwxr-xr-x 1 root root     2143568 16 mai    2020 busybox-static
    -rwxr-xr-x 2 root root     2099800 21 juin   2020 perl5.26.1
    -rwxr-xr-x 2 root root     2099800 21 juin   2020 perl
    -rwxr-xr-x 1 root root     1878040 16 mai    2020 omshell
    -rwxr-xr-x 1 root root     1877008 15 juin   2020 akonadi_kolab_resource
    -rwxr-xr-x 1 root root     1474936 15 juin   2020 akonadi_ews_resource
    -rwxr-xr-x 1 root root     1466048 16 mai    2020 w3m
    -rwxr-xr-x 1 root root     1465816 16 mai    2020 strace
    -rwxr-xr-x 1 root root     1309288 13 nov.  00:48 grub2-fstest
    -rwxr-xr-x 1 root root     1304552 13 nov.  00:48 grub2-editenv
    -rwxr-xr-x 1 root root     1277368 13 nov.  00:48 grub2-mkrescue
    -rwxr-xr-x 1 root root     1226688 29 oct.  23:59 rpcclient
    -rwxr-xr-x 1 root root     1218608 15 juin   2020 kolourpaint
    -rwxr-xr-x 1 root root    12165480  9 juin   2020 gdb
    -rwxr-xr-x 1 root root     1179968 23 juil. 12:49 flatpak
    -rwxr-xr-x 1 root root     1173808 24 nov.  00:21 systemd-analyze
    -rwxr-xr-x 1 root root     1166872 17 mai    2020 ktorrent
    -r-xr-xr-x 1 root root     1149232 16 mai    2020 iasl
    -rwxr-xr-x 1 root root     1126992 13 nov.  00:48 grub2-emu
    -rwxr-xr-x 1 root root     1079832 19 déc.  13:20 WebKitWebDriver
    


    -
    Edité par DenebeDenebe 3 janvier 2021 à 18:07:24

    • Partager sur Facebook
    • Partager sur Twitter
      3 janvier 2021 à 18:20:34

      salut,

      \ls -lS /usr/bin


      c'est une mauvaise pratique de parser la sortie de ls, il est préférable d'utiliser stat, parce que sa sortie est paramétrable, et donc stable.

      • Partager sur Facebook
      • Partager sur Twitter

      Validez la réponse utile « Un problème clairement exposé est à moitié résolu. » Pas de MP technique

        3 janvier 2021 à 19:35:03

        Bonjour dantonq,

        en fait je lis un ouvrage pour apprendre, et ici il s'agit de la commande 'sort'.

        Dans le livre, la sortie fournie par l'auteur est bien ordonnée, la mienne non. C'est ce que je cherche.

        Merci pour ta remarque.

        • Partager sur Facebook
        • Partager sur Twitter
          3 janvier 2021 à 19:43:00

          chez moi aussi, l'ordre est correct avec la commande indiquée.

          essaie comme ça :

          ls -l /usr/bin/ | sort -r -k5,5n

          ?

          • Partager sur Facebook
          • Partager sur Twitter

          Validez la réponse utile « Un problème clairement exposé est à moitié résolu. » Pas de MP technique

            4 janvier 2021 à 9:42:41

            dantonq a écrit:

            chez moi aussi, l'ordre est correct avec la commande indiquée.

            essaie comme ça :

            ls -l /usr/bin/ | sort -r -k5,5n

            ?


            La commande n'affiche pas ce que j'attends.

            Par contre cette commande (avec sort…) m'affiche ce que j'attendais:

            thierry@localhost:~> ls -l /usr/bin/ | sort -k5,5nr | head
            -rwxr-xr-x 1 root root    22878400  2 déc.  15:13 mariabackup
            -rwxr-xr-x 1 root root    15182904 12 mai    2020 flashplayer
            -rwxr-xr-x 1 root root    12165480  9 juin   2020 gdb
            -rwxr-xr-x 1 root root     9441608 25 juin   2020 gimp-2.10
            -rwxr-xr-x 1 root root     9112288  7 déc.  23:28 ld.bfd
            -rwxr-xr-x 1 root root     6323384 16 mai    2020 librecad
            -rwxr-xr-x 1 root root     5123096  2 déc.  15:13 mysql_ldb
            -rwxr-xr-x 1 root root     5123072  2 déc.  15:13 sst_dump
            -rwxr-xr-x 1 root root     4508080  2 déc.  15:13 aria_chk
            -rwxr-xr-x 1 root root     4466416  2 déc.  15:13 aria_read_log
            

            OK. J'en reviens à mon problème i.e. essayer de comprendre pourquoi cette commande ne m'affiche pas ce que je souhaite:

            thierry@localhost:~> ls -l /usr/bin/ | sort -nrk 5 | head
            -rwxr-xr-x 1 root root 15182904 12 mai 2020 flashplayer
            -rwxr-xr-x 1 root root 9441608 25 juin 2020 gimp-2.10
            -rwxr-xr-x 1 root root 6323384 16 mai 2020 librecad
            -rwxr-xr-x 1 root root 3625200 15 juin 2020 kleopatra
            -rwxr-xr-x 1 root root 3589528 25 juin 2020 gimp-console-2.10
            -rwxr-xr-x 1 root root 3188584 16 mai 2020 konversation
            -rwxr-xr-x 1 root root 3159680 28 oct. 14:01 zypper
            -rwxr-xr-x 1 root root 2992656 23 nov. 23:59 vim-nox11
            -rwxr-xr-x 1 root root 2496112 10 oct. 02:57 Xvnc
            -rwxr-xr-x 1 root root 22878400 2 déc. 15:13 mariabackup

            Je pense avoir une réponse "convenable", qui permet de mieux comprendre ce qui se passe (chez moi au minimum).

            J'ai simplement testé…

            Par contre, j'ai du mal à synthétiser le procédé en quelques phrases.

            À vous d'observer la sortie, je pense qu'elle parle d'elle même:

            thierry@localhost:~> cat tri_sort.txt      
            10 0
            10  0
            10    0
            10   
            10   0
            10 00
            10  00
            100
            11 0
            11
            1 1
            1  01
            1 00
            1  1
            1  0
            1  00
            1 1 0
            0 0 1
            1   0  1
            1  0 1
            1 0  1
            00 1
            0  1
            0 0 0
            0 01
            01
            100 0
            
            thierry@localhost:~> sort -nr tri_sort.txt 
            100 0
            10  00
            10 00
            11 0
            1 1 0
            1 0  1
            1  0 1
            1   0  1
            1  01
            10    0
            10   0
            10  0
            10 0
            1  00
            1 00
            100
            1  1
            1 1
            11
            10   
            1  0
            0  1
            01
            00 1
            0 0 1
            0 01
            0 0 0
            


            -
            Edité par DenebeDenebe 4 janvier 2021 à 9:49:56

            • Partager sur Facebook
            • Partager sur Twitter
              4 janvier 2021 à 9:59:52

              Bonjour,

              Je n'arrive pas à reproduire, ça fonctionne comme attendu:

              $ sort -nr sort.txt 
              100 0
              100
              11 0
              11
              10 00
              10  00
              10 0
              10  0
              10   0
              10    0
              10  
              1 1 0
              1 1
              1  1
              1 0  1
              1  01
              1  0 1
              1   0  1
              1 00
              1  00
              1  0
              01
              0  1
              00 1
              0 01
              0 0 1
              0 0 0
              

              Environnement différent?

              env

              Un alias?

              En utilisant un autre shell (comme sh ou zsh), on retrouve le problème?

              -
              Edité par KoaTao 4 janvier 2021 à 10:03:25

              • Partager sur Facebook
              • Partager sur Twitter
                4 janvier 2021 à 11:54:17

                Non je n'ai pas d'alias "exotique" hormis 'alias sz='sudo zypper up --details' -> pour réaliser mes mises à jours avec Opensuse… Les autres alias sont normaux genre cd .. à la place de cd..

                Je pense que cela a avoir avec l'environnement comme tu le suggères.

                La commande

                LANG=C sort -nr tri_sort.txt 

                trie le fichier comme chez toi. Comme je l'attendais…

                tout comme

                LANG=POSIX sort -nr tri_sort.txt 

                Par contre, je remarque que les espaces et les chiffres après le champs principal, celui à considérer principalement pour le tri, jouent eux aussi un rôle dans le tri.

                ----------------------------------------------------------

                J'en reviens à mon exemple du premier post, celui traitant de ls -l /usr/bin/.

                Cela fonctionne aussi avec LANG=C, car le 6 ième champs est composé maintenant de lettres, et non de chiffres (ceux du jours en question: 1-31)

                thierry@localhost:~> LANG=C ls -l /usr/bin/ | sort -nrk 5 | head
                -rwxr-xr-x 1 root root    22878400 Dec  2 15:13 mariabackup
                -rwxr-xr-x 1 root root    15182904 May 12  2020 flashplayer
                -rwxr-xr-x 1 root root    12165480 Jun  9  2020 gdb
                -rwxr-xr-x 1 root root     9441608 Jun 25  2020 gimp-2.10
                -rwxr-xr-x 1 root root     9112288 Dec  7 23:28 ld.bfd
                -rwxr-xr-x 1 root root     6323384 May 16  2020 librecad
                -rwxr-xr-x 1 root root     5123096 Dec  2 15:13 mysql_ldb
                -rwxr-xr-x 1 root root     5123072 Dec  2 15:13 sst_dump
                -rwxr-xr-x 1 root root     4508080 Dec  2 15:13 aria_chk
                -rwxr-xr-x 1 root root     4466416 Dec  2 15:13 aria_read_log
                



                Si je peux me permettre, que donne chez toi la commande:

                locale

                Chez moi:

                thierry@localhost:~> locale
                LANG=fr_FR.UTF-8
                LC_CTYPE="fr_FR.UTF-8"
                LC_NUMERIC="fr_FR.UTF-8"
                LC_TIME="fr_FR.UTF-8"
                LC_COLLATE="fr_FR.UTF-8"
                LC_MONETARY="fr_FR.UTF-8"
                LC_MESSAGES="fr_FR.UTF-8"
                LC_PAPER="fr_FR.UTF-8"
                LC_NAME="fr_FR.UTF-8"
                LC_ADDRESS="fr_FR.UTF-8"
                LC_TELEPHONE="fr_FR.UTF-8"
                LC_MEASUREMENT="fr_FR.UTF-8"
                LC_IDENTIFICATION="fr_FR.UTF-8"
                LC_ALL=
                




                -
                Edité par DenebeDenebe 4 janvier 2021 à 12:09:20

                • Partager sur Facebook
                • Partager sur Twitter
                  4 janvier 2021 à 20:54:20

                  Tu peux te permettre :D

                  $ locale
                  LANG=en_US.UTF-8
                  LC_CTYPE="en_US.UTF-8"
                  LC_NUMERIC="en_US.UTF-8"
                  LC_TIME="en_US.UTF-8"
                  LC_COLLATE="en_US.UTF-8"
                  LC_MONETARY="en_US.UTF-8"
                  LC_MESSAGES="en_US.UTF-8"
                  LC_PAPER="en_US.UTF-8"
                  LC_NAME="en_US.UTF-8"
                  LC_ADDRESS="en_US.UTF-8"
                  LC_TELEPHONE="en_US.UTF-8"
                  LC_MEASUREMENT="en_US.UTF-8"
                  LC_IDENTIFICATION="en_US.UTF-8"
                  LC_ALL=

                  Je n'arrive pas à reproduire sur ArchLinux en utilisant fr_FR.UTF-8:

                  $ uname -r
                  5.10.4-arch2-1
                  $ sort --version
                  sort (GNU coreutils) 8.32

                  Intéressant quand même, il semble que les espaces soient évalués différemment selon la locale (qui dépendent aussi de la distrib je suppose).

                  dantonq a écrit:

                  essaie comme ça :

                  ls -l /usr/bin/ | sort -r -k5,5n

                  Probablement la meilleure solution.

                  • Partager sur Facebook
                  • Partager sur Twitter
                    5 janvier 2021 à 13:39:57

                    KoaTao a écrit:

                    dantonq a écrit:

                    essaie comme ça :

                    ls -l /usr/bin/ | sort -r -k5,5n

                    Probablement la meilleure solution.


                    J'ai déjà essayé (voir mon message de 9h42), ça donne un résultat inattendu (je ne dirais pas faux, car il doit bien y avoir une explication…).

                    Mais

                    ls -l /usr/bin/ | sort -k5,5nr | head

                    fonctionne.

                    CORRECTION des deux points ci-dessous: CA NE MARCHE PAS avec Leap 15.2, peu importe l'ordinateur

                    Hier soir, j'ai essayé sur un autre ordinateur (B) avec deux autres distributions:

                    • Leap 15.2 sur ordi (B) -> ça marche (alors qu'ici Leap 15.2 (A) -> le résultat est inattendu, pas celui que je veux)
                    • Tumbleweed (B) -> ça marche…

                    Merci pour votre aide.

                    Je crois que cela va rester un mystère…

                    -
                    Edité par DenebeDenebe 6 janvier 2021 à 9:44:02

                    • Partager sur Facebook
                    • Partager sur Twitter

                    sort classe bizarrement (ls -l /usr/bin | sort -nr

                    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
                    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
                    • Editeur
                    • Markdown