Getting only specific data based on name in text file

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











up vote
2
down vote

favorite












Let's say if my text file contains:



101 Adam
201 Clarie
502 Adam
403 Tom


and i want to write a command in the shell to only give me the numbers based of a specific name. For example, only output the number for the name 'Adam' would give:



101
502


I was thinking of something like:



cut -f 1 Data_1 | grep "Adam"


but it doesn't work. Data_1 is the filename. 1 refers to the first column. I'm new to Unix so I'll appreciate some feedback on this.







share|improve this question


























    up vote
    2
    down vote

    favorite












    Let's say if my text file contains:



    101 Adam
    201 Clarie
    502 Adam
    403 Tom


    and i want to write a command in the shell to only give me the numbers based of a specific name. For example, only output the number for the name 'Adam' would give:



    101
    502


    I was thinking of something like:



    cut -f 1 Data_1 | grep "Adam"


    but it doesn't work. Data_1 is the filename. 1 refers to the first column. I'm new to Unix so I'll appreciate some feedback on this.







    share|improve this question
























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      Let's say if my text file contains:



      101 Adam
      201 Clarie
      502 Adam
      403 Tom


      and i want to write a command in the shell to only give me the numbers based of a specific name. For example, only output the number for the name 'Adam' would give:



      101
      502


      I was thinking of something like:



      cut -f 1 Data_1 | grep "Adam"


      but it doesn't work. Data_1 is the filename. 1 refers to the first column. I'm new to Unix so I'll appreciate some feedback on this.







      share|improve this question














      Let's say if my text file contains:



      101 Adam
      201 Clarie
      502 Adam
      403 Tom


      and i want to write a command in the shell to only give me the numbers based of a specific name. For example, only output the number for the name 'Adam' would give:



      101
      502


      I was thinking of something like:



      cut -f 1 Data_1 | grep "Adam"


      but it doesn't work. Data_1 is the filename. 1 refers to the first column. I'm new to Unix so I'll appreciate some feedback on this.









      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 14 '17 at 6:41









      jimmij

      29k867100




      29k867100










      asked Oct 14 '17 at 6:25









      Electric

      132




      132




















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          First you have the order of grep/cut backwards. And, unless those are actual tabs (as in Tab) separating your columns (I can't tell) you also need to specify that normal whitespace (as in Space) is your delimeter.



          grep Adam Data_1 | cut -f1 -d' '


          If you are using tabs then leave off -d' '.



          Generally speaking try one thing at a time while building a compound command like this. What do you see when you do cut alone? Does it look like applying grep to it makes sense? If not then rethink things.



          And always give the man page for each command a good read.




          Bonus: Here's a sed command to do the same thing:



          sed -n 's/^(.*)t+Adam$/1/p' Data_1


          This goes through each line in the file but prints only those that end with one or more tabs and your search string. Then, before printing, it strips off those same tabs and search string.






          share|improve this answer






















          • @B Layer yes those are tabs. if so, i won't need to specify the delimiter am i correct?
            – Electric
            Oct 14 '17 at 6:37










          • Right. If you look at the -d flag in man cut you'll see that the default is tab so nothing extra required.
            – B Layer
            Oct 14 '17 at 6:38

















          up vote
          3
          down vote













          You can use single grep with perl look around extension via -P option:



          grep -Po '.*(?=Adam)' file


          That will print everything in the line up to the word Adam.



          If you want only numbers, excluding whitespaces etc, then:



          grep -Po '[0-9]*(?=.*Adam)' file





          share|improve this answer



























            up vote
            3
            down vote













            Would suggest to use awk for this



            • Default input field separator is one or more consecutive white-spaces, so no need to worry about space/Tab separations

            • Easier to compare fixed strings (i.e less prone to regex meta characters - need to take care in constructing string with quotes and the backslash character. See also Escape Sequences)




            $ cat ip.txt
            101 Adam
            201 Clarie
            502 Adam
            403 Tom

            $ # check if 2nd column value is Adam (wont match MAdam, Adama, etc)
            $ # use $NF instead of $2 to check against last column
            $ awk '$2=="Adam"print $1' ip.txt
            101
            502

            $ # using variable instead of string constant
            $ awk -v name='Adam' '$2==nameprint $1' ip.txt
            101
            502

            $ # passing shell variable
            $ n='Tom'
            $ awk -v name="$n" '$2==nameprint $1' ip.txt
            403





            share|improve this answer


















            • 1




              (speaking of meta characters, note that using -v would still cause problems with backslash characters. Compare awk -v name='foobar' '$1 == name' with name='foobar' awk '$1 == ENVIRON["name"]')
              – Stéphane Chazelas
              Oct 14 '17 at 10:10











            • good point about backslash characters :)
              – Sundeep
              Oct 14 '17 at 10:31

















            up vote
            2
            down vote













            Simple approach with Awk:



            awk '/ Adam$/ print $1' file 
            101
            502





            share|improve this answer






















            • This will match any line with Adam. What if someone had a name like "Adama" or "Adamina" or "Adamantite" ;) (I'm sure I've seen one of the first two before). For the sake of a beginner OP a stricter search criteria might be a good idea. Even just ending it with $.
              – B Layer
              Oct 14 '17 at 7:10











            • @BLayer I was just going off the sample input; updated to handle the cases you mention.
              – jasonwryan
              Oct 14 '17 at 7:24






            • 1




              Totally hear you w/r/t the sample input. It's good to go the extra mile for inexperienced users is my thinking. Thanks for the edit.
              – B Layer
              Oct 14 '17 at 7:30










            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%2f398064%2fgetting-only-specific-data-based-on-name-in-text-file%23new-answer', 'question_page');

            );

            Post as a guest






























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            First you have the order of grep/cut backwards. And, unless those are actual tabs (as in Tab) separating your columns (I can't tell) you also need to specify that normal whitespace (as in Space) is your delimeter.



            grep Adam Data_1 | cut -f1 -d' '


            If you are using tabs then leave off -d' '.



            Generally speaking try one thing at a time while building a compound command like this. What do you see when you do cut alone? Does it look like applying grep to it makes sense? If not then rethink things.



            And always give the man page for each command a good read.




            Bonus: Here's a sed command to do the same thing:



            sed -n 's/^(.*)t+Adam$/1/p' Data_1


            This goes through each line in the file but prints only those that end with one or more tabs and your search string. Then, before printing, it strips off those same tabs and search string.






            share|improve this answer






















            • @B Layer yes those are tabs. if so, i won't need to specify the delimiter am i correct?
              – Electric
              Oct 14 '17 at 6:37










            • Right. If you look at the -d flag in man cut you'll see that the default is tab so nothing extra required.
              – B Layer
              Oct 14 '17 at 6:38














            up vote
            2
            down vote



            accepted










            First you have the order of grep/cut backwards. And, unless those are actual tabs (as in Tab) separating your columns (I can't tell) you also need to specify that normal whitespace (as in Space) is your delimeter.



            grep Adam Data_1 | cut -f1 -d' '


            If you are using tabs then leave off -d' '.



            Generally speaking try one thing at a time while building a compound command like this. What do you see when you do cut alone? Does it look like applying grep to it makes sense? If not then rethink things.



            And always give the man page for each command a good read.




            Bonus: Here's a sed command to do the same thing:



            sed -n 's/^(.*)t+Adam$/1/p' Data_1


            This goes through each line in the file but prints only those that end with one or more tabs and your search string. Then, before printing, it strips off those same tabs and search string.






            share|improve this answer






















            • @B Layer yes those are tabs. if so, i won't need to specify the delimiter am i correct?
              – Electric
              Oct 14 '17 at 6:37










            • Right. If you look at the -d flag in man cut you'll see that the default is tab so nothing extra required.
              – B Layer
              Oct 14 '17 at 6:38












            up vote
            2
            down vote



            accepted







            up vote
            2
            down vote



            accepted






            First you have the order of grep/cut backwards. And, unless those are actual tabs (as in Tab) separating your columns (I can't tell) you also need to specify that normal whitespace (as in Space) is your delimeter.



            grep Adam Data_1 | cut -f1 -d' '


            If you are using tabs then leave off -d' '.



            Generally speaking try one thing at a time while building a compound command like this. What do you see when you do cut alone? Does it look like applying grep to it makes sense? If not then rethink things.



            And always give the man page for each command a good read.




            Bonus: Here's a sed command to do the same thing:



            sed -n 's/^(.*)t+Adam$/1/p' Data_1


            This goes through each line in the file but prints only those that end with one or more tabs and your search string. Then, before printing, it strips off those same tabs and search string.






            share|improve this answer














            First you have the order of grep/cut backwards. And, unless those are actual tabs (as in Tab) separating your columns (I can't tell) you also need to specify that normal whitespace (as in Space) is your delimeter.



            grep Adam Data_1 | cut -f1 -d' '


            If you are using tabs then leave off -d' '.



            Generally speaking try one thing at a time while building a compound command like this. What do you see when you do cut alone? Does it look like applying grep to it makes sense? If not then rethink things.



            And always give the man page for each command a good read.




            Bonus: Here's a sed command to do the same thing:



            sed -n 's/^(.*)t+Adam$/1/p' Data_1


            This goes through each line in the file but prints only those that end with one or more tabs and your search string. Then, before printing, it strips off those same tabs and search string.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Oct 14 '17 at 6:54

























            answered Oct 14 '17 at 6:30









            B Layer

            3,9241525




            3,9241525











            • @B Layer yes those are tabs. if so, i won't need to specify the delimiter am i correct?
              – Electric
              Oct 14 '17 at 6:37










            • Right. If you look at the -d flag in man cut you'll see that the default is tab so nothing extra required.
              – B Layer
              Oct 14 '17 at 6:38
















            • @B Layer yes those are tabs. if so, i won't need to specify the delimiter am i correct?
              – Electric
              Oct 14 '17 at 6:37










            • Right. If you look at the -d flag in man cut you'll see that the default is tab so nothing extra required.
              – B Layer
              Oct 14 '17 at 6:38















            @B Layer yes those are tabs. if so, i won't need to specify the delimiter am i correct?
            – Electric
            Oct 14 '17 at 6:37




            @B Layer yes those are tabs. if so, i won't need to specify the delimiter am i correct?
            – Electric
            Oct 14 '17 at 6:37












            Right. If you look at the -d flag in man cut you'll see that the default is tab so nothing extra required.
            – B Layer
            Oct 14 '17 at 6:38




            Right. If you look at the -d flag in man cut you'll see that the default is tab so nothing extra required.
            – B Layer
            Oct 14 '17 at 6:38












            up vote
            3
            down vote













            You can use single grep with perl look around extension via -P option:



            grep -Po '.*(?=Adam)' file


            That will print everything in the line up to the word Adam.



            If you want only numbers, excluding whitespaces etc, then:



            grep -Po '[0-9]*(?=.*Adam)' file





            share|improve this answer
























              up vote
              3
              down vote













              You can use single grep with perl look around extension via -P option:



              grep -Po '.*(?=Adam)' file


              That will print everything in the line up to the word Adam.



              If you want only numbers, excluding whitespaces etc, then:



              grep -Po '[0-9]*(?=.*Adam)' file





              share|improve this answer






















                up vote
                3
                down vote










                up vote
                3
                down vote









                You can use single grep with perl look around extension via -P option:



                grep -Po '.*(?=Adam)' file


                That will print everything in the line up to the word Adam.



                If you want only numbers, excluding whitespaces etc, then:



                grep -Po '[0-9]*(?=.*Adam)' file





                share|improve this answer












                You can use single grep with perl look around extension via -P option:



                grep -Po '.*(?=Adam)' file


                That will print everything in the line up to the word Adam.



                If you want only numbers, excluding whitespaces etc, then:



                grep -Po '[0-9]*(?=.*Adam)' file






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Oct 14 '17 at 6:38









                jimmij

                29k867100




                29k867100




















                    up vote
                    3
                    down vote













                    Would suggest to use awk for this



                    • Default input field separator is one or more consecutive white-spaces, so no need to worry about space/Tab separations

                    • Easier to compare fixed strings (i.e less prone to regex meta characters - need to take care in constructing string with quotes and the backslash character. See also Escape Sequences)




                    $ cat ip.txt
                    101 Adam
                    201 Clarie
                    502 Adam
                    403 Tom

                    $ # check if 2nd column value is Adam (wont match MAdam, Adama, etc)
                    $ # use $NF instead of $2 to check against last column
                    $ awk '$2=="Adam"print $1' ip.txt
                    101
                    502

                    $ # using variable instead of string constant
                    $ awk -v name='Adam' '$2==nameprint $1' ip.txt
                    101
                    502

                    $ # passing shell variable
                    $ n='Tom'
                    $ awk -v name="$n" '$2==nameprint $1' ip.txt
                    403





                    share|improve this answer


















                    • 1




                      (speaking of meta characters, note that using -v would still cause problems with backslash characters. Compare awk -v name='foobar' '$1 == name' with name='foobar' awk '$1 == ENVIRON["name"]')
                      – Stéphane Chazelas
                      Oct 14 '17 at 10:10











                    • good point about backslash characters :)
                      – Sundeep
                      Oct 14 '17 at 10:31














                    up vote
                    3
                    down vote













                    Would suggest to use awk for this



                    • Default input field separator is one or more consecutive white-spaces, so no need to worry about space/Tab separations

                    • Easier to compare fixed strings (i.e less prone to regex meta characters - need to take care in constructing string with quotes and the backslash character. See also Escape Sequences)




                    $ cat ip.txt
                    101 Adam
                    201 Clarie
                    502 Adam
                    403 Tom

                    $ # check if 2nd column value is Adam (wont match MAdam, Adama, etc)
                    $ # use $NF instead of $2 to check against last column
                    $ awk '$2=="Adam"print $1' ip.txt
                    101
                    502

                    $ # using variable instead of string constant
                    $ awk -v name='Adam' '$2==nameprint $1' ip.txt
                    101
                    502

                    $ # passing shell variable
                    $ n='Tom'
                    $ awk -v name="$n" '$2==nameprint $1' ip.txt
                    403





                    share|improve this answer


















                    • 1




                      (speaking of meta characters, note that using -v would still cause problems with backslash characters. Compare awk -v name='foobar' '$1 == name' with name='foobar' awk '$1 == ENVIRON["name"]')
                      – Stéphane Chazelas
                      Oct 14 '17 at 10:10











                    • good point about backslash characters :)
                      – Sundeep
                      Oct 14 '17 at 10:31












                    up vote
                    3
                    down vote










                    up vote
                    3
                    down vote









                    Would suggest to use awk for this



                    • Default input field separator is one or more consecutive white-spaces, so no need to worry about space/Tab separations

                    • Easier to compare fixed strings (i.e less prone to regex meta characters - need to take care in constructing string with quotes and the backslash character. See also Escape Sequences)




                    $ cat ip.txt
                    101 Adam
                    201 Clarie
                    502 Adam
                    403 Tom

                    $ # check if 2nd column value is Adam (wont match MAdam, Adama, etc)
                    $ # use $NF instead of $2 to check against last column
                    $ awk '$2=="Adam"print $1' ip.txt
                    101
                    502

                    $ # using variable instead of string constant
                    $ awk -v name='Adam' '$2==nameprint $1' ip.txt
                    101
                    502

                    $ # passing shell variable
                    $ n='Tom'
                    $ awk -v name="$n" '$2==nameprint $1' ip.txt
                    403





                    share|improve this answer














                    Would suggest to use awk for this



                    • Default input field separator is one or more consecutive white-spaces, so no need to worry about space/Tab separations

                    • Easier to compare fixed strings (i.e less prone to regex meta characters - need to take care in constructing string with quotes and the backslash character. See also Escape Sequences)




                    $ cat ip.txt
                    101 Adam
                    201 Clarie
                    502 Adam
                    403 Tom

                    $ # check if 2nd column value is Adam (wont match MAdam, Adama, etc)
                    $ # use $NF instead of $2 to check against last column
                    $ awk '$2=="Adam"print $1' ip.txt
                    101
                    502

                    $ # using variable instead of string constant
                    $ awk -v name='Adam' '$2==nameprint $1' ip.txt
                    101
                    502

                    $ # passing shell variable
                    $ n='Tom'
                    $ awk -v name="$n" '$2==nameprint $1' ip.txt
                    403






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Oct 14 '17 at 10:40

























                    answered Oct 14 '17 at 9:48









                    Sundeep

                    6,9611826




                    6,9611826







                    • 1




                      (speaking of meta characters, note that using -v would still cause problems with backslash characters. Compare awk -v name='foobar' '$1 == name' with name='foobar' awk '$1 == ENVIRON["name"]')
                      – Stéphane Chazelas
                      Oct 14 '17 at 10:10











                    • good point about backslash characters :)
                      – Sundeep
                      Oct 14 '17 at 10:31












                    • 1




                      (speaking of meta characters, note that using -v would still cause problems with backslash characters. Compare awk -v name='foobar' '$1 == name' with name='foobar' awk '$1 == ENVIRON["name"]')
                      – Stéphane Chazelas
                      Oct 14 '17 at 10:10











                    • good point about backslash characters :)
                      – Sundeep
                      Oct 14 '17 at 10:31







                    1




                    1




                    (speaking of meta characters, note that using -v would still cause problems with backslash characters. Compare awk -v name='foobar' '$1 == name' with name='foobar' awk '$1 == ENVIRON["name"]')
                    – Stéphane Chazelas
                    Oct 14 '17 at 10:10





                    (speaking of meta characters, note that using -v would still cause problems with backslash characters. Compare awk -v name='foobar' '$1 == name' with name='foobar' awk '$1 == ENVIRON["name"]')
                    – Stéphane Chazelas
                    Oct 14 '17 at 10:10













                    good point about backslash characters :)
                    – Sundeep
                    Oct 14 '17 at 10:31




                    good point about backslash characters :)
                    – Sundeep
                    Oct 14 '17 at 10:31










                    up vote
                    2
                    down vote













                    Simple approach with Awk:



                    awk '/ Adam$/ print $1' file 
                    101
                    502





                    share|improve this answer






















                    • This will match any line with Adam. What if someone had a name like "Adama" or "Adamina" or "Adamantite" ;) (I'm sure I've seen one of the first two before). For the sake of a beginner OP a stricter search criteria might be a good idea. Even just ending it with $.
                      – B Layer
                      Oct 14 '17 at 7:10











                    • @BLayer I was just going off the sample input; updated to handle the cases you mention.
                      – jasonwryan
                      Oct 14 '17 at 7:24






                    • 1




                      Totally hear you w/r/t the sample input. It's good to go the extra mile for inexperienced users is my thinking. Thanks for the edit.
                      – B Layer
                      Oct 14 '17 at 7:30














                    up vote
                    2
                    down vote













                    Simple approach with Awk:



                    awk '/ Adam$/ print $1' file 
                    101
                    502





                    share|improve this answer






















                    • This will match any line with Adam. What if someone had a name like "Adama" or "Adamina" or "Adamantite" ;) (I'm sure I've seen one of the first two before). For the sake of a beginner OP a stricter search criteria might be a good idea. Even just ending it with $.
                      – B Layer
                      Oct 14 '17 at 7:10











                    • @BLayer I was just going off the sample input; updated to handle the cases you mention.
                      – jasonwryan
                      Oct 14 '17 at 7:24






                    • 1




                      Totally hear you w/r/t the sample input. It's good to go the extra mile for inexperienced users is my thinking. Thanks for the edit.
                      – B Layer
                      Oct 14 '17 at 7:30












                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    Simple approach with Awk:



                    awk '/ Adam$/ print $1' file 
                    101
                    502





                    share|improve this answer














                    Simple approach with Awk:



                    awk '/ Adam$/ print $1' file 
                    101
                    502






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Oct 14 '17 at 7:23

























                    answered Oct 14 '17 at 7:02









                    jasonwryan

                    47k14127178




                    47k14127178











                    • This will match any line with Adam. What if someone had a name like "Adama" or "Adamina" or "Adamantite" ;) (I'm sure I've seen one of the first two before). For the sake of a beginner OP a stricter search criteria might be a good idea. Even just ending it with $.
                      – B Layer
                      Oct 14 '17 at 7:10











                    • @BLayer I was just going off the sample input; updated to handle the cases you mention.
                      – jasonwryan
                      Oct 14 '17 at 7:24






                    • 1




                      Totally hear you w/r/t the sample input. It's good to go the extra mile for inexperienced users is my thinking. Thanks for the edit.
                      – B Layer
                      Oct 14 '17 at 7:30
















                    • This will match any line with Adam. What if someone had a name like "Adama" or "Adamina" or "Adamantite" ;) (I'm sure I've seen one of the first two before). For the sake of a beginner OP a stricter search criteria might be a good idea. Even just ending it with $.
                      – B Layer
                      Oct 14 '17 at 7:10











                    • @BLayer I was just going off the sample input; updated to handle the cases you mention.
                      – jasonwryan
                      Oct 14 '17 at 7:24






                    • 1




                      Totally hear you w/r/t the sample input. It's good to go the extra mile for inexperienced users is my thinking. Thanks for the edit.
                      – B Layer
                      Oct 14 '17 at 7:30















                    This will match any line with Adam. What if someone had a name like "Adama" or "Adamina" or "Adamantite" ;) (I'm sure I've seen one of the first two before). For the sake of a beginner OP a stricter search criteria might be a good idea. Even just ending it with $.
                    – B Layer
                    Oct 14 '17 at 7:10





                    This will match any line with Adam. What if someone had a name like "Adama" or "Adamina" or "Adamantite" ;) (I'm sure I've seen one of the first two before). For the sake of a beginner OP a stricter search criteria might be a good idea. Even just ending it with $.
                    – B Layer
                    Oct 14 '17 at 7:10













                    @BLayer I was just going off the sample input; updated to handle the cases you mention.
                    – jasonwryan
                    Oct 14 '17 at 7:24




                    @BLayer I was just going off the sample input; updated to handle the cases you mention.
                    – jasonwryan
                    Oct 14 '17 at 7:24




                    1




                    1




                    Totally hear you w/r/t the sample input. It's good to go the extra mile for inexperienced users is my thinking. Thanks for the edit.
                    – B Layer
                    Oct 14 '17 at 7:30




                    Totally hear you w/r/t the sample input. It's good to go the extra mile for inexperienced users is my thinking. Thanks for the edit.
                    – B Layer
                    Oct 14 '17 at 7:30

















                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f398064%2fgetting-only-specific-data-based-on-name-in-text-file%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?

                    Bahrain

                    Postfix configuration issue with fips on centos 7; mailgun relay