Search for exact string in column [duplicate]












2















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 '18 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 '18 at 11:17
















2















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 '18 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 '18 at 11:17














2












2








2








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 '18 at 19:48









Jesse_b

11.9k23064




11.9k23064










asked Nov 21 '18 at 19:35









C. CristiC. Cristi

1647




1647




marked as duplicate by thrig, Jeff Schaller, elbarna, JigglyNaga, schily Nov 22 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 11:17










3 Answers
3






active

oldest

votes


















10














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 '18 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 '18 at 20:22










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










  • It works, but why?
    – C. Cristi
    Nov 21 '18 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 '18 at 20:28



















10














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





























    0














    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 '18 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 '18 at 15:17


















    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    10














    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 '18 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 '18 at 20:22










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










    • It works, but why?
      – C. Cristi
      Nov 21 '18 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 '18 at 20:28
















    10














    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 '18 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 '18 at 20:22










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










    • It works, but why?
      – C. Cristi
      Nov 21 '18 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 '18 at 20:28














    10












    10








    10






    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 '18 at 20:24

























    answered Nov 21 '18 at 19:39









    Jesse_bJesse_b

    11.9k23064




    11.9k23064












    • Why doesn't this work if I do if with a variable?
      – C. Cristi
      Nov 21 '18 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 '18 at 20:22










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










    • It works, but why?
      – C. Cristi
      Nov 21 '18 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 '18 at 20:28


















    • Why doesn't this work if I do if with a variable?
      – C. Cristi
      Nov 21 '18 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 '18 at 20:22










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










    • It works, but why?
      – C. Cristi
      Nov 21 '18 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 '18 at 20:28
















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




    Why doesn't this work if I do if with a variable?
    – C. Cristi
    Nov 21 '18 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 '18 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 '18 at 20:22












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




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












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




    It works, but why?
    – C. Cristi
    Nov 21 '18 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 '18 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 '18 at 20:28













    10














    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


























      10














      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
























        10












        10








        10






        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 '18 at 19:45









        Jeff SchallerJeff Schaller

        39k1054125




        39k1054125























            0














            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 '18 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 '18 at 15:17
















            0














            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 '18 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 '18 at 15:17














            0












            0








            0






            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 '18 at 11:58









            LassiLassi

            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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 15:17



            Popular posts from this blog

            Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

            ts Property 'filter' does not exist on type '{}'

            mat-slide-toggle shouldn't change it's state when I click cancel in confirmation window