Extract species ID number for specific species from file with awk

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











up vote
3
down vote

favorite












I need to Write an awk command that will return the identification number from the following table for only the lines where the title is Turtle. This table is stored in turtle.txt



Id Num. Title CatchDate
433417 RedTurtle 2001-06-29
493303 BlueTurtle 1998-09-20
259497 Turtle 1985-05-08
229505 RedTurtle 1994-07-13
473076 OrangeTurtle 2002-03-08
221907 Blueturtle 1999-07-02
457032 Turtle 1993-04-09
490359 RedTurtle 1996-11-12
494595 SnappingTurtle 1985-05-20
402421 BlueTurtle 1999-08-16









share|improve this question









New contributor




Kamat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    up vote
    3
    down vote

    favorite












    I need to Write an awk command that will return the identification number from the following table for only the lines where the title is Turtle. This table is stored in turtle.txt



    Id Num. Title CatchDate
    433417 RedTurtle 2001-06-29
    493303 BlueTurtle 1998-09-20
    259497 Turtle 1985-05-08
    229505 RedTurtle 1994-07-13
    473076 OrangeTurtle 2002-03-08
    221907 Blueturtle 1999-07-02
    457032 Turtle 1993-04-09
    490359 RedTurtle 1996-11-12
    494595 SnappingTurtle 1985-05-20
    402421 BlueTurtle 1999-08-16









    share|improve this question









    New contributor




    Kamat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I need to Write an awk command that will return the identification number from the following table for only the lines where the title is Turtle. This table is stored in turtle.txt



      Id Num. Title CatchDate
      433417 RedTurtle 2001-06-29
      493303 BlueTurtle 1998-09-20
      259497 Turtle 1985-05-08
      229505 RedTurtle 1994-07-13
      473076 OrangeTurtle 2002-03-08
      221907 Blueturtle 1999-07-02
      457032 Turtle 1993-04-09
      490359 RedTurtle 1996-11-12
      494595 SnappingTurtle 1985-05-20
      402421 BlueTurtle 1999-08-16









      share|improve this question









      New contributor




      Kamat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I need to Write an awk command that will return the identification number from the following table for only the lines where the title is Turtle. This table is stored in turtle.txt



      Id Num. Title CatchDate
      433417 RedTurtle 2001-06-29
      493303 BlueTurtle 1998-09-20
      259497 Turtle 1985-05-08
      229505 RedTurtle 1994-07-13
      473076 OrangeTurtle 2002-03-08
      221907 Blueturtle 1999-07-02
      457032 Turtle 1993-04-09
      490359 RedTurtle 1996-11-12
      494595 SnappingTurtle 1985-05-20
      402421 BlueTurtle 1999-08-16






      text-processing awk






      share|improve this question









      New contributor




      Kamat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Kamat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 14 mins ago









      Kusalananda

      108k14210333




      108k14210333






      New contributor




      Kamat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 1 hour ago









      Kamat

      162




      162




      New contributor




      Kamat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Kamat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Kamat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          5 Answers
          5






          active

          oldest

          votes

















          up vote
          5
          down vote













          You can use:



          awk '$2 == "Turtle" print $1' file
          259497
          457032





          share|improve this answer





























            up vote
            5
            down vote













            awk '$2 == "Turtle" print $1' turtle.txt
            259497
            457032


            $2 is the field to select

            Turtle is the text to match

            print $1 is to print the first field.

            turtle.txt is the name of the source file.






            share|improve this answer





























              up vote
              4
              down vote













              Golfing it:



              $ awk '$2=="Turtle"&&$0=$1' <file
              259497
              457032


              Or, expanded in stages until we reach Isaac's and Goro's answers



              awk '$2 == "Turtle" && $0 = $1' <file

              awk '$2 == "Turtle" $0 = $1; print ' <file

              awk '$2 == "Turtle" print $1 ' <file


              The three are not exactly equivalent as my golfed code would not print the number if it was zero.




              Here's a proper sed solution to make up for the golfing above:



              $ sed -n '/<Turtle>/s/[[:blank:]].*//p' <file
              259497
              457032


              It finds all lines containing the word Turtle and then removes everything after the first space or tab character on those lines before printing them (printing of other lines is inhibited by -n).



              The < and > matches word start and end boundaries so that <Turtle> matches only the string Turtle and not e.g. RedTurtle.






              share|improve this answer





























                up vote
                3
                down vote













                non-awk alternative:



                grep -w "Turtle" turtle.txt | cut -d " " -f 1






                share|improve this answer



























                  up vote
                  3
                  down vote













                  Using sed:



                  sed -n '/sTurtles/s/^([0-9]+)s.*/1/p' file





                  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
                    );



                    );






                    Kamat is a new contributor. Be nice, and check out our Code of Conduct.









                     

                    draft saved


                    draft discarded


















                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f473978%2fextract-species-id-number-for-specific-species-from-file-with-awk%23new-answer', 'question_page');

                    );

                    Post as a guest






























                    5 Answers
                    5






                    active

                    oldest

                    votes








                    5 Answers
                    5






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes








                    up vote
                    5
                    down vote













                    You can use:



                    awk '$2 == "Turtle" print $1' file
                    259497
                    457032





                    share|improve this answer


























                      up vote
                      5
                      down vote













                      You can use:



                      awk '$2 == "Turtle" print $1' file
                      259497
                      457032





                      share|improve this answer
























                        up vote
                        5
                        down vote










                        up vote
                        5
                        down vote









                        You can use:



                        awk '$2 == "Turtle" print $1' file
                        259497
                        457032





                        share|improve this answer














                        You can use:



                        awk '$2 == "Turtle" print $1' file
                        259497
                        457032






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited 57 mins ago

























                        answered 1 hour ago









                        Goro

                        7,49753170




                        7,49753170






















                            up vote
                            5
                            down vote













                            awk '$2 == "Turtle" print $1' turtle.txt
                            259497
                            457032


                            $2 is the field to select

                            Turtle is the text to match

                            print $1 is to print the first field.

                            turtle.txt is the name of the source file.






                            share|improve this answer


























                              up vote
                              5
                              down vote













                              awk '$2 == "Turtle" print $1' turtle.txt
                              259497
                              457032


                              $2 is the field to select

                              Turtle is the text to match

                              print $1 is to print the first field.

                              turtle.txt is the name of the source file.






                              share|improve this answer
























                                up vote
                                5
                                down vote










                                up vote
                                5
                                down vote









                                awk '$2 == "Turtle" print $1' turtle.txt
                                259497
                                457032


                                $2 is the field to select

                                Turtle is the text to match

                                print $1 is to print the first field.

                                turtle.txt is the name of the source file.






                                share|improve this answer














                                awk '$2 == "Turtle" print $1' turtle.txt
                                259497
                                457032


                                $2 is the field to select

                                Turtle is the text to match

                                print $1 is to print the first field.

                                turtle.txt is the name of the source file.







                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited 40 mins ago

























                                answered 1 hour ago









                                Isaac

                                7,80711137




                                7,80711137




















                                    up vote
                                    4
                                    down vote













                                    Golfing it:



                                    $ awk '$2=="Turtle"&&$0=$1' <file
                                    259497
                                    457032


                                    Or, expanded in stages until we reach Isaac's and Goro's answers



                                    awk '$2 == "Turtle" && $0 = $1' <file

                                    awk '$2 == "Turtle" $0 = $1; print ' <file

                                    awk '$2 == "Turtle" print $1 ' <file


                                    The three are not exactly equivalent as my golfed code would not print the number if it was zero.




                                    Here's a proper sed solution to make up for the golfing above:



                                    $ sed -n '/<Turtle>/s/[[:blank:]].*//p' <file
                                    259497
                                    457032


                                    It finds all lines containing the word Turtle and then removes everything after the first space or tab character on those lines before printing them (printing of other lines is inhibited by -n).



                                    The < and > matches word start and end boundaries so that <Turtle> matches only the string Turtle and not e.g. RedTurtle.






                                    share|improve this answer


























                                      up vote
                                      4
                                      down vote













                                      Golfing it:



                                      $ awk '$2=="Turtle"&&$0=$1' <file
                                      259497
                                      457032


                                      Or, expanded in stages until we reach Isaac's and Goro's answers



                                      awk '$2 == "Turtle" && $0 = $1' <file

                                      awk '$2 == "Turtle" $0 = $1; print ' <file

                                      awk '$2 == "Turtle" print $1 ' <file


                                      The three are not exactly equivalent as my golfed code would not print the number if it was zero.




                                      Here's a proper sed solution to make up for the golfing above:



                                      $ sed -n '/<Turtle>/s/[[:blank:]].*//p' <file
                                      259497
                                      457032


                                      It finds all lines containing the word Turtle and then removes everything after the first space or tab character on those lines before printing them (printing of other lines is inhibited by -n).



                                      The < and > matches word start and end boundaries so that <Turtle> matches only the string Turtle and not e.g. RedTurtle.






                                      share|improve this answer
























                                        up vote
                                        4
                                        down vote










                                        up vote
                                        4
                                        down vote









                                        Golfing it:



                                        $ awk '$2=="Turtle"&&$0=$1' <file
                                        259497
                                        457032


                                        Or, expanded in stages until we reach Isaac's and Goro's answers



                                        awk '$2 == "Turtle" && $0 = $1' <file

                                        awk '$2 == "Turtle" $0 = $1; print ' <file

                                        awk '$2 == "Turtle" print $1 ' <file


                                        The three are not exactly equivalent as my golfed code would not print the number if it was zero.




                                        Here's a proper sed solution to make up for the golfing above:



                                        $ sed -n '/<Turtle>/s/[[:blank:]].*//p' <file
                                        259497
                                        457032


                                        It finds all lines containing the word Turtle and then removes everything after the first space or tab character on those lines before printing them (printing of other lines is inhibited by -n).



                                        The < and > matches word start and end boundaries so that <Turtle> matches only the string Turtle and not e.g. RedTurtle.






                                        share|improve this answer














                                        Golfing it:



                                        $ awk '$2=="Turtle"&&$0=$1' <file
                                        259497
                                        457032


                                        Or, expanded in stages until we reach Isaac's and Goro's answers



                                        awk '$2 == "Turtle" && $0 = $1' <file

                                        awk '$2 == "Turtle" $0 = $1; print ' <file

                                        awk '$2 == "Turtle" print $1 ' <file


                                        The three are not exactly equivalent as my golfed code would not print the number if it was zero.




                                        Here's a proper sed solution to make up for the golfing above:



                                        $ sed -n '/<Turtle>/s/[[:blank:]].*//p' <file
                                        259497
                                        457032


                                        It finds all lines containing the word Turtle and then removes everything after the first space or tab character on those lines before printing them (printing of other lines is inhibited by -n).



                                        The < and > matches word start and end boundaries so that <Turtle> matches only the string Turtle and not e.g. RedTurtle.







                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited 12 mins ago

























                                        answered 36 mins ago









                                        Kusalananda

                                        108k14210333




                                        108k14210333




















                                            up vote
                                            3
                                            down vote













                                            non-awk alternative:



                                            grep -w "Turtle" turtle.txt | cut -d " " -f 1






                                            share|improve this answer
























                                              up vote
                                              3
                                              down vote













                                              non-awk alternative:



                                              grep -w "Turtle" turtle.txt | cut -d " " -f 1






                                              share|improve this answer






















                                                up vote
                                                3
                                                down vote










                                                up vote
                                                3
                                                down vote









                                                non-awk alternative:



                                                grep -w "Turtle" turtle.txt | cut -d " " -f 1






                                                share|improve this answer












                                                non-awk alternative:



                                                grep -w "Turtle" turtle.txt | cut -d " " -f 1







                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered 1 hour ago









                                                RobotJohnny

                                                740216




                                                740216




















                                                    up vote
                                                    3
                                                    down vote













                                                    Using sed:



                                                    sed -n '/sTurtles/s/^([0-9]+)s.*/1/p' file





                                                    share|improve this answer
























                                                      up vote
                                                      3
                                                      down vote













                                                      Using sed:



                                                      sed -n '/sTurtles/s/^([0-9]+)s.*/1/p' file





                                                      share|improve this answer






















                                                        up vote
                                                        3
                                                        down vote










                                                        up vote
                                                        3
                                                        down vote









                                                        Using sed:



                                                        sed -n '/sTurtles/s/^([0-9]+)s.*/1/p' file





                                                        share|improve this answer












                                                        Using sed:



                                                        sed -n '/sTurtles/s/^([0-9]+)s.*/1/p' file






                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered 53 mins ago









                                                        oliv

                                                        1,121210




                                                        1,121210




















                                                            Kamat is a new contributor. Be nice, and check out our Code of Conduct.









                                                             

                                                            draft saved


                                                            draft discarded


















                                                            Kamat is a new contributor. Be nice, and check out our Code of Conduct.












                                                            Kamat is a new contributor. Be nice, and check out our Code of Conduct.











                                                            Kamat is a new contributor. Be nice, and check out our Code of Conduct.













                                                             


                                                            draft saved


                                                            draft discarded














                                                            StackExchange.ready(
                                                            function ()
                                                            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f473978%2fextract-species-id-number-for-specific-species-from-file-with-awk%23new-answer', 'question_page');

                                                            );

                                                            Post as a guest













































































                                                            Comments

                                                            Popular posts from this blog

                                                            What does second last employer means? [closed]

                                                            List of Gilmore Girls characters

                                                            Confectionery