matching strings from different files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting matching strings from different files
# 1  
Old 12-23-2011
matching strings from different files

I want to compare file 1 to file 2 and if a string from file 1 appears in file 2, then print the file 2 row, where the string appears, onto file3.

file 1 looks like this.
Code:
DOG_0004340 blah blah2 j 22424
DOG_3010311 blah blah3 o 24500
DOG_9949221 blah blah6 x 35035

file 2 looks like this:
Code:
2R PROGRAM exo 3455 7943 . + . id "blah_01"; train "DOG_0004340"; "blah";
2R PROGRAM exo 7855 9935 . + . id "blah_02"; train "DOG_0004340"; "blah";
2R PROGRAM exo 7855 9943 . + . id "blah_01"; train "DOG_0004340"; "blah";
2R PROGRAM exo  2565 3673 . + . id "blah_01"; train "DOG_3010311"; "blah";
2R PROGRAM exo  4665 4873 . + . id "blah_01"; train "DOG_3010311"; "blah";

So basically, If the word "DOG_" (plus any numerical variation) from column 1 in file 1 appears in file 2, print the file 2 row that has the corresponding string plus all of its repeats onto file 3.

Does anyone have any ideas?

Moderator's Comments:
Mod Comment Use code tags, see PM, thanks.

Last edited by zaxxon; 12-23-2011 at 05:15 PM.. Reason: code tags
# 2  
Old 12-23-2011
Code:
while read A B; do grep $A f2; done < f1 > f3

# 3  
Old 12-29-2011
Thank you for the reply. However when I put in


Code:
while read A B; do echo $A Untitled.txt; done < Untitled2.txt > output

the output file I get looks like this for example:

Code:
DOG_00092 Untitled.txt
DOG_00082 Untitled.txt
DOG_00024 Untitled.txt

so basically, it is doing part of what I need it to do but instead of printing out the file 2 row that the string is found in, it is only printing out the title of the file.
# 4  
Old 12-29-2011
Code:
 
while read A B; do echo $A Untitled.txt; done < Untitled2.txt > output

it should be grep according to the Zaxxon
# 5  
Old 01-04-2012
hi guys,

I am writing

Code:
while read A B; do echo $A f1; done < f2 > f3

but it is only printing the actual name of the "f1" in f3. so it ends up looking like

Code:
DOG_01 f2
DOG_02 f2
DOG_03 f2
DOG_01 f2

instead of actually printing out what that row should look like in f2. I am using bash shell if that matters?

---------- Post updated at 02:43 PM ---------- Previous update was at 02:40 PM ----------

Oops. I just realized you were greping not echoing. Thanks for your help!
# 6  
Old 01-04-2012
you can try this as well
Code:
grep -f <(awk '{print $1}' f1) f2 > f3


Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 01-05-2012 at 04:20 AM.. Reason: Please use code tags for code and data samples, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to extract the partial matching strings among two files?

I have a two file as shown below, file:1 >Contig_152_415 (REVERSE SENSE) >Contig_152_420 (REVERSE SENSE) >Contig_152_472 (REVERSE SENSE) >Contig_152_484 (REVERSE SENSE) File:2 >Contig_152:49081-49929 ATCGAGCAGCGCCGCGTGCGGTGCACCCTTGTGCAGATCGGGAGTAACCACGCGCACGGC... (2 Replies)
Discussion started by: dineshkumarsrk
2 Replies

2. UNIX for Beginners Questions & Answers

(g)awk: Matching strings from one file in another file between two strings

Hello all, I can get close to what I am looking for but cannot seem to hit it exactly and was wondering if I could get your help. I have the following sample from textfile with many thousands of lines: File 1 PS001,001 HLK PS002,004 L<G PS004,002 XNN PS004,006 BVX PS004,006 ZBX=... (7 Replies)
Discussion started by: jvoot
7 Replies

3. Shell Programming and Scripting

Matching only the strings I provide - sed

Hello.. I am currently learning sed and have found myself in some trouble.. I wrote this command: sed -ne 's/*\(\{2\}*\{2\}*\{2\}*\).*\(\{2\}*\{2\}*\{2\}*\).*/\1\2/p' and some of the output i get is : ->stockholm->paris<-stockholmpi<-tokyo->paris<-stockholmpi... (8 Replies)
Discussion started by: jozo95
8 Replies

4. Shell Programming and Scripting

Need to append matching strings in a file

Hi , I am writing a shell script to check pvsizes in linux box. # for i in `cat vgs1` > do > echo "########### $i ###########" > pvs|grep -i $i|awk '{print $2,$1,$5}'>pvs_$i > pvs|grep -i $i|awk '{print $1}'|while read a > do > fdisk -l $a|head -2|tail -1|awk '{print $2,$3}'>pvs_$i1 >... (3 Replies)
Discussion started by: nanduri
3 Replies

5. Shell Programming and Scripting

Concatenating 2 lines from 2 files having matching strings

Hello All Unix Users, I am still new to Unix, however I am eager to learn it.. I have 2 files, some lines have some matching substrings, I would like to concatenate these lines into one lines, leaving other untouched. Here below is an example for that.. File 1 (fasta file): >292183... (6 Replies)
Discussion started by: Mohamed EL Hadi
6 Replies

6. Shell Programming and Scripting

Picking matching strings

I have a list of file names. However in some instances I might have a "-" at the beginning of the filename or an "=". For example I might have something like this set Lst = "file1 file2 file3 -file4 file5=" I want to pick up the ones having "-" at the beginning or "=" and store them in... (22 Replies)
Discussion started by: kristinu
22 Replies

7. UNIX for Dummies Questions & Answers

Help with finding matching position on strings

I have a DNA file like below and I am able to write a short program which finds/not an input motif, but I dont understand how I can include in the code to report which position the motif was found. Example I want to find the first or all "GAT" motifs and want the program to report which position... (12 Replies)
Discussion started by: pawannoel
12 Replies

8. Programming

help with matching strings

In C programming how do i check if a char is equal to a vowel , like a e i o or u, small or big case. in my function i have the parameter like *word, and i am using word in a for loop, to check if its equal. i use tolower(word)=='a' || ..... but for some reason it only matches on lower case and... (1 Reply)
Discussion started by: omega666
1 Replies

9. Shell Programming and Scripting

Strings not matching

Hi, I have got two variables holding strings, if i echo them, they print the same value but if i compare the condition fails?? can somebody suggest something?? I have checked the word count too, they are also same. Thanks, Atul (4 Replies)
Discussion started by: atulmt
4 Replies

10. UNIX for Dummies Questions & Answers

matching characters between strings

please send the logic or program to find the matching characters between two strings for ex string1 :abc string2 :adc no .of matching characters is 2(a,c) (9 Replies)
Discussion started by: akmtcs
9 Replies
Login or Register to Ask a Question