Search for exact string in column [duplicate]
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?
text-processing grep
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.
add a comment |
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?
text-processing grep
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
add a comment |
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?
text-processing grep
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
text-processing grep
edited Nov 21 '18 at 19:48
Jesse_b
11.9k23064
11.9k23064
asked Nov 21 '18 at 19:35
C. 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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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
Why doesn't this work if I do if with a variable?
– C. Cristi
Nov 21 '18 at 20:21
Say I havevar1=$(echo "something,something4" | cut -f2 -d,)
and I try toawk -F, '$4 == "$var1"' file.csv
but if I go and ``` echo "$var1"``` I get the good result, and there exitssomething4
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
|
show 1 more comment
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).
add a comment |
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$'
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
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
Why doesn't this work if I do if with a variable?
– C. Cristi
Nov 21 '18 at 20:21
Say I havevar1=$(echo "something,something4" | cut -f2 -d,)
and I try toawk -F, '$4 == "$var1"' file.csv
but if I go and ``` echo "$var1"``` I get the good result, and there exitssomething4
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
|
show 1 more comment
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
Why doesn't this work if I do if with a variable?
– C. Cristi
Nov 21 '18 at 20:21
Say I havevar1=$(echo "something,something4" | cut -f2 -d,)
and I try toawk -F, '$4 == "$var1"' file.csv
but if I go and ``` echo "$var1"``` I get the good result, and there exitssomething4
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
|
show 1 more comment
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
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
edited Nov 21 '18 at 20:24
answered Nov 21 '18 at 19:39
Jesse_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 havevar1=$(echo "something,something4" | cut -f2 -d,)
and I try toawk -F, '$4 == "$var1"' file.csv
but if I go and ``` echo "$var1"``` I get the good result, and there exitssomething4
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
|
show 1 more comment
Why doesn't this work if I do if with a variable?
– C. Cristi
Nov 21 '18 at 20:21
Say I havevar1=$(echo "something,something4" | cut -f2 -d,)
and I try toawk -F, '$4 == "$var1"' file.csv
but if I go and ``` echo "$var1"``` I get the good result, and there exitssomething4
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
|
show 1 more comment
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).
add a comment |
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).
add a comment |
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).
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).
answered Nov 21 '18 at 19:45
Jeff Schaller
39k1053125
39k1053125
add a comment |
add a comment |
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$'
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
add a comment |
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$'
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
add a comment |
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$'
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$'
answered Nov 22 '18 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 '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
add a comment |
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
add a comment |
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