List files sorted according to specific line of contents

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite
1












I have a directory of files. There is a line in each file that says:



# order: N


where N is an integer number. I want to list all files in the directory (or even include them in wrapper script) according to that N number. Is this possible from a bash command-line?







share|improve this question


























    up vote
    2
    down vote

    favorite
    1












    I have a directory of files. There is a line in each file that says:



    # order: N


    where N is an integer number. I want to list all files in the directory (or even include them in wrapper script) according to that N number. Is this possible from a bash command-line?







    share|improve this question
























      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





      I have a directory of files. There is a line in each file that says:



      # order: N


      where N is an integer number. I want to list all files in the directory (or even include them in wrapper script) according to that N number. Is this possible from a bash command-line?







      share|improve this question














      I have a directory of files. There is a line in each file that says:



      # order: N


      where N is an integer number. I want to list all files in the directory (or even include them in wrapper script) according to that N number. Is this possible from a bash command-line?









      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 29 '17 at 17:22









      Jeff Schaller

      32.1k849109




      32.1k849109










      asked Nov 29 '17 at 16:18









      user1371264

      1365




      1365




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          With GNU grep, and assuming file names don't contain colon or newline characters:



          $ ls
          bar baz foo freeble quux
          $ cat ./*
          # order: 3
          # order: 2
          # order: 1
          # order: 4
          # order: 5
          $ grep -m1 -EH '^# order: [0-9]+$' ./* | sort -n -k3 | cut -d: -f1
          foo
          baz
          bar
          freeble
          quux





          share|improve this answer






















          • Better use grep -H, otherwise it won't work if there is only one file.
            – xenoid
            Nov 29 '17 at 16:41











          • Good catch. Done.
            – DopeGhoti
            Nov 29 '17 at 16:42

















          up vote
          2
          down vote













          With single GNU awk process:



          awk 'BEGIN PROCINFO["sorted_in"]="@val_num_asc" 
          /order: [0-9]+/ a[FILENAME]=$NF; nextfile
          END for(i in a) print i ' ./*





          share|improve this answer





























            up vote
            2
            down vote













            With zsh, you can define a glob sorting order based on the content of those lines with:



            byOrder() REPLY=$(grep '^# order:' < $REPLY)


            and then use it for example with:



            printf '%sn' *(.no+byOrder)


            or



            sorted_file_list=(*(.no+byOrder))


            (also adding a . to the glob qualifier to only consider regular files (not directories, fifos, symlinks...)).






            share|improve this answer






















              Your Answer







              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "106"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f407770%2flist-files-sorted-according-to-specific-line-of-contents%23new-answer', 'question_page');

              );

              Post as a guest






























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              4
              down vote



              accepted










              With GNU grep, and assuming file names don't contain colon or newline characters:



              $ ls
              bar baz foo freeble quux
              $ cat ./*
              # order: 3
              # order: 2
              # order: 1
              # order: 4
              # order: 5
              $ grep -m1 -EH '^# order: [0-9]+$' ./* | sort -n -k3 | cut -d: -f1
              foo
              baz
              bar
              freeble
              quux





              share|improve this answer






















              • Better use grep -H, otherwise it won't work if there is only one file.
                – xenoid
                Nov 29 '17 at 16:41











              • Good catch. Done.
                – DopeGhoti
                Nov 29 '17 at 16:42














              up vote
              4
              down vote



              accepted










              With GNU grep, and assuming file names don't contain colon or newline characters:



              $ ls
              bar baz foo freeble quux
              $ cat ./*
              # order: 3
              # order: 2
              # order: 1
              # order: 4
              # order: 5
              $ grep -m1 -EH '^# order: [0-9]+$' ./* | sort -n -k3 | cut -d: -f1
              foo
              baz
              bar
              freeble
              quux





              share|improve this answer






















              • Better use grep -H, otherwise it won't work if there is only one file.
                – xenoid
                Nov 29 '17 at 16:41











              • Good catch. Done.
                – DopeGhoti
                Nov 29 '17 at 16:42












              up vote
              4
              down vote



              accepted







              up vote
              4
              down vote



              accepted






              With GNU grep, and assuming file names don't contain colon or newline characters:



              $ ls
              bar baz foo freeble quux
              $ cat ./*
              # order: 3
              # order: 2
              # order: 1
              # order: 4
              # order: 5
              $ grep -m1 -EH '^# order: [0-9]+$' ./* | sort -n -k3 | cut -d: -f1
              foo
              baz
              bar
              freeble
              quux





              share|improve this answer














              With GNU grep, and assuming file names don't contain colon or newline characters:



              $ ls
              bar baz foo freeble quux
              $ cat ./*
              # order: 3
              # order: 2
              # order: 1
              # order: 4
              # order: 5
              $ grep -m1 -EH '^# order: [0-9]+$' ./* | sort -n -k3 | cut -d: -f1
              foo
              baz
              bar
              freeble
              quux






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 29 '17 at 16:58









              Stéphane Chazelas

              282k53520854




              282k53520854










              answered Nov 29 '17 at 16:27









              DopeGhoti

              40.6k54979




              40.6k54979











              • Better use grep -H, otherwise it won't work if there is only one file.
                – xenoid
                Nov 29 '17 at 16:41











              • Good catch. Done.
                – DopeGhoti
                Nov 29 '17 at 16:42
















              • Better use grep -H, otherwise it won't work if there is only one file.
                – xenoid
                Nov 29 '17 at 16:41











              • Good catch. Done.
                – DopeGhoti
                Nov 29 '17 at 16:42















              Better use grep -H, otherwise it won't work if there is only one file.
              – xenoid
              Nov 29 '17 at 16:41





              Better use grep -H, otherwise it won't work if there is only one file.
              – xenoid
              Nov 29 '17 at 16:41













              Good catch. Done.
              – DopeGhoti
              Nov 29 '17 at 16:42




              Good catch. Done.
              – DopeGhoti
              Nov 29 '17 at 16:42












              up vote
              2
              down vote













              With single GNU awk process:



              awk 'BEGIN PROCINFO["sorted_in"]="@val_num_asc" 
              /order: [0-9]+/ a[FILENAME]=$NF; nextfile
              END for(i in a) print i ' ./*





              share|improve this answer


























                up vote
                2
                down vote













                With single GNU awk process:



                awk 'BEGIN PROCINFO["sorted_in"]="@val_num_asc" 
                /order: [0-9]+/ a[FILENAME]=$NF; nextfile
                END for(i in a) print i ' ./*





                share|improve this answer
























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  With single GNU awk process:



                  awk 'BEGIN PROCINFO["sorted_in"]="@val_num_asc" 
                  /order: [0-9]+/ a[FILENAME]=$NF; nextfile
                  END for(i in a) print i ' ./*





                  share|improve this answer














                  With single GNU awk process:



                  awk 'BEGIN PROCINFO["sorted_in"]="@val_num_asc" 
                  /order: [0-9]+/ a[FILENAME]=$NF; nextfile
                  END for(i in a) print i ' ./*






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 29 '17 at 16:53









                  Stéphane Chazelas

                  282k53520854




                  282k53520854










                  answered Nov 29 '17 at 16:43









                  RomanPerekhrest

                  22.4k12145




                  22.4k12145




















                      up vote
                      2
                      down vote













                      With zsh, you can define a glob sorting order based on the content of those lines with:



                      byOrder() REPLY=$(grep '^# order:' < $REPLY)


                      and then use it for example with:



                      printf '%sn' *(.no+byOrder)


                      or



                      sorted_file_list=(*(.no+byOrder))


                      (also adding a . to the glob qualifier to only consider regular files (not directories, fifos, symlinks...)).






                      share|improve this answer


























                        up vote
                        2
                        down vote













                        With zsh, you can define a glob sorting order based on the content of those lines with:



                        byOrder() REPLY=$(grep '^# order:' < $REPLY)


                        and then use it for example with:



                        printf '%sn' *(.no+byOrder)


                        or



                        sorted_file_list=(*(.no+byOrder))


                        (also adding a . to the glob qualifier to only consider regular files (not directories, fifos, symlinks...)).






                        share|improve this answer
























                          up vote
                          2
                          down vote










                          up vote
                          2
                          down vote









                          With zsh, you can define a glob sorting order based on the content of those lines with:



                          byOrder() REPLY=$(grep '^# order:' < $REPLY)


                          and then use it for example with:



                          printf '%sn' *(.no+byOrder)


                          or



                          sorted_file_list=(*(.no+byOrder))


                          (also adding a . to the glob qualifier to only consider regular files (not directories, fifos, symlinks...)).






                          share|improve this answer














                          With zsh, you can define a glob sorting order based on the content of those lines with:



                          byOrder() REPLY=$(grep '^# order:' < $REPLY)


                          and then use it for example with:



                          printf '%sn' *(.no+byOrder)


                          or



                          sorted_file_list=(*(.no+byOrder))


                          (also adding a . to the glob qualifier to only consider regular files (not directories, fifos, symlinks...)).







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 29 '17 at 17:01

























                          answered Nov 29 '17 at 16:49









                          Stéphane Chazelas

                          282k53520854




                          282k53520854



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f407770%2flist-files-sorted-according-to-specific-line-of-contents%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              Peggy Mitchell

                              Palaiologos

                              The Forum (Inglewood, California)