Sort File with word2019 Community Moderator Electionsort a file based on length of the column/rowGrouped sorting of continuous paragraphs (separated by blank line)?Sort -u without sorting but with better uniqueness?sort behaves weirdly with scientific notationSort text by specific location on each lineSort unix alphabetically then numerically, not working as I intendedremove all duplicates from a text file without sortSort text files with multiple lines as a rowSort by Regex when reviewing fileHow to print all the lines that's first word is the first word of a file?

Am I not good enough for you?

GPLv2 - licensing for commercial use

Why was Goose renamed from Chewie for the Captain Marvel film?

Can someone explain what is being said here in color publishing in the American Mathematical Monthly?

How are showroom/display vehicles prepared?

How do you like my writing?

Could you please stop shuffling the deck and play already?

Should I tell my boss the work he did was worthless

Examples of a statistic that is not independent of sample's distribution?

What to do when during a meeting client people start to fight (even physically) with each others?

How do I deal with a powergamer in a game full of beginners in a school club?

Is it true that real estate prices mainly go up?

Intuition behind counterexample of Euler's sum of powers conjecture

Tricky AM-GM inequality

Try Catch Block Affecting a Variable in an Enclosing Scope

Virginia employer terminated employee and wants signing bonus returned

Finding algorithms of QGIS commands?

How to pass a string to a command that expects a file?

infinitive telling the purpose

How to create a hard link to an inode (ext4)?

Accepted offer letter, position changed

Force user to remove USB token

Best approach to update all entries in a list that is paginated?

Bash script should only kill those instances of another script's that it has launched



Sort File with word



2019 Community Moderator Electionsort a file based on length of the column/rowGrouped sorting of continuous paragraphs (separated by blank line)?Sort -u without sorting but with better uniqueness?sort behaves weirdly with scientific notationSort text by specific location on each lineSort unix alphabetically then numerically, not working as I intendedremove all duplicates from a text file without sortSort text files with multiple lines as a rowSort by Regex when reviewing fileHow to print all the lines that's first word is the first word of a file?










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



















  • 3





    sort -k2r LoginExternal.txt

    – steeldriver
    2 hours ago











  • Genius! Can you explain how this works?

    – Ibrahim A
    2 hours ago











  • I also need the names to stay in alphabetical order

    – Ibrahim A
    2 hours ago











  • 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
    2 hours ago
















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



















  • 3





    sort -k2r LoginExternal.txt

    – steeldriver
    2 hours ago











  • Genius! Can you explain how this works?

    – Ibrahim A
    2 hours ago











  • I also need the names to stay in alphabetical order

    – Ibrahim A
    2 hours ago











  • 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
    2 hours ago














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 1 hour ago









Jeff Schaller

43.3k1159139




43.3k1159139










asked 2 hours ago









Ibrahim AIbrahim A

379




379







  • 3





    sort -k2r LoginExternal.txt

    – steeldriver
    2 hours ago











  • Genius! Can you explain how this works?

    – Ibrahim A
    2 hours ago











  • I also need the names to stay in alphabetical order

    – Ibrahim A
    2 hours ago











  • 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
    2 hours ago













  • 3





    sort -k2r LoginExternal.txt

    – steeldriver
    2 hours ago











  • Genius! Can you explain how this works?

    – Ibrahim A
    2 hours ago











  • I also need the names to stay in alphabetical order

    – Ibrahim A
    2 hours ago











  • 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
    2 hours ago








3




3





sort -k2r LoginExternal.txt

– steeldriver
2 hours ago





sort -k2r LoginExternal.txt

– steeldriver
2 hours ago













Genius! Can you explain how this works?

– Ibrahim A
2 hours ago





Genius! Can you explain how this works?

– Ibrahim A
2 hours ago













I also need the names to stay in alphabetical order

– Ibrahim A
2 hours ago





I also need the names to stay in alphabetical order

– Ibrahim A
2 hours ago













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
2 hours ago






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
2 hours ago











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






















        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 1 hour ago









            Jeff SchallerJeff Schaller

            43.3k1159139




            43.3k1159139























                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 52 mins ago

























                    answered 1 hour ago









                    KusalanandaKusalananda

                    135k17255421




                    135k17255421





















                        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



























                          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

























                            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 2 hours ago









                            Ibrahim AIbrahim A

                            379




                            379



























                                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







                                -linux, sort, text-processing

                                Popular posts from this blog

                                Frič See also Navigation menuinternal link

                                Identify plant with long narrow paired leaves and reddish stems Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?What is this plant with long sharp leaves? Is it a weed?What is this 3ft high, stalky plant, with mid sized narrow leaves?What is this young shrub with opposite ovate, crenate leaves and reddish stems?What is this plant with large broad serrated leaves?Identify this upright branching weed with long leaves and reddish stemsPlease help me identify this bulbous plant with long, broad leaves and white flowersWhat is this small annual with narrow gray/green leaves and rust colored daisy-type flowers?What is this chilli plant?Does anyone know what type of chilli plant this is?Help identify this plant

                                fontconfig warning: “/etc/fonts/fonts.conf”, line 100: unknown “element blank” The 2019 Stack Overflow Developer Survey Results Are In“tar: unrecognized option --warning” during 'apt-get install'How to fix Fontconfig errorHow do I figure out which font file is chosen for a system generic font alias?Why are some apt-get-installed fonts being ignored by fc-list, xfontsel, etc?Reload settings in /etc/fonts/conf.dTaking 30 seconds longer to boot after upgrade from jessie to stretchHow to match multiple font names with a single <match> element?Adding a custom font to fontconfigRemoving fonts from fontconfig <match> resultsBroken fonts after upgrading Firefox ESR to latest Firefox