search for matching value between lists


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search for matching value between lists
# 1  
Old 07-18-2011
search for matching value between lists

Hello. I have two very large files, file1 and file2. File1 has two columns of paired numbers eg.
2 5
9 17
47 700
File1 has one column of numbers eg.
5
7
76

I would like to search in file2 for numbers that are greater than or equal to the values from column1 of file1 but less then or equal to values from column2 of file1 and then print out matching numbers from both files. I am trying to use awk to do this but am having problems. Any suggestions?
# 2  
Old 07-18-2011
What is the desired output for your sample input?
# 3  
Old 07-18-2011
The desired output from the example would be
2 5 5
47 700 76
The first two columns would be from file1 and the last column from file2
# 4  
Old 07-18-2011
Try:
Code:
awk 'NR==FNR{a[NR]=$1;next}a[FNR]>=$1&&a[FNR]<=$2{print $0" "a[FNR]}' file2 file1

# 5  
Old 07-18-2011
The following code will work:
Code:
#!/usr/bin/ksh
while read mNbr; do
  while read mFrom mTo; do
    if [[ ${mNbr} -ge ${mFrom} && ${mNbr} -le ${mTo} ]]; then
      echo "${mNbr} ${mFrom} ${mTo}"
      break
    fi
  done < Target_Numbers
done< Source_Numbers

Here is an example:
Source_Numbers:
Code:
2
5
7
23
76

Target_Numbers:
Code:
2 5
9 17
47 700

Result:
Code:
2 2 5
5 2 5
76 47 700

# 6  
Old 07-18-2011
Thanks. When I use this program I get this message on every line.

")syntax error: invalid arithmetic operator (error token is "
my files have large numbers like 6280051. Could this be a problem? Also should I reverse which is target file and which is source file?

---------- Post updated at 05:51 PM ---------- Previous update was at 05:37 PM ----------

Thanks but I don't get anything to stndout. Also I am using large numbers with up to 7 digits eg 6500431. Could this be a problem?
# 7  
Old 07-18-2011
A signed 32-bit integer type (a conservative common denominator for a long while now) can represent an integer ranging from approximately 2 billion to negative 2 billion. 7 million-ish is not a problem.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Matching column search in two files

Hi, I have a tab delimited file1: NC_013499.1 3180 3269 GQ342961.1 NC_030295.1 5925 6014 FN398100.2 NC_007915.1 6307 6396 KU529284.1 NC_013499.1 5033 5122 GQ342961.1 And a second file2: NC_030295.1 RefSeq gene 136 5115 ... (6 Replies)
Discussion started by: Ibk
6 Replies

2. Shell Programming and Scripting

How can I extract XML block around matching search string?

I want to extract XML block surrounding search string Ex: print XML block for string "myapp1-ear" surrounded by "<application> .. </application>" Input XML: <?xml version="1.0" encoding="UTF-8"?> <deployment-request> <requestor> <first-name>kchinnam</first-name> ... (16 Replies)
Discussion started by: kchinnam
16 Replies

3. Shell Programming and Scripting

Matching column and search closest elements

Hi all I have a great challenge that I am not able to resolve. Briefly, I have a file like this: ID_1 chr1 100 - ID_2 chr2 300 + and another file like this: name_1 chr1 150 no - name_2 chr1 250 yes - name_3 chr2 350 yes + name_4 chr2 280 yes + Well, for each entry in file1 I would... (2 Replies)
Discussion started by: giuliangiuseppe
2 Replies

4. Shell Programming and Scripting

Help need with PERL multiple search pattern matching!

My example file is as given below: conn=1 uid=oracle conn=2 uid=db2 conn=3 uid=oracle conn=4 uid=hash conn=5 uid=skher conn=6 uid=oracle conn=7 uid=mpalkar conn=8 uid=anarke conn=9 uid=oracle conn=1 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.10.5.6 to 10.18.6.5 conn=2... (3 Replies)
Discussion started by: sags007_99
3 Replies

5. UNIX for Dummies Questions & Answers

Search and extract matching patterns

%%%%% (9 Replies)
Discussion started by: lucasvs
9 Replies

6. Shell Programming and Scripting

Search string in unix and print whole matching word

Hi I have requirement to search string starting with specific characters and print whole matching word in that string. example mystr="ATTRIBUTE NAME="Event Name" VALUE="Execute"" I want to search by passing "NAME=" and result should be NAME="Event Name". i am using below command but... (3 Replies)
Discussion started by: tmalik79
3 Replies

7. Shell Programming and Scripting

AWK:- matching pattern search

Dear Friends, I have a flat file. To pick certain details we have written an awk where we are facing difficulty. Sample of flat file. line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8 line 9 line 10 line 11 line 12 line 13 line 14 (Matching pattern "Lkm_i-lnr:"can be... (4 Replies)
Discussion started by: anushree.a
4 Replies

8. Shell Programming and Scripting

Shell Script to Create non-duplicate lists from two lists

File_A contains Strings: a b c d File_B contains Strings: a c z Need to have script written in either sh or ksh. Derive resultant files (File_New_A and File_New_B) from lists File_A and File_B where string elements in File_New_A and File_New_B are listed below. Resultant... (7 Replies)
Discussion started by: mlv_99
7 Replies

9. Shell Programming and Scripting

awk/sed search lines in file1 matching columns in file2

Hi All, as you can see I'm pretty new to this board. :D I'm struggling around with small script to search a few fields in another file. Basically I have file1 looking like this: 15:38:28 sz:10001 pr:14.16 15:38:28 sz:10002 pr:18.41 15:38:29 sz:10003 pr:19.28 15:38:30 sz:10004... (1 Reply)
Discussion started by: floripoint
1 Replies

10. Shell Programming and Scripting

Search array elements as file for a matching string

I would like to find a list of files in a directory less than 2 days old and put them into an array variable. And then search for each file in the array for a matching string say "Return-code= 0". If it matches, then display the array element with a message as "OK". Your help will be greatly... (1 Reply)
Discussion started by: mkbaral
1 Replies
Login or Register to Ask a Question