Sort through numbers

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











up vote
0
down vote

favorite












I need to sort a list by the ISBN NUMBER (the third column) from an input file to a file.sh and sent to an out file (file.out).
The input file (file.input) would have a list



Donald Smith,Fire Lands,97868545414459
Adam Barry,The Armies,97564325678855
Jennifer Lelan,Childhood dreams,97546766544237


using a looping structure to process the data and the titles Author Name of book ISBN.



The result



Author Name of Book ISBN

Jennifer Lelan Chilhood Dreams 97546766544237
Adam Barry The Armies 97564325678855
Donald Smith Fire Lands 97868545414459






share|improve this question

























    up vote
    0
    down vote

    favorite












    I need to sort a list by the ISBN NUMBER (the third column) from an input file to a file.sh and sent to an out file (file.out).
    The input file (file.input) would have a list



    Donald Smith,Fire Lands,97868545414459
    Adam Barry,The Armies,97564325678855
    Jennifer Lelan,Childhood dreams,97546766544237


    using a looping structure to process the data and the titles Author Name of book ISBN.



    The result



    Author Name of Book ISBN

    Jennifer Lelan Chilhood Dreams 97546766544237
    Adam Barry The Armies 97564325678855
    Donald Smith Fire Lands 97868545414459






    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I need to sort a list by the ISBN NUMBER (the third column) from an input file to a file.sh and sent to an out file (file.out).
      The input file (file.input) would have a list



      Donald Smith,Fire Lands,97868545414459
      Adam Barry,The Armies,97564325678855
      Jennifer Lelan,Childhood dreams,97546766544237


      using a looping structure to process the data and the titles Author Name of book ISBN.



      The result



      Author Name of Book ISBN

      Jennifer Lelan Chilhood Dreams 97546766544237
      Adam Barry The Armies 97564325678855
      Donald Smith Fire Lands 97868545414459






      share|improve this question













      I need to sort a list by the ISBN NUMBER (the third column) from an input file to a file.sh and sent to an out file (file.out).
      The input file (file.input) would have a list



      Donald Smith,Fire Lands,97868545414459
      Adam Barry,The Armies,97564325678855
      Jennifer Lelan,Childhood dreams,97546766544237


      using a looping structure to process the data and the titles Author Name of book ISBN.



      The result



      Author Name of Book ISBN

      Jennifer Lelan Chilhood Dreams 97546766544237
      Adam Barry The Armies 97564325678855
      Donald Smith Fire Lands 97868545414459








      share|improve this question












      share|improve this question




      share|improve this question








      edited Jun 13 at 18:15









      Kusalananda

      101k13199312




      101k13199312









      asked Jun 13 at 15:45









      Jonny kebell

      1




      1




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          5
          down vote













          First of all, you would not loop over this data: Why is using a shell loop to process text considered bad practice?



          If the only commas in the file are the commas that delimit the fields, then



          sort -t ',' -k3n -o file.output file.input


          would sort the data numerically on the number in the third column. The output would be written to file.output.



          For the given data, file.output would look like



          Jennifer Lelan,Childhood dreams,97546766544237
          Adam Barry,The Armies,97564325678855
          Donald Smith,Fire Lands,97868545414459


          To further process this data, one could consider using an awk program. Since you have not specified what kind of processing you'd like to do, the following just extracts the data into variables (not really necessary) for each line and prints them:



          sort -t ',' -k3n file.input |
          awk -F ',' ' author=$1; title=$2; isbn=$3;
          printf("Author: %snTitle: %snISBN: %sn",
          author, title, isbn) '


          Note that there is no need to store the sorted data in an intermediate file in this case.



          The output given the data in the question:



          Author: Jennifer Lelan
          Title: Childhood dreams
          ISBN: 97546766544237
          Author: Adam Barry
          Title: The Armies
          ISBN: 97564325678855
          Author: Donald Smith
          Title: Fire Lands
          ISBN: 97868545414459


          For getting the data into nice looking columns, and with dashes in the ISBN number, you don't need awk. The following uses sed for the formatting of the ISBN numbers and column to format the columns:



          sort -t ',' -k3n file.input |
          sed -E -e 's/,([0-9]3)([0-9]4)([0-9]5)/,1-2-3-/' |
          column -s ',' -t


          The output will be



          Jennifer Lelan Childhood dreams 975-4676-65442-37
          Adam Barry The Armies 975-6432-56788-55
          Donald Smith Fire Lands 978-6854-54144-59


          Note that the ISBN numbers look a bit wonky. That's because they are 14 digits long. Real ISBN numbers are either 10 or 13 digits long, and the above code assumes that they are 13 digits (or at least 12 digits).



          To add columns headers:



          sort -t ',' -k3n file.input |
          echo 'Author,Name of book,ISBN'
          sed -E -e 's/,([0-9]3)([0-9]4)([0-9]5)/,1-2-3-/'
          |
          column -s ',' -t


          Which produces



          Author Name of book ISBN
          Jennifer Lelan Childhood dreams 975-4676-65442-37
          Adam Barry The Armies 975-6432-56788-55
          Donald Smith Fire Lands 978-6854-54144-59


          ... using no explicit loops in the shell.






          share|improve this answer






























            up vote
            4
            down vote













            sort is clearly the best tool for sorting.



            If awk is required, you can use GNU awk:



            gawk -F, '
            line[$NF] = $0
            END
            PROCINFO["sorted_in"] = "@ind_num_asc"
            for (isbn in line) print line[isbn]

            ' file


            See https://www.gnu.org/software/gawk/manual/html_node/Controlling-Array-Traversal.html and https://www.gnu.org/software/gawk/manual/html_node/Controlling-Scanning.html






            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%2f449583%2fsort-through-numbers%23new-answer', 'question_page');

              );

              Post as a guest






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              5
              down vote













              First of all, you would not loop over this data: Why is using a shell loop to process text considered bad practice?



              If the only commas in the file are the commas that delimit the fields, then



              sort -t ',' -k3n -o file.output file.input


              would sort the data numerically on the number in the third column. The output would be written to file.output.



              For the given data, file.output would look like



              Jennifer Lelan,Childhood dreams,97546766544237
              Adam Barry,The Armies,97564325678855
              Donald Smith,Fire Lands,97868545414459


              To further process this data, one could consider using an awk program. Since you have not specified what kind of processing you'd like to do, the following just extracts the data into variables (not really necessary) for each line and prints them:



              sort -t ',' -k3n file.input |
              awk -F ',' ' author=$1; title=$2; isbn=$3;
              printf("Author: %snTitle: %snISBN: %sn",
              author, title, isbn) '


              Note that there is no need to store the sorted data in an intermediate file in this case.



              The output given the data in the question:



              Author: Jennifer Lelan
              Title: Childhood dreams
              ISBN: 97546766544237
              Author: Adam Barry
              Title: The Armies
              ISBN: 97564325678855
              Author: Donald Smith
              Title: Fire Lands
              ISBN: 97868545414459


              For getting the data into nice looking columns, and with dashes in the ISBN number, you don't need awk. The following uses sed for the formatting of the ISBN numbers and column to format the columns:



              sort -t ',' -k3n file.input |
              sed -E -e 's/,([0-9]3)([0-9]4)([0-9]5)/,1-2-3-/' |
              column -s ',' -t


              The output will be



              Jennifer Lelan Childhood dreams 975-4676-65442-37
              Adam Barry The Armies 975-6432-56788-55
              Donald Smith Fire Lands 978-6854-54144-59


              Note that the ISBN numbers look a bit wonky. That's because they are 14 digits long. Real ISBN numbers are either 10 or 13 digits long, and the above code assumes that they are 13 digits (or at least 12 digits).



              To add columns headers:



              sort -t ',' -k3n file.input |
              echo 'Author,Name of book,ISBN'
              sed -E -e 's/,([0-9]3)([0-9]4)([0-9]5)/,1-2-3-/'
              |
              column -s ',' -t


              Which produces



              Author Name of book ISBN
              Jennifer Lelan Childhood dreams 975-4676-65442-37
              Adam Barry The Armies 975-6432-56788-55
              Donald Smith Fire Lands 978-6854-54144-59


              ... using no explicit loops in the shell.






              share|improve this answer



























                up vote
                5
                down vote













                First of all, you would not loop over this data: Why is using a shell loop to process text considered bad practice?



                If the only commas in the file are the commas that delimit the fields, then



                sort -t ',' -k3n -o file.output file.input


                would sort the data numerically on the number in the third column. The output would be written to file.output.



                For the given data, file.output would look like



                Jennifer Lelan,Childhood dreams,97546766544237
                Adam Barry,The Armies,97564325678855
                Donald Smith,Fire Lands,97868545414459


                To further process this data, one could consider using an awk program. Since you have not specified what kind of processing you'd like to do, the following just extracts the data into variables (not really necessary) for each line and prints them:



                sort -t ',' -k3n file.input |
                awk -F ',' ' author=$1; title=$2; isbn=$3;
                printf("Author: %snTitle: %snISBN: %sn",
                author, title, isbn) '


                Note that there is no need to store the sorted data in an intermediate file in this case.



                The output given the data in the question:



                Author: Jennifer Lelan
                Title: Childhood dreams
                ISBN: 97546766544237
                Author: Adam Barry
                Title: The Armies
                ISBN: 97564325678855
                Author: Donald Smith
                Title: Fire Lands
                ISBN: 97868545414459


                For getting the data into nice looking columns, and with dashes in the ISBN number, you don't need awk. The following uses sed for the formatting of the ISBN numbers and column to format the columns:



                sort -t ',' -k3n file.input |
                sed -E -e 's/,([0-9]3)([0-9]4)([0-9]5)/,1-2-3-/' |
                column -s ',' -t


                The output will be



                Jennifer Lelan Childhood dreams 975-4676-65442-37
                Adam Barry The Armies 975-6432-56788-55
                Donald Smith Fire Lands 978-6854-54144-59


                Note that the ISBN numbers look a bit wonky. That's because they are 14 digits long. Real ISBN numbers are either 10 or 13 digits long, and the above code assumes that they are 13 digits (or at least 12 digits).



                To add columns headers:



                sort -t ',' -k3n file.input |
                echo 'Author,Name of book,ISBN'
                sed -E -e 's/,([0-9]3)([0-9]4)([0-9]5)/,1-2-3-/'
                |
                column -s ',' -t


                Which produces



                Author Name of book ISBN
                Jennifer Lelan Childhood dreams 975-4676-65442-37
                Adam Barry The Armies 975-6432-56788-55
                Donald Smith Fire Lands 978-6854-54144-59


                ... using no explicit loops in the shell.






                share|improve this answer

























                  up vote
                  5
                  down vote










                  up vote
                  5
                  down vote









                  First of all, you would not loop over this data: Why is using a shell loop to process text considered bad practice?



                  If the only commas in the file are the commas that delimit the fields, then



                  sort -t ',' -k3n -o file.output file.input


                  would sort the data numerically on the number in the third column. The output would be written to file.output.



                  For the given data, file.output would look like



                  Jennifer Lelan,Childhood dreams,97546766544237
                  Adam Barry,The Armies,97564325678855
                  Donald Smith,Fire Lands,97868545414459


                  To further process this data, one could consider using an awk program. Since you have not specified what kind of processing you'd like to do, the following just extracts the data into variables (not really necessary) for each line and prints them:



                  sort -t ',' -k3n file.input |
                  awk -F ',' ' author=$1; title=$2; isbn=$3;
                  printf("Author: %snTitle: %snISBN: %sn",
                  author, title, isbn) '


                  Note that there is no need to store the sorted data in an intermediate file in this case.



                  The output given the data in the question:



                  Author: Jennifer Lelan
                  Title: Childhood dreams
                  ISBN: 97546766544237
                  Author: Adam Barry
                  Title: The Armies
                  ISBN: 97564325678855
                  Author: Donald Smith
                  Title: Fire Lands
                  ISBN: 97868545414459


                  For getting the data into nice looking columns, and with dashes in the ISBN number, you don't need awk. The following uses sed for the formatting of the ISBN numbers and column to format the columns:



                  sort -t ',' -k3n file.input |
                  sed -E -e 's/,([0-9]3)([0-9]4)([0-9]5)/,1-2-3-/' |
                  column -s ',' -t


                  The output will be



                  Jennifer Lelan Childhood dreams 975-4676-65442-37
                  Adam Barry The Armies 975-6432-56788-55
                  Donald Smith Fire Lands 978-6854-54144-59


                  Note that the ISBN numbers look a bit wonky. That's because they are 14 digits long. Real ISBN numbers are either 10 or 13 digits long, and the above code assumes that they are 13 digits (or at least 12 digits).



                  To add columns headers:



                  sort -t ',' -k3n file.input |
                  echo 'Author,Name of book,ISBN'
                  sed -E -e 's/,([0-9]3)([0-9]4)([0-9]5)/,1-2-3-/'
                  |
                  column -s ',' -t


                  Which produces



                  Author Name of book ISBN
                  Jennifer Lelan Childhood dreams 975-4676-65442-37
                  Adam Barry The Armies 975-6432-56788-55
                  Donald Smith Fire Lands 978-6854-54144-59


                  ... using no explicit loops in the shell.






                  share|improve this answer















                  First of all, you would not loop over this data: Why is using a shell loop to process text considered bad practice?



                  If the only commas in the file are the commas that delimit the fields, then



                  sort -t ',' -k3n -o file.output file.input


                  would sort the data numerically on the number in the third column. The output would be written to file.output.



                  For the given data, file.output would look like



                  Jennifer Lelan,Childhood dreams,97546766544237
                  Adam Barry,The Armies,97564325678855
                  Donald Smith,Fire Lands,97868545414459


                  To further process this data, one could consider using an awk program. Since you have not specified what kind of processing you'd like to do, the following just extracts the data into variables (not really necessary) for each line and prints them:



                  sort -t ',' -k3n file.input |
                  awk -F ',' ' author=$1; title=$2; isbn=$3;
                  printf("Author: %snTitle: %snISBN: %sn",
                  author, title, isbn) '


                  Note that there is no need to store the sorted data in an intermediate file in this case.



                  The output given the data in the question:



                  Author: Jennifer Lelan
                  Title: Childhood dreams
                  ISBN: 97546766544237
                  Author: Adam Barry
                  Title: The Armies
                  ISBN: 97564325678855
                  Author: Donald Smith
                  Title: Fire Lands
                  ISBN: 97868545414459


                  For getting the data into nice looking columns, and with dashes in the ISBN number, you don't need awk. The following uses sed for the formatting of the ISBN numbers and column to format the columns:



                  sort -t ',' -k3n file.input |
                  sed -E -e 's/,([0-9]3)([0-9]4)([0-9]5)/,1-2-3-/' |
                  column -s ',' -t


                  The output will be



                  Jennifer Lelan Childhood dreams 975-4676-65442-37
                  Adam Barry The Armies 975-6432-56788-55
                  Donald Smith Fire Lands 978-6854-54144-59


                  Note that the ISBN numbers look a bit wonky. That's because they are 14 digits long. Real ISBN numbers are either 10 or 13 digits long, and the above code assumes that they are 13 digits (or at least 12 digits).



                  To add columns headers:



                  sort -t ',' -k3n file.input |
                  echo 'Author,Name of book,ISBN'
                  sed -E -e 's/,([0-9]3)([0-9]4)([0-9]5)/,1-2-3-/'
                  |
                  column -s ',' -t


                  Which produces



                  Author Name of book ISBN
                  Jennifer Lelan Childhood dreams 975-4676-65442-37
                  Adam Barry The Armies 975-6432-56788-55
                  Donald Smith Fire Lands 978-6854-54144-59


                  ... using no explicit loops in the shell.







                  share|improve this answer















                  share|improve this answer



                  share|improve this answer








                  edited Jun 13 at 18:17


























                  answered Jun 13 at 15:50









                  Kusalananda

                  101k13199312




                  101k13199312






















                      up vote
                      4
                      down vote













                      sort is clearly the best tool for sorting.



                      If awk is required, you can use GNU awk:



                      gawk -F, '
                      line[$NF] = $0
                      END
                      PROCINFO["sorted_in"] = "@ind_num_asc"
                      for (isbn in line) print line[isbn]

                      ' file


                      See https://www.gnu.org/software/gawk/manual/html_node/Controlling-Array-Traversal.html and https://www.gnu.org/software/gawk/manual/html_node/Controlling-Scanning.html






                      share|improve this answer

























                        up vote
                        4
                        down vote













                        sort is clearly the best tool for sorting.



                        If awk is required, you can use GNU awk:



                        gawk -F, '
                        line[$NF] = $0
                        END
                        PROCINFO["sorted_in"] = "@ind_num_asc"
                        for (isbn in line) print line[isbn]

                        ' file


                        See https://www.gnu.org/software/gawk/manual/html_node/Controlling-Array-Traversal.html and https://www.gnu.org/software/gawk/manual/html_node/Controlling-Scanning.html






                        share|improve this answer























                          up vote
                          4
                          down vote










                          up vote
                          4
                          down vote









                          sort is clearly the best tool for sorting.



                          If awk is required, you can use GNU awk:



                          gawk -F, '
                          line[$NF] = $0
                          END
                          PROCINFO["sorted_in"] = "@ind_num_asc"
                          for (isbn in line) print line[isbn]

                          ' file


                          See https://www.gnu.org/software/gawk/manual/html_node/Controlling-Array-Traversal.html and https://www.gnu.org/software/gawk/manual/html_node/Controlling-Scanning.html






                          share|improve this answer













                          sort is clearly the best tool for sorting.



                          If awk is required, you can use GNU awk:



                          gawk -F, '
                          line[$NF] = $0
                          END
                          PROCINFO["sorted_in"] = "@ind_num_asc"
                          for (isbn in line) print line[isbn]

                          ' file


                          See https://www.gnu.org/software/gawk/manual/html_node/Controlling-Array-Traversal.html and https://www.gnu.org/software/gawk/manual/html_node/Controlling-Scanning.html







                          share|improve this answer













                          share|improve this answer



                          share|improve this answer











                          answered Jun 13 at 15:58









                          glenn jackman

                          45.6k265100




                          45.6k265100






















                               

                              draft saved


                              draft discarded


























                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f449583%2fsort-through-numbers%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              How to check contact read email or not when send email to Individual?

                              Bahrain

                              Postfix configuration issue with fips on centos 7; mailgun relay