Sort File with word

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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I have a text file that looks something like this



Name1 OpenFin
Name2 Chrome
Name3 OpenFin
Name4 Chrome
Name5 OpenFin
Name6 OpenFin


I want to sort this is a way where all lines with OpenFin appears first followed by all lines with Chrome



This is what I have so far:



grep OpenFin LoginExcInternal.txt | grep Chrome LoginExcInternal.txt > test.txt









share|improve this question



















  • 4





    sort -k2r LoginExternal.txt

    – steeldriver
    Mar 12 at 14:36











  • Genius! Can you explain how this works?

    – Ibrahim A
    Mar 12 at 14:38











  • I also need the names to stay in alphabetical order

    – Ibrahim A
    Mar 12 at 14:39











  • The names you have provided do stay in (lexical) order - if you have a more complex requirement, then please provide an example reflecting that. If you have GNU sort then you can try sort --stable -k2r LoginExternal.txt

    – steeldriver
    Mar 12 at 14:50


















1















I have a text file that looks something like this



Name1 OpenFin
Name2 Chrome
Name3 OpenFin
Name4 Chrome
Name5 OpenFin
Name6 OpenFin


I want to sort this is a way where all lines with OpenFin appears first followed by all lines with Chrome



This is what I have so far:



grep OpenFin LoginExcInternal.txt | grep Chrome LoginExcInternal.txt > test.txt









share|improve this question



















  • 4





    sort -k2r LoginExternal.txt

    – steeldriver
    Mar 12 at 14:36











  • Genius! Can you explain how this works?

    – Ibrahim A
    Mar 12 at 14:38











  • I also need the names to stay in alphabetical order

    – Ibrahim A
    Mar 12 at 14:39











  • The names you have provided do stay in (lexical) order - if you have a more complex requirement, then please provide an example reflecting that. If you have GNU sort then you can try sort --stable -k2r LoginExternal.txt

    – steeldriver
    Mar 12 at 14:50














1












1








1








I have a text file that looks something like this



Name1 OpenFin
Name2 Chrome
Name3 OpenFin
Name4 Chrome
Name5 OpenFin
Name6 OpenFin


I want to sort this is a way where all lines with OpenFin appears first followed by all lines with Chrome



This is what I have so far:



grep OpenFin LoginExcInternal.txt | grep Chrome LoginExcInternal.txt > test.txt









share|improve this question
















I have a text file that looks something like this



Name1 OpenFin
Name2 Chrome
Name3 OpenFin
Name4 Chrome
Name5 OpenFin
Name6 OpenFin


I want to sort this is a way where all lines with OpenFin appears first followed by all lines with Chrome



This is what I have so far:



grep OpenFin LoginExcInternal.txt | grep Chrome LoginExcInternal.txt > test.txt






linux text-processing sort






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 12 at 15:43









Jeff Schaller

44.9k1164147




44.9k1164147










asked Mar 12 at 14:32









Ibrahim AIbrahim A

419




419







  • 4





    sort -k2r LoginExternal.txt

    – steeldriver
    Mar 12 at 14:36











  • Genius! Can you explain how this works?

    – Ibrahim A
    Mar 12 at 14:38











  • I also need the names to stay in alphabetical order

    – Ibrahim A
    Mar 12 at 14:39











  • The names you have provided do stay in (lexical) order - if you have a more complex requirement, then please provide an example reflecting that. If you have GNU sort then you can try sort --stable -k2r LoginExternal.txt

    – steeldriver
    Mar 12 at 14:50













  • 4





    sort -k2r LoginExternal.txt

    – steeldriver
    Mar 12 at 14:36











  • Genius! Can you explain how this works?

    – Ibrahim A
    Mar 12 at 14:38











  • I also need the names to stay in alphabetical order

    – Ibrahim A
    Mar 12 at 14:39











  • The names you have provided do stay in (lexical) order - if you have a more complex requirement, then please provide an example reflecting that. If you have GNU sort then you can try sort --stable -k2r LoginExternal.txt

    – steeldriver
    Mar 12 at 14:50








4




4





sort -k2r LoginExternal.txt

– steeldriver
Mar 12 at 14:36





sort -k2r LoginExternal.txt

– steeldriver
Mar 12 at 14:36













Genius! Can you explain how this works?

– Ibrahim A
Mar 12 at 14:38





Genius! Can you explain how this works?

– Ibrahim A
Mar 12 at 14:38













I also need the names to stay in alphabetical order

– Ibrahim A
Mar 12 at 14:39





I also need the names to stay in alphabetical order

– Ibrahim A
Mar 12 at 14:39













The names you have provided do stay in (lexical) order - if you have a more complex requirement, then please provide an example reflecting that. If you have GNU sort then you can try sort --stable -k2r LoginExternal.txt

– steeldriver
Mar 12 at 14:50






The names you have provided do stay in (lexical) order - if you have a more complex requirement, then please provide an example reflecting that. If you have GNU sort then you can try sort --stable -k2r LoginExternal.txt

– steeldriver
Mar 12 at 14:50











3 Answers
3






active

oldest

votes


















2














A direct translation of your requirement:




all lines with OpenFin appears first, followed by all lines with Chrome




... without having to know which sorts first would be:



 grep -F OpenFin LoginExcInternal.txt; grep -F Chrome LoginExcInternal.txt; > test.txt


This also keeps the lines in the order in which they appeared in the original file (without going through hoops to keep sort from sorting on the whole line).






share|improve this answer






























    1














    Your command



    grep OpenFin LoginExcInternal.txt | grep Chrome LoginExcInternal.txt > test.txt


    would only produce the lines from the original data that contains the string Chrome anywhere on the line. This is because the result of the first grep in the pipeline would not be handled at all by the right hand side of the pipeline (it would simply be discarded).



    grep will, when given a filename to read from, not process its standard input stream. It's on the standard input stream that the result of the first grep arrives.




    sort -k2r LoginExcInternal.txt >test.txt


    You want the lines with Chrome in their 2nd column to come after the lines containing OpenFin in the same column. The above does that by means of sorting the original data on the second column in reverse lexicographical order.



    The -k2r flag to sort tells the utility to sort the data on the second column (and onwards, if there were more than two columns), in reverse order (the r). If two lines have the same value in the second column, then the whole line will be used as the sorting key.



    Lines with any other values in the second column will be sorted with the rest of the lines and also be part of the output.




    If you care about the original ordering of the data and don't want to change it, and assuming that you only want to extract the lines with these two values in the second column (and no other values) then do it in two steps:



    awk -v value='OpenFin' '$2 == value' LoginExcInternal.txt >test.txt
    awk -v value='Chrome' '$2 == value' LoginExcInternal.txt >>test.txt


    This applies the same awk code twice on the original data, with different values in the variable value. The code does a string comparison in the second column (only) and prints the lines that have the specified value. The first run of the awk script does the OpenFin values, and the second adds the Chrome lines to this.



    This avoids involving the first column in the comparison of the data, which would be an issue if any of the first column's values happened to be Chrome or OpenFin. Also, since the awk code uses string comparisons, it avoids outputting lines whose second column may contain one of the strings as substring.






    share|improve this answer
































      0














      This was answered by the user "emilrn" then removed for some reason. However, their response worked perfectly.



      I was told to run both of these commands:



      cat LoginExcInternal.txt | grep OpenFin >> test.txt
      cat LoginExcInternal.txt | grep Chrome >> test.txt


      Thank you emilrn!






      share|improve this answer























      • unnecessary cat. cat file | grep ... is almost always unnecessary.

        – Weijun Zhou
        Mar 12 at 18:48












      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',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      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%2f505891%2fsort-file-with-word%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      A direct translation of your requirement:




      all lines with OpenFin appears first, followed by all lines with Chrome




      ... without having to know which sorts first would be:



       grep -F OpenFin LoginExcInternal.txt; grep -F Chrome LoginExcInternal.txt; > test.txt


      This also keeps the lines in the order in which they appeared in the original file (without going through hoops to keep sort from sorting on the whole line).






      share|improve this answer



























        2














        A direct translation of your requirement:




        all lines with OpenFin appears first, followed by all lines with Chrome




        ... without having to know which sorts first would be:



         grep -F OpenFin LoginExcInternal.txt; grep -F Chrome LoginExcInternal.txt; > test.txt


        This also keeps the lines in the order in which they appeared in the original file (without going through hoops to keep sort from sorting on the whole line).






        share|improve this answer

























          2












          2








          2







          A direct translation of your requirement:




          all lines with OpenFin appears first, followed by all lines with Chrome




          ... without having to know which sorts first would be:



           grep -F OpenFin LoginExcInternal.txt; grep -F Chrome LoginExcInternal.txt; > test.txt


          This also keeps the lines in the order in which they appeared in the original file (without going through hoops to keep sort from sorting on the whole line).






          share|improve this answer













          A direct translation of your requirement:




          all lines with OpenFin appears first, followed by all lines with Chrome




          ... without having to know which sorts first would be:



           grep -F OpenFin LoginExcInternal.txt; grep -F Chrome LoginExcInternal.txt; > test.txt


          This also keeps the lines in the order in which they appeared in the original file (without going through hoops to keep sort from sorting on the whole line).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 12 at 15:48









          Jeff SchallerJeff Schaller

          44.9k1164147




          44.9k1164147























              1














              Your command



              grep OpenFin LoginExcInternal.txt | grep Chrome LoginExcInternal.txt > test.txt


              would only produce the lines from the original data that contains the string Chrome anywhere on the line. This is because the result of the first grep in the pipeline would not be handled at all by the right hand side of the pipeline (it would simply be discarded).



              grep will, when given a filename to read from, not process its standard input stream. It's on the standard input stream that the result of the first grep arrives.




              sort -k2r LoginExcInternal.txt >test.txt


              You want the lines with Chrome in their 2nd column to come after the lines containing OpenFin in the same column. The above does that by means of sorting the original data on the second column in reverse lexicographical order.



              The -k2r flag to sort tells the utility to sort the data on the second column (and onwards, if there were more than two columns), in reverse order (the r). If two lines have the same value in the second column, then the whole line will be used as the sorting key.



              Lines with any other values in the second column will be sorted with the rest of the lines and also be part of the output.




              If you care about the original ordering of the data and don't want to change it, and assuming that you only want to extract the lines with these two values in the second column (and no other values) then do it in two steps:



              awk -v value='OpenFin' '$2 == value' LoginExcInternal.txt >test.txt
              awk -v value='Chrome' '$2 == value' LoginExcInternal.txt >>test.txt


              This applies the same awk code twice on the original data, with different values in the variable value. The code does a string comparison in the second column (only) and prints the lines that have the specified value. The first run of the awk script does the OpenFin values, and the second adds the Chrome lines to this.



              This avoids involving the first column in the comparison of the data, which would be an issue if any of the first column's values happened to be Chrome or OpenFin. Also, since the awk code uses string comparisons, it avoids outputting lines whose second column may contain one of the strings as substring.






              share|improve this answer





























                1














                Your command



                grep OpenFin LoginExcInternal.txt | grep Chrome LoginExcInternal.txt > test.txt


                would only produce the lines from the original data that contains the string Chrome anywhere on the line. This is because the result of the first grep in the pipeline would not be handled at all by the right hand side of the pipeline (it would simply be discarded).



                grep will, when given a filename to read from, not process its standard input stream. It's on the standard input stream that the result of the first grep arrives.




                sort -k2r LoginExcInternal.txt >test.txt


                You want the lines with Chrome in their 2nd column to come after the lines containing OpenFin in the same column. The above does that by means of sorting the original data on the second column in reverse lexicographical order.



                The -k2r flag to sort tells the utility to sort the data on the second column (and onwards, if there were more than two columns), in reverse order (the r). If two lines have the same value in the second column, then the whole line will be used as the sorting key.



                Lines with any other values in the second column will be sorted with the rest of the lines and also be part of the output.




                If you care about the original ordering of the data and don't want to change it, and assuming that you only want to extract the lines with these two values in the second column (and no other values) then do it in two steps:



                awk -v value='OpenFin' '$2 == value' LoginExcInternal.txt >test.txt
                awk -v value='Chrome' '$2 == value' LoginExcInternal.txt >>test.txt


                This applies the same awk code twice on the original data, with different values in the variable value. The code does a string comparison in the second column (only) and prints the lines that have the specified value. The first run of the awk script does the OpenFin values, and the second adds the Chrome lines to this.



                This avoids involving the first column in the comparison of the data, which would be an issue if any of the first column's values happened to be Chrome or OpenFin. Also, since the awk code uses string comparisons, it avoids outputting lines whose second column may contain one of the strings as substring.






                share|improve this answer



























                  1












                  1








                  1







                  Your command



                  grep OpenFin LoginExcInternal.txt | grep Chrome LoginExcInternal.txt > test.txt


                  would only produce the lines from the original data that contains the string Chrome anywhere on the line. This is because the result of the first grep in the pipeline would not be handled at all by the right hand side of the pipeline (it would simply be discarded).



                  grep will, when given a filename to read from, not process its standard input stream. It's on the standard input stream that the result of the first grep arrives.




                  sort -k2r LoginExcInternal.txt >test.txt


                  You want the lines with Chrome in their 2nd column to come after the lines containing OpenFin in the same column. The above does that by means of sorting the original data on the second column in reverse lexicographical order.



                  The -k2r flag to sort tells the utility to sort the data on the second column (and onwards, if there were more than two columns), in reverse order (the r). If two lines have the same value in the second column, then the whole line will be used as the sorting key.



                  Lines with any other values in the second column will be sorted with the rest of the lines and also be part of the output.




                  If you care about the original ordering of the data and don't want to change it, and assuming that you only want to extract the lines with these two values in the second column (and no other values) then do it in two steps:



                  awk -v value='OpenFin' '$2 == value' LoginExcInternal.txt >test.txt
                  awk -v value='Chrome' '$2 == value' LoginExcInternal.txt >>test.txt


                  This applies the same awk code twice on the original data, with different values in the variable value. The code does a string comparison in the second column (only) and prints the lines that have the specified value. The first run of the awk script does the OpenFin values, and the second adds the Chrome lines to this.



                  This avoids involving the first column in the comparison of the data, which would be an issue if any of the first column's values happened to be Chrome or OpenFin. Also, since the awk code uses string comparisons, it avoids outputting lines whose second column may contain one of the strings as substring.






                  share|improve this answer















                  Your command



                  grep OpenFin LoginExcInternal.txt | grep Chrome LoginExcInternal.txt > test.txt


                  would only produce the lines from the original data that contains the string Chrome anywhere on the line. This is because the result of the first grep in the pipeline would not be handled at all by the right hand side of the pipeline (it would simply be discarded).



                  grep will, when given a filename to read from, not process its standard input stream. It's on the standard input stream that the result of the first grep arrives.




                  sort -k2r LoginExcInternal.txt >test.txt


                  You want the lines with Chrome in their 2nd column to come after the lines containing OpenFin in the same column. The above does that by means of sorting the original data on the second column in reverse lexicographical order.



                  The -k2r flag to sort tells the utility to sort the data on the second column (and onwards, if there were more than two columns), in reverse order (the r). If two lines have the same value in the second column, then the whole line will be used as the sorting key.



                  Lines with any other values in the second column will be sorted with the rest of the lines and also be part of the output.




                  If you care about the original ordering of the data and don't want to change it, and assuming that you only want to extract the lines with these two values in the second column (and no other values) then do it in two steps:



                  awk -v value='OpenFin' '$2 == value' LoginExcInternal.txt >test.txt
                  awk -v value='Chrome' '$2 == value' LoginExcInternal.txt >>test.txt


                  This applies the same awk code twice on the original data, with different values in the variable value. The code does a string comparison in the second column (only) and prints the lines that have the specified value. The first run of the awk script does the OpenFin values, and the second adds the Chrome lines to this.



                  This avoids involving the first column in the comparison of the data, which would be an issue if any of the first column's values happened to be Chrome or OpenFin. Also, since the awk code uses string comparisons, it avoids outputting lines whose second column may contain one of the strings as substring.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 12 at 16:17

























                  answered Mar 12 at 15:48









                  KusalanandaKusalananda

                  141k17262438




                  141k17262438





















                      0














                      This was answered by the user "emilrn" then removed for some reason. However, their response worked perfectly.



                      I was told to run both of these commands:



                      cat LoginExcInternal.txt | grep OpenFin >> test.txt
                      cat LoginExcInternal.txt | grep Chrome >> test.txt


                      Thank you emilrn!






                      share|improve this answer























                      • unnecessary cat. cat file | grep ... is almost always unnecessary.

                        – Weijun Zhou
                        Mar 12 at 18:48
















                      0














                      This was answered by the user "emilrn" then removed for some reason. However, their response worked perfectly.



                      I was told to run both of these commands:



                      cat LoginExcInternal.txt | grep OpenFin >> test.txt
                      cat LoginExcInternal.txt | grep Chrome >> test.txt


                      Thank you emilrn!






                      share|improve this answer























                      • unnecessary cat. cat file | grep ... is almost always unnecessary.

                        – Weijun Zhou
                        Mar 12 at 18:48














                      0












                      0








                      0







                      This was answered by the user "emilrn" then removed for some reason. However, their response worked perfectly.



                      I was told to run both of these commands:



                      cat LoginExcInternal.txt | grep OpenFin >> test.txt
                      cat LoginExcInternal.txt | grep Chrome >> test.txt


                      Thank you emilrn!






                      share|improve this answer













                      This was answered by the user "emilrn" then removed for some reason. However, their response worked perfectly.



                      I was told to run both of these commands:



                      cat LoginExcInternal.txt | grep OpenFin >> test.txt
                      cat LoginExcInternal.txt | grep Chrome >> test.txt


                      Thank you emilrn!







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Mar 12 at 15:08









                      Ibrahim AIbrahim A

                      419




                      419












                      • unnecessary cat. cat file | grep ... is almost always unnecessary.

                        – Weijun Zhou
                        Mar 12 at 18:48


















                      • unnecessary cat. cat file | grep ... is almost always unnecessary.

                        – Weijun Zhou
                        Mar 12 at 18:48

















                      unnecessary cat. cat file | grep ... is almost always unnecessary.

                      – Weijun Zhou
                      Mar 12 at 18:48






                      unnecessary cat. cat file | grep ... is almost always unnecessary.

                      – Weijun Zhou
                      Mar 12 at 18:48


















                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














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

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown






                      Popular posts from this blog

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

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?