[Solved] Find entries from one file in another


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [Solved] Find entries from one file in another
# 1  
Old 04-21-2013
[Solved] Find entries from one file in another

Please help.
I want to read a file line by line (only 1 column) and print all corresponding rows from a second file (2 columns) . Only first column of second file has to be matched with only column of first file. Order of lines in output doesn't matter.


Simplified example

Code:
Input 1

A
B
Ca
D

Input 2

A Apple
B Bat
Ba Bad
Ax Axe
Ca Cat
C Cab
B Bag

Desired output

A Apple
B Bat
Ca Cat
B Bag

---------- Post updated at 04:42 PM ---------- Previous update was at 04:13 PM ----------

never mind..figured this out myself..thank you ...joy of being a biologist able to do this..

Moderator's Comments:
Mod Comment edit by bakunin: very good! Congrats. We would be overjoyed if you would have posted your solution because this would help all who have the same or a similar problem like you.
I changed the thread's title to [Solved].

Last edited by bakunin; 04-22-2013 at 01:11 PM..
# 2  
Old 04-21-2013
for i in `cat file1`
do
grep $i file2 >>output
done
This User Gave Thanks to zozoo For This Post:
# 3  
Old 04-21-2013
I would highly recommend to avoid using this syntax: for i in `cat file1`

You can find the reason here
These 2 Users Gave Thanks to Yoda For This Post:
# 4  
Old 04-21-2013
Code:
cat file1|while read f;do
grep $f file2 >> output
done

This User Gave Thanks to zozoo For This Post:
# 5  
Old 04-21-2013
That will search for matches in the whole line.
Code:
grep "^$f[[:blank:]]" "$file2"

is more promising.
Most efficient is awk... but this smells a bit like homework?
This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 04-22-2013
Its not homework. I`m trying to extract look-up information for some genes, part of my research. Thank you all.
# 7  
Old 04-22-2013
If you have a look at the man page of "grep" you will notice the "-f" option. Quote from the man-page:

Code:
-f FILE, --file=FILE
	      Obtain  patterns	from  FILE, one per line.  The empty file con-
	      tains zero patterns, and therefore matches nothing.

Therefore, supposing your keys are in file "keys.txt" and you want to search for these keys in "search.txt":

Code:
grep -f keys.txt search.txt

does exactly what you want.

Reading man pages is a very commendable activity. I do it many times day when working with Unix.

I hope this helps.

bakunin
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] Find command is not working

Hello Friends, I have a problem about a little script, when i run the following two lines one by one on CLI then they work well: /usr/bin/mkdir `perl -e 'use POSIX qw(strftime); print strftime "%Y-%m-%d",localtime(time() - 30*24*60*60);'` find . -type f -name "fuseesb.log.*" -mtime 30... (5 Replies)
Discussion started by: EAGL€
5 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Find the newest file in two directories

Hi i have two directories and files inside them Directory 1: Directory 2: These files are the same but song.mp3 in Directory 1 is newer than song.mp3 in Directory 2, and work.txt in Directory 2 is newer than work.txt in Directory 1. Now is my question. How i can compare these files... (10 Replies)
Discussion started by: Falstaff
10 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Using w; how to list find all the users

Hi, On my system, I have about 75 users. Some uers have two or three sessions on the same systems. when I do w, it shows all the sessions. Is there any way to find only the users and not worry about how many sessions. For example: you can see sshe has four connection. I only want to know... (5 Replies)
Discussion started by: samnyc
5 Replies

4. Shell Programming and Scripting

[solved] Problem with find

hi All, I am using the command find /home/mqm/check/ -mtime +1|wc -l to get a count of the files older than a day under the check directory. But instead of getting the count of the files under check directory I m getting a count including the check directory itself. Can someone please tell... (0 Replies)
Discussion started by: jayii
0 Replies

5. Shell Programming and Scripting

find with wild card [solved]

Can somebody help me with the following syntax? I want to find all files that end with *.arc SUFFIX=".arc" find /tmp -name "\*$SUFFIX" -print 2>/dev/null ---------- Post updated at 03:45 PM ---------- Previous update was at 03:41 PM ---------- got it thanks -name... (0 Replies)
Discussion started by: BeefStu
0 Replies

6. UNIX for Dummies Questions & Answers

[Solved] Assistance with find command please

Trying to locate files less than xx days old, throughout all directories/subdirectories, but excluding certain types of directories and files. The directories I want to search all contain the same characteristic (dbdef, pldef, ghdef, etc), and there are subdirectories within that I need to... (2 Replies)
Discussion started by: Condmach
2 Replies

7. Ubuntu

[Solved] Using Find with an exclude/exclude file

I am familiar with using tar and exclude/include files: tar zcf backup.dirs.tgz --files-from=include.mydirs --exclude-from=exclude.mydirs --no-recursion but was wondering if I could use find in the same way. I know that you can just specify the directories to exclude but my list is... (2 Replies)
Discussion started by: metallica1973
2 Replies

8. Shell Programming and Scripting

[solved] merging two files and writing to another file- solved

i have two files as file1: 1 2 3 file2: a b c and the output should be: file3: 1~a 2~b 3~c (1 Reply)
Discussion started by: mlpathir
1 Replies

9. Shell Programming and Scripting

Find Usage solved

How can I find the biggest file in a branch? I tried find / \*.\* | xargs du | sort -n 1,1 | head 1 but shell do nothings :( ---------- Post updated at 03:07 PM ---------- Previous update was at 02:41 PM ---------- Solved: find / -name \*.\* | xargs du | sort -nr | head -n 1 (0 Replies)
Discussion started by: Guccio
0 Replies

10. Shell Programming and Scripting

[Solved] Find Specific records from file and add totals into variables

Hi Eveyone, I am working on one shell script to find the specific records from data file and add the totals into variables and print them. you can find the sample data file below for more clarification. Sample Data File: PXSTYL00__20090803USA CHCART00__20090803IND... (7 Replies)
Discussion started by: veeru
7 Replies
Login or Register to Ask a Question