Extract `m`th (first) column value for line with specific `n`th (second) column value from file

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











up vote
6
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.















  • 1




    I'm amazed that this simple question could attract 6 answers so far.
    – simlev
    11 hours ago










  • @simlev, the Hot Network Questions algorithm is a mystery; it's contributing to the views & votes here
    – Jeff Schaller
    11 hours ago










  • Hi Kamat. I generalized the title of your question. The answer would be the same if this was about, say, a part number / description / price table, so the fact that it's about animals in this case seems inconsequential to answering the question.
    – Michael Kjörling
    9 hours ago










  • (1) I’m amazed that a trivial question like this can accumulate five edits (by five different people), six upvotes, no downvotes, and no close votes (after approximately 7 hours). (2) @MichaelKjörling: Wow, that is the general version of the title?!  When I saw the title “Extract first column value for line with specific second column value from three-columns file with awk”, my first thought was that it should be renamed to “Extract a field from a line based on a value in another field”.
    – G-Man
    6 hours ago










  • Possible duplicate of filter based on a field value in awk
    – G-Man
    36 secs ago














up vote
6
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.















  • 1




    I'm amazed that this simple question could attract 6 answers so far.
    – simlev
    11 hours ago










  • @simlev, the Hot Network Questions algorithm is a mystery; it's contributing to the views & votes here
    – Jeff Schaller
    11 hours ago










  • Hi Kamat. I generalized the title of your question. The answer would be the same if this was about, say, a part number / description / price table, so the fact that it's about animals in this case seems inconsequential to answering the question.
    – Michael Kjörling
    9 hours ago










  • (1) I’m amazed that a trivial question like this can accumulate five edits (by five different people), six upvotes, no downvotes, and no close votes (after approximately 7 hours). (2) @MichaelKjörling: Wow, that is the general version of the title?!  When I saw the title “Extract first column value for line with specific second column value from three-columns file with awk”, my first thought was that it should be renamed to “Extract a field from a line based on a value in another field”.
    – G-Man
    6 hours ago










  • Possible duplicate of filter based on a field value in awk
    – G-Man
    36 secs ago












up vote
6
down vote

favorite









up vote
6
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 sed






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









Isaac

7,85711137




7,85711137






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









Kamat

383




383




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.







  • 1




    I'm amazed that this simple question could attract 6 answers so far.
    – simlev
    11 hours ago










  • @simlev, the Hot Network Questions algorithm is a mystery; it's contributing to the views & votes here
    – Jeff Schaller
    11 hours ago










  • Hi Kamat. I generalized the title of your question. The answer would be the same if this was about, say, a part number / description / price table, so the fact that it's about animals in this case seems inconsequential to answering the question.
    – Michael Kjörling
    9 hours ago










  • (1) I’m amazed that a trivial question like this can accumulate five edits (by five different people), six upvotes, no downvotes, and no close votes (after approximately 7 hours). (2) @MichaelKjörling: Wow, that is the general version of the title?!  When I saw the title “Extract first column value for line with specific second column value from three-columns file with awk”, my first thought was that it should be renamed to “Extract a field from a line based on a value in another field”.
    – G-Man
    6 hours ago










  • Possible duplicate of filter based on a field value in awk
    – G-Man
    36 secs ago












  • 1




    I'm amazed that this simple question could attract 6 answers so far.
    – simlev
    11 hours ago










  • @simlev, the Hot Network Questions algorithm is a mystery; it's contributing to the views & votes here
    – Jeff Schaller
    11 hours ago










  • Hi Kamat. I generalized the title of your question. The answer would be the same if this was about, say, a part number / description / price table, so the fact that it's about animals in this case seems inconsequential to answering the question.
    – Michael Kjörling
    9 hours ago










  • (1) I’m amazed that a trivial question like this can accumulate five edits (by five different people), six upvotes, no downvotes, and no close votes (after approximately 7 hours). (2) @MichaelKjörling: Wow, that is the general version of the title?!  When I saw the title “Extract first column value for line with specific second column value from three-columns file with awk”, my first thought was that it should be renamed to “Extract a field from a line based on a value in another field”.
    – G-Man
    6 hours ago










  • Possible duplicate of filter based on a field value in awk
    – G-Man
    36 secs ago







1




1




I'm amazed that this simple question could attract 6 answers so far.
– simlev
11 hours ago




I'm amazed that this simple question could attract 6 answers so far.
– simlev
11 hours ago












@simlev, the Hot Network Questions algorithm is a mystery; it's contributing to the views & votes here
– Jeff Schaller
11 hours ago




@simlev, the Hot Network Questions algorithm is a mystery; it's contributing to the views & votes here
– Jeff Schaller
11 hours ago












Hi Kamat. I generalized the title of your question. The answer would be the same if this was about, say, a part number / description / price table, so the fact that it's about animals in this case seems inconsequential to answering the question.
– Michael Kjörling
9 hours ago




Hi Kamat. I generalized the title of your question. The answer would be the same if this was about, say, a part number / description / price table, so the fact that it's about animals in this case seems inconsequential to answering the question.
– Michael Kjörling
9 hours ago












(1) I’m amazed that a trivial question like this can accumulate five edits (by five different people), six upvotes, no downvotes, and no close votes (after approximately 7 hours). (2) @MichaelKjörling: Wow, that is the general version of the title?!  When I saw the title “Extract first column value for line with specific second column value from three-columns file with awk”, my first thought was that it should be renamed to “Extract a field from a line based on a value in another field”.
– G-Man
6 hours ago




(1) I’m amazed that a trivial question like this can accumulate five edits (by five different people), six upvotes, no downvotes, and no close votes (after approximately 7 hours). (2) @MichaelKjörling: Wow, that is the general version of the title?!  When I saw the title “Extract first column value for line with specific second column value from three-columns file with awk”, my first thought was that it should be renamed to “Extract a field from a line based on a value in another field”.
– G-Man
6 hours ago












Possible duplicate of filter based on a field value in awk
– G-Man
36 secs ago




Possible duplicate of filter based on a field value in awk
– G-Man
36 secs ago










6 Answers
6






active

oldest

votes

















up vote
6
down vote













With awk:



$ 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.

With sed:



$ <infile sed -E 's/[[:blank:]]+/n/g;/([^n]+n)1Turtle/([^n]*).*/1/;p};d'


Explained:




  • <infile Source file


  • sed -E Use sed with POSIX ERE (Extended Regular Expresions)


  • 's/[[:blank:]]+/n/g Replace all (runs +) of tab-space with a new line.


  • /([^n]+n)1Turtlen/ If field n (use n-1 here) match Turtle (exactly).


  • ([^n]*).*/1/ Extract field 1 (first line)


  • p};d' Print what was selected and delete everything in any case.

General solution for any pair of field(s) n and m:



<infile sed -E 's/[[:blank:]]+/n/g;/([^n]+n)1Turtle/s/([^n]+n)0([^n]*).*/2/;p;d'



  • <infile Source file


  • sed -E ' For sed with ERE regexes.


  • s/[[:blank:]]+/n/g Break all input into lines at (runs of) tabs or spaces.


  • /([^n]+n)1Turtle/ If the pattern space match the nth field (use n-1 (1) here).


  • Start a sequence of commands.


  • s/ Start a replace (a s/// command).


  • ([^n]+n)0 Match m-1 (0) lines (for field m).


  • ([^n]*) Capture the field (the line) to keep in backreference 2.


  • .* And match everything else (in the pattern space (the original line)).


  • /2/ Replace all of above (The pattern space) with what was captured in 2.


  • ;p; Print it. And close command sequence.


  • d In any case, delete the pattern space, start again.


  • ' End sed command.





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
      4
      down vote













      Using sed:



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





      share|improve this answer




















      • Of course this would match “Turtle” appearing in any column except for the first or the last.  (Of course, for a three-column file, that does narrow it down to the second column — but it will fail on a line where the third column is missing.)   And, of course, it depends on the ID number being purely numeric.  Some people use things like “123.01”, “456A”, “78(9)” and “987-65-4321” as ID “numbers”.
        – G-Man
        6 hours ago

















      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 (the result of $0=$1 is used as a conditional).




      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 remowes the first space or tab character and everything after it 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






















      • The first one won't work properly when the id is zero
        – user000001
        11 hours ago






      • 1




        @user000001 This was already made clear in the answer.
        – Kusalananda
        11 hours ago






      • 2




        But it adds nothing useful IMO. That's more of an example of things not to do (use assignments as conditions in awk).
        – Stéphane Chazelas
        11 hours ago











      • Note that <Turtle> is not portable and would match on Red-Turtle. You would need something like sed -n 's/^[[:blank:]]*([^[:blank:]]1,)[[:blank:]]1,Turtle([[:blank:]].*)0,1$/1/p' for a real equivalent.
        – Stéphane Chazelas
        11 hours ago










      • @Kusalananda: Also, of course, your sed solution would match “Turtle” appearing in any column.
        – G-Man
        6 hours ago

















      up vote
      3
      down vote













      non-awk alternative:



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






      share|improve this answer
















      • 1




        But this would match “Turtle” (or even something like ‘‘Turtle#1998’’ or ‘‘Mock-Turtle’’) appearing in any column.
        – G-Man
        6 hours ago

















      up vote
      1
      down vote













      You may employ grep in this:



       grep -oP '^d+(?=h+Turtleh)'





      share|improve this answer




















      • We expect answers as complex/cryptic as this  to include an explanation.
        – G-Man
        6 hours ago










      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-mth-first-column-value-for-line-with-specific-nth-second-column%23new-answer', 'question_page');

      );

      Post as a guest






























      6 Answers
      6






      active

      oldest

      votes








      6 Answers
      6






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      6
      down vote













      With awk:



      $ 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.

      With sed:



      $ <infile sed -E 's/[[:blank:]]+/n/g;/([^n]+n)1Turtle/([^n]*).*/1/;p};d'


      Explained:




      • <infile Source file


      • sed -E Use sed with POSIX ERE (Extended Regular Expresions)


      • 's/[[:blank:]]+/n/g Replace all (runs +) of tab-space with a new line.


      • /([^n]+n)1Turtlen/ If field n (use n-1 here) match Turtle (exactly).


      • ([^n]*).*/1/ Extract field 1 (first line)


      • p};d' Print what was selected and delete everything in any case.

      General solution for any pair of field(s) n and m:



      <infile sed -E 's/[[:blank:]]+/n/g;/([^n]+n)1Turtle/s/([^n]+n)0([^n]*).*/2/;p;d'



      • <infile Source file


      • sed -E ' For sed with ERE regexes.


      • s/[[:blank:]]+/n/g Break all input into lines at (runs of) tabs or spaces.


      • /([^n]+n)1Turtle/ If the pattern space match the nth field (use n-1 (1) here).


      • Start a sequence of commands.


      • s/ Start a replace (a s/// command).


      • ([^n]+n)0 Match m-1 (0) lines (for field m).


      • ([^n]*) Capture the field (the line) to keep in backreference 2.


      • .* And match everything else (in the pattern space (the original line)).


      • /2/ Replace all of above (The pattern space) with what was captured in 2.


      • ;p; Print it. And close command sequence.


      • d In any case, delete the pattern space, start again.


      • ' End sed command.





      share|improve this answer


























        up vote
        6
        down vote













        With awk:



        $ 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.

        With sed:



        $ <infile sed -E 's/[[:blank:]]+/n/g;/([^n]+n)1Turtle/([^n]*).*/1/;p};d'


        Explained:




        • <infile Source file


        • sed -E Use sed with POSIX ERE (Extended Regular Expresions)


        • 's/[[:blank:]]+/n/g Replace all (runs +) of tab-space with a new line.


        • /([^n]+n)1Turtlen/ If field n (use n-1 here) match Turtle (exactly).


        • ([^n]*).*/1/ Extract field 1 (first line)


        • p};d' Print what was selected and delete everything in any case.

        General solution for any pair of field(s) n and m:



        <infile sed -E 's/[[:blank:]]+/n/g;/([^n]+n)1Turtle/s/([^n]+n)0([^n]*).*/2/;p;d'



        • <infile Source file


        • sed -E ' For sed with ERE regexes.


        • s/[[:blank:]]+/n/g Break all input into lines at (runs of) tabs or spaces.


        • /([^n]+n)1Turtle/ If the pattern space match the nth field (use n-1 (1) here).


        • Start a sequence of commands.


        • s/ Start a replace (a s/// command).


        • ([^n]+n)0 Match m-1 (0) lines (for field m).


        • ([^n]*) Capture the field (the line) to keep in backreference 2.


        • .* And match everything else (in the pattern space (the original line)).


        • /2/ Replace all of above (The pattern space) with what was captured in 2.


        • ;p; Print it. And close command sequence.


        • d In any case, delete the pattern space, start again.


        • ' End sed command.





        share|improve this answer
























          up vote
          6
          down vote










          up vote
          6
          down vote









          With awk:



          $ 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.

          With sed:



          $ <infile sed -E 's/[[:blank:]]+/n/g;/([^n]+n)1Turtle/([^n]*).*/1/;p};d'


          Explained:




          • <infile Source file


          • sed -E Use sed with POSIX ERE (Extended Regular Expresions)


          • 's/[[:blank:]]+/n/g Replace all (runs +) of tab-space with a new line.


          • /([^n]+n)1Turtlen/ If field n (use n-1 here) match Turtle (exactly).


          • ([^n]*).*/1/ Extract field 1 (first line)


          • p};d' Print what was selected and delete everything in any case.

          General solution for any pair of field(s) n and m:



          <infile sed -E 's/[[:blank:]]+/n/g;/([^n]+n)1Turtle/s/([^n]+n)0([^n]*).*/2/;p;d'



          • <infile Source file


          • sed -E ' For sed with ERE regexes.


          • s/[[:blank:]]+/n/g Break all input into lines at (runs of) tabs or spaces.


          • /([^n]+n)1Turtle/ If the pattern space match the nth field (use n-1 (1) here).


          • Start a sequence of commands.


          • s/ Start a replace (a s/// command).


          • ([^n]+n)0 Match m-1 (0) lines (for field m).


          • ([^n]*) Capture the field (the line) to keep in backreference 2.


          • .* And match everything else (in the pattern space (the original line)).


          • /2/ Replace all of above (The pattern space) with what was captured in 2.


          • ;p; Print it. And close command sequence.


          • d In any case, delete the pattern space, start again.


          • ' End sed command.





          share|improve this answer














          With awk:



          $ 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.

          With sed:



          $ <infile sed -E 's/[[:blank:]]+/n/g;/([^n]+n)1Turtle/([^n]*).*/1/;p};d'


          Explained:




          • <infile Source file


          • sed -E Use sed with POSIX ERE (Extended Regular Expresions)


          • 's/[[:blank:]]+/n/g Replace all (runs +) of tab-space with a new line.


          • /([^n]+n)1Turtlen/ If field n (use n-1 here) match Turtle (exactly).


          • ([^n]*).*/1/ Extract field 1 (first line)


          • p};d' Print what was selected and delete everything in any case.

          General solution for any pair of field(s) n and m:



          <infile sed -E 's/[[:blank:]]+/n/g;/([^n]+n)1Turtle/s/([^n]+n)0([^n]*).*/2/;p;d'



          • <infile Source file


          • sed -E ' For sed with ERE regexes.


          • s/[[:blank:]]+/n/g Break all input into lines at (runs of) tabs or spaces.


          • /([^n]+n)1Turtle/ If the pattern space match the nth field (use n-1 (1) here).


          • Start a sequence of commands.


          • s/ Start a replace (a s/// command).


          • ([^n]+n)0 Match m-1 (0) lines (for field m).


          • ([^n]*) Capture the field (the line) to keep in backreference 2.


          • .* And match everything else (in the pattern space (the original line)).


          • /2/ Replace all of above (The pattern space) with what was captured in 2.


          • ;p; Print it. And close command sequence.


          • d In any case, delete the pattern space, start again.


          • ' End sed command.






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 16 mins ago

























          answered 14 hours ago









          Isaac

          7,85711137




          7,85711137






















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

























                  answered 14 hours ago









                  Goro

                  7,56753371




                  7,56753371




















                      up vote
                      4
                      down vote













                      Using sed:



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





                      share|improve this answer




















                      • Of course this would match “Turtle” appearing in any column except for the first or the last.  (Of course, for a three-column file, that does narrow it down to the second column — but it will fail on a line where the third column is missing.)   And, of course, it depends on the ID number being purely numeric.  Some people use things like “123.01”, “456A”, “78(9)” and “987-65-4321” as ID “numbers”.
                        – G-Man
                        6 hours ago














                      up vote
                      4
                      down vote













                      Using sed:



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





                      share|improve this answer




















                      • Of course this would match “Turtle” appearing in any column except for the first or the last.  (Of course, for a three-column file, that does narrow it down to the second column — but it will fail on a line where the third column is missing.)   And, of course, it depends on the ID number being purely numeric.  Some people use things like “123.01”, “456A”, “78(9)” and “987-65-4321” as ID “numbers”.
                        – G-Man
                        6 hours ago












                      up vote
                      4
                      down vote










                      up vote
                      4
                      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 14 hours ago









                      oliv

                      1,131210




                      1,131210











                      • Of course this would match “Turtle” appearing in any column except for the first or the last.  (Of course, for a three-column file, that does narrow it down to the second column — but it will fail on a line where the third column is missing.)   And, of course, it depends on the ID number being purely numeric.  Some people use things like “123.01”, “456A”, “78(9)” and “987-65-4321” as ID “numbers”.
                        – G-Man
                        6 hours ago
















                      • Of course this would match “Turtle” appearing in any column except for the first or the last.  (Of course, for a three-column file, that does narrow it down to the second column — but it will fail on a line where the third column is missing.)   And, of course, it depends on the ID number being purely numeric.  Some people use things like “123.01”, “456A”, “78(9)” and “987-65-4321” as ID “numbers”.
                        – G-Man
                        6 hours ago















                      Of course this would match “Turtle” appearing in any column except for the first or the last.  (Of course, for a three-column file, that does narrow it down to the second column — but it will fail on a line where the third column is missing.)   And, of course, it depends on the ID number being purely numeric.  Some people use things like “123.01”, “456A”, “78(9)” and “987-65-4321” as ID “numbers”.
                      – G-Man
                      6 hours ago




                      Of course this would match “Turtle” appearing in any column except for the first or the last.  (Of course, for a three-column file, that does narrow it down to the second column — but it will fail on a line where the third column is missing.)   And, of course, it depends on the ID number being purely numeric.  Some people use things like “123.01”, “456A”, “78(9)” and “987-65-4321” as ID “numbers”.
                      – G-Man
                      6 hours ago










                      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 (the result of $0=$1 is used as a conditional).




                      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 remowes the first space or tab character and everything after it 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






















                      • The first one won't work properly when the id is zero
                        – user000001
                        11 hours ago






                      • 1




                        @user000001 This was already made clear in the answer.
                        – Kusalananda
                        11 hours ago






                      • 2




                        But it adds nothing useful IMO. That's more of an example of things not to do (use assignments as conditions in awk).
                        – Stéphane Chazelas
                        11 hours ago











                      • Note that <Turtle> is not portable and would match on Red-Turtle. You would need something like sed -n 's/^[[:blank:]]*([^[:blank:]]1,)[[:blank:]]1,Turtle([[:blank:]].*)0,1$/1/p' for a real equivalent.
                        – Stéphane Chazelas
                        11 hours ago










                      • @Kusalananda: Also, of course, your sed solution would match “Turtle” appearing in any column.
                        – G-Man
                        6 hours ago














                      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 (the result of $0=$1 is used as a conditional).




                      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 remowes the first space or tab character and everything after it 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






















                      • The first one won't work properly when the id is zero
                        – user000001
                        11 hours ago






                      • 1




                        @user000001 This was already made clear in the answer.
                        – Kusalananda
                        11 hours ago






                      • 2




                        But it adds nothing useful IMO. That's more of an example of things not to do (use assignments as conditions in awk).
                        – Stéphane Chazelas
                        11 hours ago











                      • Note that <Turtle> is not portable and would match on Red-Turtle. You would need something like sed -n 's/^[[:blank:]]*([^[:blank:]]1,)[[:blank:]]1,Turtle([[:blank:]].*)0,1$/1/p' for a real equivalent.
                        – Stéphane Chazelas
                        11 hours ago










                      • @Kusalananda: Also, of course, your sed solution would match “Turtle” appearing in any column.
                        – G-Man
                        6 hours ago












                      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 (the result of $0=$1 is used as a conditional).




                      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 remowes the first space or tab character and everything after it 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 (the result of $0=$1 is used as a conditional).




                      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 remowes the first space or tab character and everything after it 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 13 hours ago

























                      answered 13 hours ago









                      Kusalananda

                      109k14210334




                      109k14210334











                      • The first one won't work properly when the id is zero
                        – user000001
                        11 hours ago






                      • 1




                        @user000001 This was already made clear in the answer.
                        – Kusalananda
                        11 hours ago






                      • 2




                        But it adds nothing useful IMO. That's more of an example of things not to do (use assignments as conditions in awk).
                        – Stéphane Chazelas
                        11 hours ago











                      • Note that <Turtle> is not portable and would match on Red-Turtle. You would need something like sed -n 's/^[[:blank:]]*([^[:blank:]]1,)[[:blank:]]1,Turtle([[:blank:]].*)0,1$/1/p' for a real equivalent.
                        – Stéphane Chazelas
                        11 hours ago










                      • @Kusalananda: Also, of course, your sed solution would match “Turtle” appearing in any column.
                        – G-Man
                        6 hours ago
















                      • The first one won't work properly when the id is zero
                        – user000001
                        11 hours ago






                      • 1




                        @user000001 This was already made clear in the answer.
                        – Kusalananda
                        11 hours ago






                      • 2




                        But it adds nothing useful IMO. That's more of an example of things not to do (use assignments as conditions in awk).
                        – Stéphane Chazelas
                        11 hours ago











                      • Note that <Turtle> is not portable and would match on Red-Turtle. You would need something like sed -n 's/^[[:blank:]]*([^[:blank:]]1,)[[:blank:]]1,Turtle([[:blank:]].*)0,1$/1/p' for a real equivalent.
                        – Stéphane Chazelas
                        11 hours ago










                      • @Kusalananda: Also, of course, your sed solution would match “Turtle” appearing in any column.
                        – G-Man
                        6 hours ago















                      The first one won't work properly when the id is zero
                      – user000001
                      11 hours ago




                      The first one won't work properly when the id is zero
                      – user000001
                      11 hours ago




                      1




                      1




                      @user000001 This was already made clear in the answer.
                      – Kusalananda
                      11 hours ago




                      @user000001 This was already made clear in the answer.
                      – Kusalananda
                      11 hours ago




                      2




                      2




                      But it adds nothing useful IMO. That's more of an example of things not to do (use assignments as conditions in awk).
                      – Stéphane Chazelas
                      11 hours ago





                      But it adds nothing useful IMO. That's more of an example of things not to do (use assignments as conditions in awk).
                      – Stéphane Chazelas
                      11 hours ago













                      Note that <Turtle> is not portable and would match on Red-Turtle. You would need something like sed -n 's/^[[:blank:]]*([^[:blank:]]1,)[[:blank:]]1,Turtle([[:blank:]].*)0,1$/1/p' for a real equivalent.
                      – Stéphane Chazelas
                      11 hours ago




                      Note that <Turtle> is not portable and would match on Red-Turtle. You would need something like sed -n 's/^[[:blank:]]*([^[:blank:]]1,)[[:blank:]]1,Turtle([[:blank:]].*)0,1$/1/p' for a real equivalent.
                      – Stéphane Chazelas
                      11 hours ago












                      @Kusalananda: Also, of course, your sed solution would match “Turtle” appearing in any column.
                      – G-Man
                      6 hours ago




                      @Kusalananda: Also, of course, your sed solution would match “Turtle” appearing in any column.
                      – G-Man
                      6 hours ago










                      up vote
                      3
                      down vote













                      non-awk alternative:



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






                      share|improve this answer
















                      • 1




                        But this would match “Turtle” (or even something like ‘‘Turtle#1998’’ or ‘‘Mock-Turtle’’) appearing in any column.
                        – G-Man
                        6 hours ago














                      up vote
                      3
                      down vote













                      non-awk alternative:



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






                      share|improve this answer
















                      • 1




                        But this would match “Turtle” (or even something like ‘‘Turtle#1998’’ or ‘‘Mock-Turtle’’) appearing in any column.
                        – G-Man
                        6 hours ago












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









                      RobotJohnny

                      740216




                      740216







                      • 1




                        But this would match “Turtle” (or even something like ‘‘Turtle#1998’’ or ‘‘Mock-Turtle’’) appearing in any column.
                        – G-Man
                        6 hours ago












                      • 1




                        But this would match “Turtle” (or even something like ‘‘Turtle#1998’’ or ‘‘Mock-Turtle’’) appearing in any column.
                        – G-Man
                        6 hours ago







                      1




                      1




                      But this would match “Turtle” (or even something like ‘‘Turtle#1998’’ or ‘‘Mock-Turtle’’) appearing in any column.
                      – G-Man
                      6 hours ago




                      But this would match “Turtle” (or even something like ‘‘Turtle#1998’’ or ‘‘Mock-Turtle’’) appearing in any column.
                      – G-Man
                      6 hours ago










                      up vote
                      1
                      down vote













                      You may employ grep in this:



                       grep -oP '^d+(?=h+Turtleh)'





                      share|improve this answer




















                      • We expect answers as complex/cryptic as this  to include an explanation.
                        – G-Man
                        6 hours ago














                      up vote
                      1
                      down vote













                      You may employ grep in this:



                       grep -oP '^d+(?=h+Turtleh)'





                      share|improve this answer




















                      • We expect answers as complex/cryptic as this  to include an explanation.
                        – G-Man
                        6 hours ago












                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote









                      You may employ grep in this:



                       grep -oP '^d+(?=h+Turtleh)'





                      share|improve this answer












                      You may employ grep in this:



                       grep -oP '^d+(?=h+Turtleh)'






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 11 hours ago









                      Rakesh Sharma

                      217113




                      217113











                      • We expect answers as complex/cryptic as this  to include an explanation.
                        – G-Man
                        6 hours ago
















                      • We expect answers as complex/cryptic as this  to include an explanation.
                        – G-Man
                        6 hours ago















                      We expect answers as complex/cryptic as this  to include an explanation.
                      – G-Man
                      6 hours ago




                      We expect answers as complex/cryptic as this  to include an explanation.
                      – G-Man
                      6 hours ago










                      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-mth-first-column-value-for-line-with-specific-nth-second-column%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      Long meetings (6-7 hours a day): Being “babysat” by supervisor

                      Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                      Confectionery