Is the an elegant way to select rows in a file where a given column has a given value?

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











up vote
0
down vote

favorite












I have a file with a lot of lines like this



0 file:/home/knappen/somefilename.txt 7 0.2838356973995272 19 0.21823286052009455 18 0.10121158392434988 15 0.07816193853427897 11
0.07284278959810875 6 0.056885342789598115 8 0.03738179669030733 22 0.032062647754137114 23 0.01610520094562648 12 0.01610520094562648 16 0.010786052009456266 0 0.010786052009456266 13 0.009013002364066195 5 0.009013002364066195 10 0.007239952718676124 9 0.007239952718676124 14 0.005466903073286052 4 0.005466903073286052 21 0.003693853427895981 20 0.003693853427895981 17 0.003693853427895981 3 0.003693853427895981 2 0.003693853427895981 1
0.003693853427895981


and I want to select all rows where the entry in the third columns equals to some given number.



I know how to write a pattern for grep -E for this purpose or to write a small python or perl script with this effect, but I wonder whether there is an elegant solution using GNU coreutils.



P.S. I found some answers with good suggestions in this question Selecting rows in a CSV file based on column value, but the tools are beyond GNU coreutils. The answers there are good enough to work for me, but for the sake of learning more about the power of the shell utilities I ask this question anyhow.










share|improve this question

























    up vote
    0
    down vote

    favorite












    I have a file with a lot of lines like this



    0 file:/home/knappen/somefilename.txt 7 0.2838356973995272 19 0.21823286052009455 18 0.10121158392434988 15 0.07816193853427897 11
    0.07284278959810875 6 0.056885342789598115 8 0.03738179669030733 22 0.032062647754137114 23 0.01610520094562648 12 0.01610520094562648 16 0.010786052009456266 0 0.010786052009456266 13 0.009013002364066195 5 0.009013002364066195 10 0.007239952718676124 9 0.007239952718676124 14 0.005466903073286052 4 0.005466903073286052 21 0.003693853427895981 20 0.003693853427895981 17 0.003693853427895981 3 0.003693853427895981 2 0.003693853427895981 1
    0.003693853427895981


    and I want to select all rows where the entry in the third columns equals to some given number.



    I know how to write a pattern for grep -E for this purpose or to write a small python or perl script with this effect, but I wonder whether there is an elegant solution using GNU coreutils.



    P.S. I found some answers with good suggestions in this question Selecting rows in a CSV file based on column value, but the tools are beyond GNU coreutils. The answers there are good enough to work for me, but for the sake of learning more about the power of the shell utilities I ask this question anyhow.










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a file with a lot of lines like this



      0 file:/home/knappen/somefilename.txt 7 0.2838356973995272 19 0.21823286052009455 18 0.10121158392434988 15 0.07816193853427897 11
      0.07284278959810875 6 0.056885342789598115 8 0.03738179669030733 22 0.032062647754137114 23 0.01610520094562648 12 0.01610520094562648 16 0.010786052009456266 0 0.010786052009456266 13 0.009013002364066195 5 0.009013002364066195 10 0.007239952718676124 9 0.007239952718676124 14 0.005466903073286052 4 0.005466903073286052 21 0.003693853427895981 20 0.003693853427895981 17 0.003693853427895981 3 0.003693853427895981 2 0.003693853427895981 1
      0.003693853427895981


      and I want to select all rows where the entry in the third columns equals to some given number.



      I know how to write a pattern for grep -E for this purpose or to write a small python or perl script with this effect, but I wonder whether there is an elegant solution using GNU coreutils.



      P.S. I found some answers with good suggestions in this question Selecting rows in a CSV file based on column value, but the tools are beyond GNU coreutils. The answers there are good enough to work for me, but for the sake of learning more about the power of the shell utilities I ask this question anyhow.










      share|improve this question













      I have a file with a lot of lines like this



      0 file:/home/knappen/somefilename.txt 7 0.2838356973995272 19 0.21823286052009455 18 0.10121158392434988 15 0.07816193853427897 11
      0.07284278959810875 6 0.056885342789598115 8 0.03738179669030733 22 0.032062647754137114 23 0.01610520094562648 12 0.01610520094562648 16 0.010786052009456266 0 0.010786052009456266 13 0.009013002364066195 5 0.009013002364066195 10 0.007239952718676124 9 0.007239952718676124 14 0.005466903073286052 4 0.005466903073286052 21 0.003693853427895981 20 0.003693853427895981 17 0.003693853427895981 3 0.003693853427895981 2 0.003693853427895981 1
      0.003693853427895981


      and I want to select all rows where the entry in the third columns equals to some given number.



      I know how to write a pattern for grep -E for this purpose or to write a small python or perl script with this effect, but I wonder whether there is an elegant solution using GNU coreutils.



      P.S. I found some answers with good suggestions in this question Selecting rows in a CSV file based on column value, but the tools are beyond GNU coreutils. The answers there are good enough to work for me, but for the sake of learning more about the power of the shell utilities I ask this question anyhow.







      text-processing csv coreutils






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 15 hours ago









      jknappen

      258113




      258113




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote













          Awk is a pretty powerful tool for text-formatting/manipulation and pattern matching. It de-limits each line of the file with a custom de-limiter with default being a single white-space. Once split the individual fields can be accessed from $1,$2..$N, where N is the last column number in the line.



          So to your requirement just match the third column with the value you need



          awk '$3 == "string"' file 


          If the file is a CSV which has a different de-limiter, e.g. , just define it as



          awk -v FS="," '$3 == "string"' file


          Also awk is not GNU specific, its variant should be available across *nix machines and also POSIX compliant.






          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: 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%2f481275%2fis-the-an-elegant-way-to-select-rows-in-a-file-where-a-given-column-has-a-given%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote













            Awk is a pretty powerful tool for text-formatting/manipulation and pattern matching. It de-limits each line of the file with a custom de-limiter with default being a single white-space. Once split the individual fields can be accessed from $1,$2..$N, where N is the last column number in the line.



            So to your requirement just match the third column with the value you need



            awk '$3 == "string"' file 


            If the file is a CSV which has a different de-limiter, e.g. , just define it as



            awk -v FS="," '$3 == "string"' file


            Also awk is not GNU specific, its variant should be available across *nix machines and also POSIX compliant.






            share|improve this answer


























              up vote
              3
              down vote













              Awk is a pretty powerful tool for text-formatting/manipulation and pattern matching. It de-limits each line of the file with a custom de-limiter with default being a single white-space. Once split the individual fields can be accessed from $1,$2..$N, where N is the last column number in the line.



              So to your requirement just match the third column with the value you need



              awk '$3 == "string"' file 


              If the file is a CSV which has a different de-limiter, e.g. , just define it as



              awk -v FS="," '$3 == "string"' file


              Also awk is not GNU specific, its variant should be available across *nix machines and also POSIX compliant.






              share|improve this answer
























                up vote
                3
                down vote










                up vote
                3
                down vote









                Awk is a pretty powerful tool for text-formatting/manipulation and pattern matching. It de-limits each line of the file with a custom de-limiter with default being a single white-space. Once split the individual fields can be accessed from $1,$2..$N, where N is the last column number in the line.



                So to your requirement just match the third column with the value you need



                awk '$3 == "string"' file 


                If the file is a CSV which has a different de-limiter, e.g. , just define it as



                awk -v FS="," '$3 == "string"' file


                Also awk is not GNU specific, its variant should be available across *nix machines and also POSIX compliant.






                share|improve this answer














                Awk is a pretty powerful tool for text-formatting/manipulation and pattern matching. It de-limits each line of the file with a custom de-limiter with default being a single white-space. Once split the individual fields can be accessed from $1,$2..$N, where N is the last column number in the line.



                So to your requirement just match the third column with the value you need



                awk '$3 == "string"' file 


                If the file is a CSV which has a different de-limiter, e.g. , just define it as



                awk -v FS="," '$3 == "string"' file


                Also awk is not GNU specific, its variant should be available across *nix machines and also POSIX compliant.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 15 hours ago

























                answered 15 hours ago









                Inian

                3,695823




                3,695823



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f481275%2fis-the-an-elegant-way-to-select-rows-in-a-file-where-a-given-column-has-a-given%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?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?