0

I have a csv file with many lines and I need to find some matches and out it to a file. My data are like this:

File 1

qwerty
asdfgh
zxcvbn
qwerty
aassdd
zzxxcc
yyuuii
tttttt

I need to match:

File 2

qwert
tttttt
aassdd.

As my file are big and my list for matching is long, I am doing this:

while read n ; do grep $n File_1.csv >> results.csv ; done < File_2.csv

I can't get the result I wanted.

  • 3
    So what is the result you wanted then? Apart from the issues re. read and IFS and backslashes, shouldn't that work? Though you could just use grep -f file2 file1 to read the patterns to be matched directly from a file. – ilkkachu May 30 '17 at 10:02
  • 1
    We can't help you fix a problem if you don't tell us what the problem is. This will depend on your input, your output and the output you expect. If you try your command on the example data you have given us, it will work as expected (well, as I expect, I don't know what you expect since you haven't told us). So, if you don't see what you expect, that's because of your actual data. So please edit your question, tell us what you expect and what is actually happening and give us an example that reproduces the problem. – terdon May 30 '17 at 10:20
3

You don't need to loop; the -f option takes a file with the patterns to search for:

grep -Ff File_2.csv File_1.csv > results.csv

I also added the -F option so the search terms are treated literally, not as regex.

0

If each file has no duplicates then you can do the following:

# In file_1 and file_2
sort file_1 file_2 | uniq -d
# In file_1 or file_2 but not both
sort file_1 file_2 | uniq -u
# In file_1 and not file_2
sort file_1 file_2 | uniq -d | sort - file_1 | uniq -u
# In file_2 and not file_1
sort file_1 file_2 | uniq -d | sort - file_2 | uniq -u
  • They probably wanted some sort of substring or pattern match, since they used grep and one of the patterns in file2 is just qwert. But then again we don't know that since there's no clear mention of the expected output... – ilkkachu May 30 '17 at 12:48
1

The grep utility can read patterns from one file and match them against the contents of another file. No looping in the shell is required.

$ grep -f patterns file

Using the two file that you have in your question (file 1 is file while file 2 is patterns), this produces

qwerty 
qwerty 
tttttt 

To match with the patterns from patterns as fixed strings (not regular expressions), add -F:

$ grep -F -f patterns file

For the example given, this produces the same result as without -F.

To force matching across complete lines, add -x:

$ grep -x -F -f patterns file
tttttt   

Since qwerty does not match qwert completely, those lines are not returned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.