Search for exact string in column [duplicate]











up vote
2
down vote

favorite













This question already has an answer here:




  • How to use grep on column?

    3 answers



  • How can I extract/change lines in a text file whose data are separated into fields?

    1 answer




I have a file with words separated by ',' on lines, every line has the same number of words, say 4. So ther are in the form of:



something1,something2,something3,something4



I want to search for the line that contains on the 4th column exactly something4, but how do i do that if there exists another line that is something like this:



something1,something2,something3,1_something4



with grep I will get both these lines, but I only want the line that has on the 4th element exactly something4 what should I do?










share|improve this question















marked as duplicate by thrig, Jeff Schaller, elbarna, JigglyNaga, schily Nov 22 at 17:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • You might be interested in my answer here: How can I extract/change lines in a text file whose data are separated into fields?
    – terdon
    Nov 22 at 11:17















up vote
2
down vote

favorite













This question already has an answer here:




  • How to use grep on column?

    3 answers



  • How can I extract/change lines in a text file whose data are separated into fields?

    1 answer




I have a file with words separated by ',' on lines, every line has the same number of words, say 4. So ther are in the form of:



something1,something2,something3,something4



I want to search for the line that contains on the 4th column exactly something4, but how do i do that if there exists another line that is something like this:



something1,something2,something3,1_something4



with grep I will get both these lines, but I only want the line that has on the 4th element exactly something4 what should I do?










share|improve this question















marked as duplicate by thrig, Jeff Schaller, elbarna, JigglyNaga, schily Nov 22 at 17:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • You might be interested in my answer here: How can I extract/change lines in a text file whose data are separated into fields?
    – terdon
    Nov 22 at 11:17













up vote
2
down vote

favorite









up vote
2
down vote

favorite












This question already has an answer here:




  • How to use grep on column?

    3 answers



  • How can I extract/change lines in a text file whose data are separated into fields?

    1 answer




I have a file with words separated by ',' on lines, every line has the same number of words, say 4. So ther are in the form of:



something1,something2,something3,something4



I want to search for the line that contains on the 4th column exactly something4, but how do i do that if there exists another line that is something like this:



something1,something2,something3,1_something4



with grep I will get both these lines, but I only want the line that has on the 4th element exactly something4 what should I do?










share|improve this question
















This question already has an answer here:




  • How to use grep on column?

    3 answers



  • How can I extract/change lines in a text file whose data are separated into fields?

    1 answer




I have a file with words separated by ',' on lines, every line has the same number of words, say 4. So ther are in the form of:



something1,something2,something3,something4



I want to search for the line that contains on the 4th column exactly something4, but how do i do that if there exists another line that is something like this:



something1,something2,something3,1_something4



with grep I will get both these lines, but I only want the line that has on the 4th element exactly something4 what should I do?





This question already has an answer here:




  • How to use grep on column?

    3 answers



  • How can I extract/change lines in a text file whose data are separated into fields?

    1 answer








text-processing grep






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 19:48









Jesse_b

11.4k23063




11.4k23063










asked Nov 21 at 19:35









C. Cristi

1647




1647




marked as duplicate by thrig, Jeff Schaller, elbarna, JigglyNaga, schily Nov 22 at 17:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by thrig, Jeff Schaller, elbarna, JigglyNaga, schily Nov 22 at 17:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • You might be interested in my answer here: How can I extract/change lines in a text file whose data are separated into fields?
    – terdon
    Nov 22 at 11:17


















  • You might be interested in my answer here: How can I extract/change lines in a text file whose data are separated into fields?
    – terdon
    Nov 22 at 11:17
















You might be interested in my answer here: How can I extract/change lines in a text file whose data are separated into fields?
– terdon
Nov 22 at 11:17




You might be interested in my answer here: How can I extract/change lines in a text file whose data are separated into fields?
– terdon
Nov 22 at 11:17










3 Answers
3






active

oldest

votes

















up vote
10
down vote



accepted










You can use awk for this:



awk -F, '$4 == "something4"' file.csv


This should print the entire line for any line where the 4th column is exactly something4





In order to pass a variable into awk you would need to do the following:



var1=$(echo "something,something4" | cut -f2 -d,)
awk -F, -vsearch="$var1" '$4 == search' file.csv





share|improve this answer























  • Why doesn't this work if I do if with a variable?
    – C. Cristi
    Nov 21 at 20:21










  • Say I have var1=$(echo "something,something4" | cut -f2 -d,) and I try to awk -F, '$4 == "$var1"' file.csv but if I go and ``` echo "$var1"``` I get the good result, and there exits something4 in file.csv
    – C. Cristi
    Nov 21 at 20:22










  • @C.Cristi: I have updated my answer. Let me know if it doesn't work for you.
    – Jesse_b
    Nov 21 at 20:24










  • It works, but why?
    – C. Cristi
    Nov 21 at 20:26






  • 1




    Your shell variable won't expand inside the single quotes around the awk command. So awk has the -v option to pass variables into the awk program.
    – Jesse_b
    Nov 21 at 20:28


















up vote
10
down vote













Or, since there's only 4 columns and you want something specific in the last one,



grep ',something4$' < input


(posted in case you're actually OK with grep; awk is a great solution here).






share|improve this answer




























    up vote
    0
    down vote













    Generally, for this type of problem you can use cut to get only the column you want, then grep on that.



    echo something1,something2,something3,something4 | cut -d , -f 4 | grep '^something4$'






    share|improve this answer

















    • 1




      The only problem with this is the assumption that the OP wants to find the line that matches.
      – Jeff Schaller
      Nov 23 at 12:00










    • You're right. cut would only be useful for checking for match/no match or counting matches. Or finding all column values that match a regexp.
      – Lassi
      Nov 23 at 15:17


















    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    10
    down vote



    accepted










    You can use awk for this:



    awk -F, '$4 == "something4"' file.csv


    This should print the entire line for any line where the 4th column is exactly something4





    In order to pass a variable into awk you would need to do the following:



    var1=$(echo "something,something4" | cut -f2 -d,)
    awk -F, -vsearch="$var1" '$4 == search' file.csv





    share|improve this answer























    • Why doesn't this work if I do if with a variable?
      – C. Cristi
      Nov 21 at 20:21










    • Say I have var1=$(echo "something,something4" | cut -f2 -d,) and I try to awk -F, '$4 == "$var1"' file.csv but if I go and ``` echo "$var1"``` I get the good result, and there exits something4 in file.csv
      – C. Cristi
      Nov 21 at 20:22










    • @C.Cristi: I have updated my answer. Let me know if it doesn't work for you.
      – Jesse_b
      Nov 21 at 20:24










    • It works, but why?
      – C. Cristi
      Nov 21 at 20:26






    • 1




      Your shell variable won't expand inside the single quotes around the awk command. So awk has the -v option to pass variables into the awk program.
      – Jesse_b
      Nov 21 at 20:28















    up vote
    10
    down vote



    accepted










    You can use awk for this:



    awk -F, '$4 == "something4"' file.csv


    This should print the entire line for any line where the 4th column is exactly something4





    In order to pass a variable into awk you would need to do the following:



    var1=$(echo "something,something4" | cut -f2 -d,)
    awk -F, -vsearch="$var1" '$4 == search' file.csv





    share|improve this answer























    • Why doesn't this work if I do if with a variable?
      – C. Cristi
      Nov 21 at 20:21










    • Say I have var1=$(echo "something,something4" | cut -f2 -d,) and I try to awk -F, '$4 == "$var1"' file.csv but if I go and ``` echo "$var1"``` I get the good result, and there exits something4 in file.csv
      – C. Cristi
      Nov 21 at 20:22










    • @C.Cristi: I have updated my answer. Let me know if it doesn't work for you.
      – Jesse_b
      Nov 21 at 20:24










    • It works, but why?
      – C. Cristi
      Nov 21 at 20:26






    • 1




      Your shell variable won't expand inside the single quotes around the awk command. So awk has the -v option to pass variables into the awk program.
      – Jesse_b
      Nov 21 at 20:28













    up vote
    10
    down vote



    accepted







    up vote
    10
    down vote



    accepted






    You can use awk for this:



    awk -F, '$4 == "something4"' file.csv


    This should print the entire line for any line where the 4th column is exactly something4





    In order to pass a variable into awk you would need to do the following:



    var1=$(echo "something,something4" | cut -f2 -d,)
    awk -F, -vsearch="$var1" '$4 == search' file.csv





    share|improve this answer














    You can use awk for this:



    awk -F, '$4 == "something4"' file.csv


    This should print the entire line for any line where the 4th column is exactly something4





    In order to pass a variable into awk you would need to do the following:



    var1=$(echo "something,something4" | cut -f2 -d,)
    awk -F, -vsearch="$var1" '$4 == search' file.csv






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 21 at 20:24

























    answered Nov 21 at 19:39









    Jesse_b

    11.4k23063




    11.4k23063












    • Why doesn't this work if I do if with a variable?
      – C. Cristi
      Nov 21 at 20:21










    • Say I have var1=$(echo "something,something4" | cut -f2 -d,) and I try to awk -F, '$4 == "$var1"' file.csv but if I go and ``` echo "$var1"``` I get the good result, and there exits something4 in file.csv
      – C. Cristi
      Nov 21 at 20:22










    • @C.Cristi: I have updated my answer. Let me know if it doesn't work for you.
      – Jesse_b
      Nov 21 at 20:24










    • It works, but why?
      – C. Cristi
      Nov 21 at 20:26






    • 1




      Your shell variable won't expand inside the single quotes around the awk command. So awk has the -v option to pass variables into the awk program.
      – Jesse_b
      Nov 21 at 20:28


















    • Why doesn't this work if I do if with a variable?
      – C. Cristi
      Nov 21 at 20:21










    • Say I have var1=$(echo "something,something4" | cut -f2 -d,) and I try to awk -F, '$4 == "$var1"' file.csv but if I go and ``` echo "$var1"``` I get the good result, and there exits something4 in file.csv
      – C. Cristi
      Nov 21 at 20:22










    • @C.Cristi: I have updated my answer. Let me know if it doesn't work for you.
      – Jesse_b
      Nov 21 at 20:24










    • It works, but why?
      – C. Cristi
      Nov 21 at 20:26






    • 1




      Your shell variable won't expand inside the single quotes around the awk command. So awk has the -v option to pass variables into the awk program.
      – Jesse_b
      Nov 21 at 20:28
















    Why doesn't this work if I do if with a variable?
    – C. Cristi
    Nov 21 at 20:21




    Why doesn't this work if I do if with a variable?
    – C. Cristi
    Nov 21 at 20:21












    Say I have var1=$(echo "something,something4" | cut -f2 -d,) and I try to awk -F, '$4 == "$var1"' file.csv but if I go and ``` echo "$var1"``` I get the good result, and there exits something4 in file.csv
    – C. Cristi
    Nov 21 at 20:22




    Say I have var1=$(echo "something,something4" | cut -f2 -d,) and I try to awk -F, '$4 == "$var1"' file.csv but if I go and ``` echo "$var1"``` I get the good result, and there exits something4 in file.csv
    – C. Cristi
    Nov 21 at 20:22












    @C.Cristi: I have updated my answer. Let me know if it doesn't work for you.
    – Jesse_b
    Nov 21 at 20:24




    @C.Cristi: I have updated my answer. Let me know if it doesn't work for you.
    – Jesse_b
    Nov 21 at 20:24












    It works, but why?
    – C. Cristi
    Nov 21 at 20:26




    It works, but why?
    – C. Cristi
    Nov 21 at 20:26




    1




    1




    Your shell variable won't expand inside the single quotes around the awk command. So awk has the -v option to pass variables into the awk program.
    – Jesse_b
    Nov 21 at 20:28




    Your shell variable won't expand inside the single quotes around the awk command. So awk has the -v option to pass variables into the awk program.
    – Jesse_b
    Nov 21 at 20:28












    up vote
    10
    down vote













    Or, since there's only 4 columns and you want something specific in the last one,



    grep ',something4$' < input


    (posted in case you're actually OK with grep; awk is a great solution here).






    share|improve this answer

























      up vote
      10
      down vote













      Or, since there's only 4 columns and you want something specific in the last one,



      grep ',something4$' < input


      (posted in case you're actually OK with grep; awk is a great solution here).






      share|improve this answer























        up vote
        10
        down vote










        up vote
        10
        down vote









        Or, since there's only 4 columns and you want something specific in the last one,



        grep ',something4$' < input


        (posted in case you're actually OK with grep; awk is a great solution here).






        share|improve this answer












        Or, since there's only 4 columns and you want something specific in the last one,



        grep ',something4$' < input


        (posted in case you're actually OK with grep; awk is a great solution here).







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 at 19:45









        Jeff Schaller

        36.8k1052121




        36.8k1052121






















            up vote
            0
            down vote













            Generally, for this type of problem you can use cut to get only the column you want, then grep on that.



            echo something1,something2,something3,something4 | cut -d , -f 4 | grep '^something4$'






            share|improve this answer

















            • 1




              The only problem with this is the assumption that the OP wants to find the line that matches.
              – Jeff Schaller
              Nov 23 at 12:00










            • You're right. cut would only be useful for checking for match/no match or counting matches. Or finding all column values that match a regexp.
              – Lassi
              Nov 23 at 15:17















            up vote
            0
            down vote













            Generally, for this type of problem you can use cut to get only the column you want, then grep on that.



            echo something1,something2,something3,something4 | cut -d , -f 4 | grep '^something4$'






            share|improve this answer

















            • 1




              The only problem with this is the assumption that the OP wants to find the line that matches.
              – Jeff Schaller
              Nov 23 at 12:00










            • You're right. cut would only be useful for checking for match/no match or counting matches. Or finding all column values that match a regexp.
              – Lassi
              Nov 23 at 15:17













            up vote
            0
            down vote










            up vote
            0
            down vote









            Generally, for this type of problem you can use cut to get only the column you want, then grep on that.



            echo something1,something2,something3,something4 | cut -d , -f 4 | grep '^something4$'






            share|improve this answer












            Generally, for this type of problem you can use cut to get only the column you want, then grep on that.



            echo something1,something2,something3,something4 | cut -d , -f 4 | grep '^something4$'







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 at 11:58









            Lassi

            26618




            26618








            • 1




              The only problem with this is the assumption that the OP wants to find the line that matches.
              – Jeff Schaller
              Nov 23 at 12:00










            • You're right. cut would only be useful for checking for match/no match or counting matches. Or finding all column values that match a regexp.
              – Lassi
              Nov 23 at 15:17














            • 1




              The only problem with this is the assumption that the OP wants to find the line that matches.
              – Jeff Schaller
              Nov 23 at 12:00










            • You're right. cut would only be useful for checking for match/no match or counting matches. Or finding all column values that match a regexp.
              – Lassi
              Nov 23 at 15:17








            1




            1




            The only problem with this is the assumption that the OP wants to find the line that matches.
            – Jeff Schaller
            Nov 23 at 12:00




            The only problem with this is the assumption that the OP wants to find the line that matches.
            – Jeff Schaller
            Nov 23 at 12:00












            You're right. cut would only be useful for checking for match/no match or counting matches. Or finding all column values that match a regexp.
            – Lassi
            Nov 23 at 15:17




            You're right. cut would only be useful for checking for match/no match or counting matches. Or finding all column values that match a regexp.
            – Lassi
            Nov 23 at 15:17



            Popular posts from this blog

            Ellipse (mathématiques)

            Quarter-circle Tiles

            Mont Emei