Match number from 2 input files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match number from 2 input files
# 1  
Old 11-16-2012
Match number from 2 input files

I have 2 input files;

file1:
Code:
1 1000 2000 x1
1 5000 7000 x5
1 8000 10000 x8

file2:
Code:
1 1000 x1 z1
2 1001 x2 z2
1 1999 x3 z3
2 2000 x4 z4
1 2001 x5 z5
1 2002 x6 z6
1 6100 x7 z7
1 6200 x8 z8
1 7500 x9 z9
2 8500 x10 z10

I want to get the output look like this

output file:
Code:
1 1000 x1 z1
1 1999 x3 z3
1 6100 x7 z7
1 6200 x8 z8

In summary : output came from the value of column 1 in file 2 is match with column 1 in file 1 ,then if the value in column 2 of file 2 is in between column 2 and column 3 of file 1 then all data in that line should be keep, otherwise take it out.

Please help me!!!!

Last edited by Franklin52; 11-16-2012 at 03:43 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 11-16-2012
Code:
awk 'FNR==NR{if(($1,1) in start) {start[$1,++cr[$1]]=$2;end[$1,cr[$1]]=$3}
else {start[$1,++cr[$1]]=$2;end[$1,cr[$1]]=$3}
next}
($1,1) in start{
for(i=1;i<=cr[$1];i++)
 if($2>=start[$1,i] && $2<=end[$1,i])
 {
  print
  break
 }
}' file1 file2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Data match 2 files based on first 2 columns matching only and join if match

Hi, i have 2 files , the data i need to match is in masterfile and i need to pull out column 3 from master if column 1 and 2 match and output entire row to new file I have tried with join and awk and i keep getting blank outputs or same file is there an easier way than what i am... (4 Replies)
Discussion started by: axis88
4 Replies

2. UNIX for Beginners Questions & Answers

Count the number of files to delete doesnt match

Good evening, need your help please Need to delete certain files before octobre 1 2016, so need to know how many files im going to delete, for instance ls -lrt file_20160*.lis!wc -l but using grep -c to another file called bplist which contains the list of all files backed up doesn match... (7 Replies)
Discussion started by: alexcol
7 Replies

3. Shell Programming and Scripting

awk to match field between two files and use conditions on match

I am trying to look for $2 of file1 (skipping the header) in $2 of file2 (skipping the header) and if they match and the value in $10 is > 30 and $11 is > 49, then print the line from file1 to a output file. If no match is foung the line is not printed. Both the input and output are tab-delimited.... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

Finding log files that match number pattern

I have logs files which are generated each day depending on how many processes are running. Some days it could spin up 30 processes. Other days it could spin up 50. The log files all have the same pattern with the number being the different factor. e.g. LOG_FILE_1.log LOG_FILE_2.log etc etc ... (2 Replies)
Discussion started by: atelford
2 Replies

5. Shell Programming and Scripting

Counting the number of files within a directory input by the user

So I have a loop that stated if a directory exists or not. If it does it prints the number of files within that directory. I use this code... result=`(ls -l . | egrep -c '^-')` However, no matter which directory I input, it outputs the number "2" What is wrong here? (4 Replies)
Discussion started by: itech4814
4 Replies

6. Shell Programming and Scripting

grep - match files containing minimum number of pattern matches

I want to search a bunch of files and list only those containing a minimum number of pattern matches. So if I want to identify files containing 3 (or more) instances of the pattern "said:" and I have file1 that contains the lines: He said: She said: and file2 that contains the lines: He... (3 Replies)
Discussion started by: stumpyuk
3 Replies

7. Shell Programming and Scripting

List files only when a certain number of files match

Hi, I have many files named CCR20110720011001.CTRD CCR20110720011501.CTRD CCR20110720012001.CTRD CCR20110720012501.CTRD CCR20110720021001.CTRD ... (9 Replies)
Discussion started by: shadyfright
9 Replies

8. UNIX for Dummies Questions & Answers

Comparing two files and count number of lines that match

Hello all, I always found help for my problems using the search option, but this time my request is too specific. I have two files that I want to compare. File1 is the index and File2 contains the data: File1: chr1 protein_coding exon 500 600 . + . gene_id "20532";... (0 Replies)
Discussion started by: DerSeb
0 Replies

9. Shell Programming and Scripting

Need script to take input from file, match on it in file 2 and input data

All, I am trying to figure out a script to run in windows that will allow me to match on First column in file1 to 8th Column in File2 then Insert file1 column2 to file2 column4 then create a new file. File1: 12345 Sam 12346 Bob 12347 Bill File2:... (1 Reply)
Discussion started by: darkoth
1 Replies

10. Shell Programming and Scripting

variable number of input files

Hi all, I'm an extremely newbie with unix/perl. I have a perl script that I need to run on a variable number of input files (i.e., I have to run the perl script on each file, but from run to run, I have a different list of files that need to be put through this script). I think this can... (2 Replies)
Discussion started by: sunmatrix
2 Replies
Login or Register to Ask a Question