Find common terms in two text file, xargs, grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find common terms in two text file, xargs, grep
# 1  
Old 10-11-2011
Find common terms in two text file, xargs, grep

Hello,

I'm interested in finding all occurrences of the terms in file1 in file2, which are both csv files. I can do this with a loop but I'm interested in knowing if I can also do it with the help of xargs and grep. What I have tried:

Code:
cat file1 | xargs grep file2

The problem is that grep tries to open the terms in file1 instead of using them as patterns.
# 2  
Old 10-11-2011
Code:
grep -F file1 file2

Code:
fgrep file1 file2

This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 10-11-2011
Thank you, that is so much better! I managed to get grep -f to do what I wanted, was not aware of this.
# 4  
Old 10-11-2011
If you want to search for the patterns from a file ( file1 in your example ) , you must use the "-f" option.

"grep -F" is equivalent to "fgrep" ( treating patterns as fix string and not regex) but differ from "grep -f" (patterns from file ) . you probably want to combine the both.
Code:
grep -F -f file1 file2

or

Code:
fgrep -f file1 file2

This User Gave Thanks to clx For This Post:
# 5  
Old 10-12-2011
Thank you, I used the "-f" option, adding "-F" as well made it significantly faster.

---------- Post updated 10-12-11 at 12:27 AM ---------- Previous update was 10-11-11 at 04:43 AM ----------

I have another question related to this use of grep. If I instead wan to list all items in file1 that does not exist in file2 by negating how can that be done?

I tried the -v flag but it did not do this, am I missing something here or is it not possible to do this with grep alone? I have used -v before with grep but never combined with -f that is new to me.

Code:
grep -v -F -f file1 file2



EDIT:
I managed to get this working with the following script, any comments or tips if there are easier ways to accomplish the same this would be appreciated.

Code:
#!/bin/bash

if [ "$#" -ne 2 ]
then
    echo "Missing arguments"
    exit 1
fi

while read line ; do RESULT=$(grep "$line" "$1") ;

if [ -z "$RESULT" ]
then
     echo "$line"
fi ; done < "$2"


Last edited by eon; 10-12-2011 at 03:11 AM..
# 6  
Old 10-12-2011
Quote:
Originally Posted by eon
If I instead wan to list all items in file1 that does not exist in file2 by negating how can that be done?

I tried the -v flag but it did not do this, am I missing something here or is it not possible to do this with grep alone? I have used -v before with grep but never combined with -f that is new to me.

Code:
grep -v -F -f file1 file2


Code:
grep -vFf file2 file1

# 7  
Old 10-12-2011
That still does not work I'm afraid.

file1 contains 99 lines which all appear in file2.

file2 contains 109 lines.

What I want is to get the 10 lines that does not appear in file1.

Doing this:

Code:
grep -vFf file1 file2 | wc -l

gives me: 109

but switching the order gives me: 0
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Find a file with common initials and last words

Hi, I have a requirement like i have to find out files and remove them on a daily basis. The files are generated as abc_jnfn_201404230004.csv abc_jnfo_201404230004.csv abc_jnfp_201404230004.csv abc_jnfq_201404230004.csv abd_jnfn_201404220004.csv abe_jnfn_201404220004.csv i want to... (1 Reply)
Discussion started by: Mohammed_Tabish
1 Replies

2. UNIX for Dummies Questions & Answers

Grep -B used with -f? (Searching a file using a list of terms, output is lines before each match)

(1 Reply)
Discussion started by: Twinklefingers
1 Replies

3. UNIX for Dummies Questions & Answers

Grep? - using a file of terms to search another file when the information is on a different line

I have a flat file that looks like this, let's call it Chromosome_9.txt: FT /Gene_Name="Guanyl-Acetylase 9" FT /Gene_Number"36952" FT /Gene_Name="Endoplasmic Luciferase" FT /Gene_Number"36953" FT ... (4 Replies)
Discussion started by: Twinklefingers
4 Replies

4. Shell Programming and Scripting

Xargs or Find with output to a file

Hi, I've got to setup a script that will run daily, and find a log file of a certain age, and then compress and transfer this file to a new location. so far i've been able to specify the file i want with: find . -name 'filename.*.log' -mtime 14 -exec compress -vf {} \; this prints out... (4 Replies)
Discussion started by: horhif
4 Replies

5. Shell Programming and Scripting

Grep multiple search terms with context

I have a file that is a sort library in the format: ##def title1 content1 stuff1 content2 stuff2 ##enddef ##def title2 etc.. I want to grep def and content and pull some trailing context from content so the result would look something like: (1 Reply)
Discussion started by: Moe.Wilensky
1 Replies

6. OS X (Apple)

Need Help with GREP REGEX scripts for common BB-EDIT text-editing

Hi Everybody.. I'm a "newbie" to using Command-line... A few half-remembered DOS commands from 30 years ago, and the very handy "Sudo rm -R pathname" REMOVE command... I do a lot of "cleaning" of plain-text OCR text files. with assorted common line-break, punctuation and capitalization... (1 Reply)
Discussion started by: TheMacGuy
1 Replies

7. UNIX for Dummies Questions & Answers

find/xargs/*grep: find multi-line empty "try-catch" blocks - eg, missing ; not in a commented block

How can I recursively find all files in a directory and print out the file and first line number of any text blocks that match the below cases? This would seem to involve find, xargs, *grep, regex, etc. In summary, I want to find so-called empty "try-catch blocks" that do not contain code... (0 Replies)
Discussion started by: lifechamp
0 Replies

8. UNIX for Dummies Questions & Answers

find . -name "*.*" | xargs grep "help"

Hi all, I am a unix noob. Need some basic help. I have tried using google, but not able to figure this out. Here are the scenarios: 1. How do I find a directory with a particular name, say "Merlin" in the entire file system? I tried : find / -type d -name "dir_name" The problem is I'm... (3 Replies)
Discussion started by: neil.k
3 Replies

9. Shell Programming and Scripting

File find | xargs grep for pattern file

Folks I've been struggling this with for far too liong now and need your help! I've been happily using grep for a search of a directory, to list the files which contain a string: find . -type f -mtime -5 -print | xargs grep -l 'invoiceID=\"12345\"' Now the list of 'invoiceID' I am... (4 Replies)
Discussion started by: daveaasmith
4 Replies

10. Shell Programming and Scripting

How to strip out common terms in string

Hi, I have this kinda of data:- 0,0,0,0,1,2,0,4,5,6,7,foo 0,0,0,0,1,4,0,5,5,5,5,foo1 0,0,6,0,1,6,0,6,1,2,3,orange etc... I wanted to remove the 0 which occur on the same rows of foo,foo1 and orange in this case. Desired output is:- 0,1,2,4,5,6,7,foo 0,1,4,5,5,5,5,foo1... (9 Replies)
Discussion started by: ahjiefreak
9 Replies
Login or Register to Ask a Question