Extracting lines that contain an exact phrase that includes a tab

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 that contains the following lines



50[tab]H[tab]1
44[tab]H[tab]2
4[tab]H[tab]3
4[tab]H[tab]4
44[tab]H[tab]5
234[tab]H[tab]6


I would like to only extract the last line that starts with the exact phrase "4[tab]H" (so this would be "4[tab]H[tab]4") into another file. I have tried:



grep "^4*.H" filein.in | tail -1 >> fileout.out


but instead it extracts "44[tab]H[tab]5". I need it to extract the last line that starts with the exact string: "4[tab]H".







share|improve this question
























    up vote
    0
    down vote

    favorite












    I have a file that contains the following lines



    50[tab]H[tab]1
    44[tab]H[tab]2
    4[tab]H[tab]3
    4[tab]H[tab]4
    44[tab]H[tab]5
    234[tab]H[tab]6


    I would like to only extract the last line that starts with the exact phrase "4[tab]H" (so this would be "4[tab]H[tab]4") into another file. I have tried:



    grep "^4*.H" filein.in | tail -1 >> fileout.out


    but instead it extracts "44[tab]H[tab]5". I need it to extract the last line that starts with the exact string: "4[tab]H".







    share|improve this question






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a file that contains the following lines



      50[tab]H[tab]1
      44[tab]H[tab]2
      4[tab]H[tab]3
      4[tab]H[tab]4
      44[tab]H[tab]5
      234[tab]H[tab]6


      I would like to only extract the last line that starts with the exact phrase "4[tab]H" (so this would be "4[tab]H[tab]4") into another file. I have tried:



      grep "^4*.H" filein.in | tail -1 >> fileout.out


      but instead it extracts "44[tab]H[tab]5". I need it to extract the last line that starts with the exact string: "4[tab]H".







      share|improve this question












      I have a file that contains the following lines



      50[tab]H[tab]1
      44[tab]H[tab]2
      4[tab]H[tab]3
      4[tab]H[tab]4
      44[tab]H[tab]5
      234[tab]H[tab]6


      I would like to only extract the last line that starts with the exact phrase "4[tab]H" (so this would be "4[tab]H[tab]4") into another file. I have tried:



      grep "^4*.H" filein.in | tail -1 >> fileout.out


      but instead it extracts "44[tab]H[tab]5". I need it to extract the last line that starts with the exact string: "4[tab]H".









      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 31 at 23:48









      A. B

      224




      224




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          In ksh93/zsh/bash/mksh/FreeBSD sh:



          grep $'^4tH' filein.in | tail -n 1


          Or in any shell:



          awk '/^4tH/ line=$0; ; END if(line!="") print line; ' filein.in





          share|improve this answer





























            up vote
            1
            down vote













            With gnu sed



            sed '/^4tH/h;$bA;N;D;:A;x;/^$/d' infile





            share|improve this answer
















            • 2




              Or gsed -n '/^4tH/h;$g;/./p;'
              – Stéphane Chazelas
              Apr 1 at 10:10






            • 1




              Or gsed '/^4tH/h;$!d;g;/./!d'
              – ctac_
              Apr 1 at 10:36

















            up vote
            0
            down vote













            You can also reverse the input file line wise using tac and then get first match





            $ # add another t after H if needed
            $ tac ip.txt | grep -m1 $'^4tH'
            4 H 4

            $ tac ip.txt | awk -F't' '$1=="4" && $2=="H"print; exit'
            4 H 4





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



              );








               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f434760%2fextracting-lines-that-contain-an-exact-phrase-that-includes-a-tab%23new-answer', 'question_page');

              );

              Post as a guest






























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              4
              down vote



              accepted










              In ksh93/zsh/bash/mksh/FreeBSD sh:



              grep $'^4tH' filein.in | tail -n 1


              Or in any shell:



              awk '/^4tH/ line=$0; ; END if(line!="") print line; ' filein.in





              share|improve this answer


























                up vote
                4
                down vote



                accepted










                In ksh93/zsh/bash/mksh/FreeBSD sh:



                grep $'^4tH' filein.in | tail -n 1


                Or in any shell:



                awk '/^4tH/ line=$0; ; END if(line!="") print line; ' filein.in





                share|improve this answer
























                  up vote
                  4
                  down vote



                  accepted







                  up vote
                  4
                  down vote



                  accepted






                  In ksh93/zsh/bash/mksh/FreeBSD sh:



                  grep $'^4tH' filein.in | tail -n 1


                  Or in any shell:



                  awk '/^4tH/ line=$0; ; END if(line!="") print line; ' filein.in





                  share|improve this answer














                  In ksh93/zsh/bash/mksh/FreeBSD sh:



                  grep $'^4tH' filein.in | tail -n 1


                  Or in any shell:



                  awk '/^4tH/ line=$0; ; END if(line!="") print line; ' filein.in






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Apr 1 at 10:08









                  Stéphane Chazelas

                  280k53514846




                  280k53514846










                  answered Mar 31 at 23:56









                  Hauke Laging

                  53.2k1282130




                  53.2k1282130






















                      up vote
                      1
                      down vote













                      With gnu sed



                      sed '/^4tH/h;$bA;N;D;:A;x;/^$/d' infile





                      share|improve this answer
















                      • 2




                        Or gsed -n '/^4tH/h;$g;/./p;'
                        – Stéphane Chazelas
                        Apr 1 at 10:10






                      • 1




                        Or gsed '/^4tH/h;$!d;g;/./!d'
                        – ctac_
                        Apr 1 at 10:36














                      up vote
                      1
                      down vote













                      With gnu sed



                      sed '/^4tH/h;$bA;N;D;:A;x;/^$/d' infile





                      share|improve this answer
















                      • 2




                        Or gsed -n '/^4tH/h;$g;/./p;'
                        – Stéphane Chazelas
                        Apr 1 at 10:10






                      • 1




                        Or gsed '/^4tH/h;$!d;g;/./!d'
                        – ctac_
                        Apr 1 at 10:36












                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote









                      With gnu sed



                      sed '/^4tH/h;$bA;N;D;:A;x;/^$/d' infile





                      share|improve this answer












                      With gnu sed



                      sed '/^4tH/h;$bA;N;D;:A;x;/^$/d' infile






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Apr 1 at 9:52









                      ctac_

                      1,016116




                      1,016116







                      • 2




                        Or gsed -n '/^4tH/h;$g;/./p;'
                        – Stéphane Chazelas
                        Apr 1 at 10:10






                      • 1




                        Or gsed '/^4tH/h;$!d;g;/./!d'
                        – ctac_
                        Apr 1 at 10:36












                      • 2




                        Or gsed -n '/^4tH/h;$g;/./p;'
                        – Stéphane Chazelas
                        Apr 1 at 10:10






                      • 1




                        Or gsed '/^4tH/h;$!d;g;/./!d'
                        – ctac_
                        Apr 1 at 10:36







                      2




                      2




                      Or gsed -n '/^4tH/h;$g;/./p;'
                      – Stéphane Chazelas
                      Apr 1 at 10:10




                      Or gsed -n '/^4tH/h;$g;/./p;'
                      – Stéphane Chazelas
                      Apr 1 at 10:10




                      1




                      1




                      Or gsed '/^4tH/h;$!d;g;/./!d'
                      – ctac_
                      Apr 1 at 10:36




                      Or gsed '/^4tH/h;$!d;g;/./!d'
                      – ctac_
                      Apr 1 at 10:36










                      up vote
                      0
                      down vote













                      You can also reverse the input file line wise using tac and then get first match





                      $ # add another t after H if needed
                      $ tac ip.txt | grep -m1 $'^4tH'
                      4 H 4

                      $ tac ip.txt | awk -F't' '$1=="4" && $2=="H"print; exit'
                      4 H 4





                      share|improve this answer
























                        up vote
                        0
                        down vote













                        You can also reverse the input file line wise using tac and then get first match





                        $ # add another t after H if needed
                        $ tac ip.txt | grep -m1 $'^4tH'
                        4 H 4

                        $ tac ip.txt | awk -F't' '$1=="4" && $2=="H"print; exit'
                        4 H 4





                        share|improve this answer






















                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          You can also reverse the input file line wise using tac and then get first match





                          $ # add another t after H if needed
                          $ tac ip.txt | grep -m1 $'^4tH'
                          4 H 4

                          $ tac ip.txt | awk -F't' '$1=="4" && $2=="H"print; exit'
                          4 H 4





                          share|improve this answer












                          You can also reverse the input file line wise using tac and then get first match





                          $ # add another t after H if needed
                          $ tac ip.txt | grep -m1 $'^4tH'
                          4 H 4

                          $ tac ip.txt | awk -F't' '$1=="4" && $2=="H"print; exit'
                          4 H 4






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Apr 1 at 2:22









                          Sundeep

                          6,9511826




                          6,9511826






















                               

                              draft saved


                              draft discarded


























                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f434760%2fextracting-lines-that-contain-an-exact-phrase-that-includes-a-tab%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?