How to take out multiple matches in file2 using file1 as a reference?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to take out multiple matches in file2 using file1 as a reference?
# 1  
Old 03-14-2012
How to take out multiple matches in file2 using file1 as a reference?

Hi guys,

As the title, I want to take out the lines in file2 if the TCONS_* matches that in file1. Then put the lines in a file3. The TCONS numbers ranges from TCONS_00000024 to TCONS_00381684. Those numbers are continuous in file2, not in file1. Below is just some lines of file1 and file2

File1:
Code:
TCONS_00000284        
TCONS_00198804
TCONS_00198826
TCONS_00067013


File2:

Code:
chr1   exon    65233257        65233407     transcript_id "TCONS_00000284"; 
chr1   exon    65236893        65237073     transcript_id "TCONS_00000284"; 
chr1   exon    65233257        65233407     transcript_id "TCONS_00000285";
chr1   exon    65236893        65237073     transcript_id "TCONS_00000285";


Thanks very much!



Last edited by joeyg; 03-14-2012 at 03:48 PM.. Reason: Please use CodeTags for sample files and scripts
# 2  
Old 03-14-2012
Take a look at the command
Code:
grep -f

# 3  
Old 03-14-2012
Quote:
Originally Posted by joeyg
Take a look at the command
Code:
grep -f

Thanks Joey. But I need to compare all the transcript id in file1 and file2, so I need to use some loops. I'm quite new to perl, could you please give me more details about the codes?
# 4  
Old 03-14-2012
Why perl? grep -f is what you want

Code:
# this makes file3
grep -f file1 file2 > file3
# selectively lose lines in file2 that are in file1
grep -f -v file2 file2 > tmpfile
# read and check that tmpfile is correct.  then
mv tmpfile file2

You don't need perl or loops, joeyg gave you great advice.
# 5  
Old 03-14-2012
Quote:
Originally Posted by jim mcnamara
Why perl? grep -f is what you want

Code:
# this makes file3
grep -f file1 file2 > file3
# selectively lose lines in file2 that are in file1
grep -f -v file2 file2 > tmpfile
# read and check that tmpfile is correct.  then
mv tmpfile file2

You don't need perl or loops, joeyg gave you great advice.
Does the "grep -vf file1 file2>tmpfile" mean move the the lines in file2 that are not in file1 to tmpfile? I don't need those that don't match.
What does the the first line mean?
Thanks very much.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to search field2 in file2 using range of fields file1 and using match to another field in file1

I am trying to use awk to find all the $2 values in file2 which is ~30MB and tab-delimited, that are between $2 and $3 in file1 which is ~2GB and tab-delimited. I have just found out that I need to use $1 and $2 and $3 from file1 and $1 and $2of file2 must match $1 of file1 and be in the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. UNIX for Dummies Questions & Answers

Compare file1 and file2, print matching lines in same order as file1

I want to print only the lines in file2 that match file1, in the same order as they appear in file 1 file1 file2 desired output: I'm getting the lines to match awk 'FNR==NR {a++}; FNR!=NR && a' file1 file2 but they are in sorted order, which is not what I want: Can anyone... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

3. Shell Programming and Scripting

Using regex's from file1, print line and line after matches in file2

Good day, I have a list of regular expressions in file1. For each match in file2, print the containing line and the line after. file1: file2: Output: I can match a regex and print the line and line after awk '{lines = $0} /Macrosiphum_rosae/ {print lines ; print lines } ' ... (1 Reply)
Discussion started by: pathunkathunk
1 Replies

4. Shell Programming and Scripting

If file1 and file2 exist then

HI, I would like a little help on writing a if statement. What i have so far is: #!/bin/bash FILE1=path/to/file1 FILE2=path/to/file2 echo ${FILE1} ${FILE2} if ] then echo file1 and file2 not found else echo FILE ok fi (6 Replies)
Discussion started by: techy1
6 Replies

5. Shell Programming and Scripting

look for line from FILE1 at FILE2

Hi guys! I'm trying to write something to find each line of file1 into file2, if line is found return YES, if not found return NO. The result can be written to a new file. Can you please help me out? FILE1 INPUT: WATER CAR SNAKE (in reality this file has about 600 lines each with a... (2 Replies)
Discussion started by: demmel
2 Replies

6. UNIX for Dummies Questions & Answers

if matching strings in file1 and file2, add column from file1 to file2

I have very limited coding skills but I'm wondering if someone could help me with this. There are many threads about matching strings in two files, but I have no idea how to add a column from one file to another based on a matching string. I'm looking to match column1 in file1 to the number... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

7. Shell Programming and Scripting

grep -f file1 file2

Hi I started to learn bash a week ago. I need filter the strings from the last column of a "file2" that match with a column from an other "file1" file1: chr10100036394-100038350AK077761 chr10100041065-100046547AK032226 chr10100041065-100046547AK016270 chr10100041065-100046547AK078231 ...... (6 Replies)
Discussion started by: geparada88
6 Replies

8. Shell Programming and Scripting

file1 newer then file2

Hello, I am new to shell scripting and i need to create a script with the following directions and I can not figure it out. Create a shell script called newest.bash that takes two filenames as input arguments ($1 and $2) and prints out the name of the newest file (i.e. the file with the... (1 Reply)
Discussion started by: mandylynn78
1 Replies

9. Shell Programming and Scripting

Compare multiple fields in file1 to file2 and print line and next line

Hello, I have two files that I need to compare and print out the line from file2 that has the first 6 fields matching the first 6 fields in file1. Complicating this are the following restrictions 1. file1 is only a few thousand lines at most and file2 is greater than 2 million 2. I need to... (7 Replies)
Discussion started by: gillesc_mac
7 Replies

10. Shell Programming and Scripting

match value from file1 in file2

Hi, i've two files (file1, file2) i want to take value (in column1) and search in file2 if the they match print the value from file2. this is what i have so far. awk 'FILENAME=="file1"{ arr=$1 } FILENAME=="file2" {print $0} ' file1 file2 (2 Replies)
Discussion started by: myguess21
2 Replies
Login or Register to Ask a Question