Range Comparison Of Column Value in File1 with Other File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Range Comparison Of Column Value in File1 with Other File
# 8  
Old 12-10-2014
For your first question in post #7, try
Code:
awk     'NR==FNR        {MIN[NR]=$2;MAX[NR]=$3;next}

                        {A=substr($17,6,length($17)-5)
                         for (i in MIN)
                         if (A >= MIN[i] && A<= MAX[i]) 
                                {print $0 > "ROWB"++CNT
                                 next
                                }
                         print $0 > "KOL"
                        }
        ' FS="|"  file2 file1

Your second, recently updated question can be handled in a similar way, but your attempt has some errors:
- the MIN array doesn't exist any more, so for (i in MIN) won't work.
- you don't have different files for different lines as requested, but just two for KOL anbd ROWB
- lines not matching anywhere are lost.
- the indexing of the arrays, although working with for (i in ...) may lead to surprises in other context.

Last edited by RudiC; 12-10-2014 at 05:47 AM..
# 9  
Old 12-10-2014
Hi Rudi,
Thanks for the input, basis on your comments I have modified as below & getting expected output. Please suggest if this would be ok from efficiency perspective as one file will be iterated twice.

Code:
 awk 'FNR==1 {++filecounter}
filecounter==1 {KOLMIN[NR]=$1;KOLMAX[NR]=$2;next}
filecounter==2 {ROWBMIN[NR]=$1;ROWBMAX[NR]=$2;next}
{
 for (i in KOLMIN)
  if (substr($17,6,length($17)-5) >= KOLMIN[i] && substr($17,6,length($17)-5) <= KOLMAX[i] )
                {
   print $0   >>"koloutfile"
   next
  }
 for (i in ROWBMIN)
  if (substr($17,6,length($17)-5) >= ROWBMIN[i] && substr($17,6,length($17)-5) <= ROWBMAX[i])
  {
   print $0  >>"rowboutfile"
   next
  } 
 print $0   >>"outfile"
}
' FS="|" OFS="|" file3 file2 file1

---------- Post updated 12-11-14 at 09:24 AM ---------- Previous update was 12-10-14 at 05:10 PM ----------

Updated code to be referred as below, please help on this

Code:
  
  
 awk 'FNR==1 {++filecounter}
filecounter==1 {KOLMIN[NR]=$2;KOLMAX[NR]=$3;next}
filecounter==2 {ROWBMIN[NR]=$2;ROWBMAX[NR]=$3;next}
{
 for (i in KOLMIN)
  if (substr($17,6,length($17)-5) >= KOLMIN[i] && substr($17,6,length($17)-5) <= KOLMAX[i] )
                {
   print $0   >>"koloutfile"
   next
  }
 for (i in ROWBMIN)
  if (substr($17,6,length($17)-5) >= ROWBMIN[i] && substr($17,6,length($17)-5) <= ROWBMAX[i])
  {
   print $0  >>"rowboutfile"
   next
  } 
 print $0   >>"outfile"
}
' FS="|" OFS="|" file3 file2 file1

# 10  
Old 12-11-2014
If you're happy with the result, I think we're done. There's no file being iterated twice. And, instead of using substr several times, you could put its result into a variable and use that four times.
This User Gave Thanks to RudiC For This Post:
# 11  
Old 12-11-2014
Thanks Rudi
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 sum value of a column by range defined in another file awk?

I have two files, file1.table is the count table, and the other is the range condition file2.range. file1.table chr start end count N1 0 48 1 N1 48 181 2 N1 181 193 0 N1 193 326 2 N1 326 457 0 N1 457 471 1 N1 471 590 2 N1 590 604 1 N1 604 752 1 N1 752 875 1 file2.range... (12 Replies)
Discussion started by: yifangt
12 Replies

2. 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

3. Shell Programming and Scripting

Replace specific column range in a non-delimited file with a string!

Hi All, I will need an help with respect to replacing a range of columns on a non-delimited file using a particular string pattern. Say file input is MYNUMBERD000000-BAN CHUE INSNTS ** N+ MYAREDSDD000000+BAN CHUE INSNTS ** N+ MYDERFFFSD00000-GIR PENT - ACH ** ... (5 Replies)
Discussion started by: navojit dutta
5 Replies

4. UNIX for Dummies Questions & Answers

Extracting rows from a text file if the value of a column falls between a certain range

Hi, I have a file that looks like the following: 10 100080417 rs7915867 ILMN_1343295 12 6243093 7747537 10 100190264 rs2296431 ILMN_1343295 12 6643093 6647537 10 100719451 SNP94374 ILMN_1343295 12 6688093 7599537 ... (1 Reply)
Discussion started by: evelibertine
1 Replies

5. 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

6. Shell Programming and Scripting

Match one column of file1 with that of file2

Hi, I have file1 like this aaa ggg ddd vvv eeeand file2 aaa 2 aaa 443 xxx 76 aaa 34 ggg 33 wee 99 ggg 33 ddd 1 ddd 10 ddd 98 sds 23 (4 Replies)
Discussion started by: polsum
4 Replies

7. Shell Programming and Scripting

awk column comparison big file

Hi all, I would like to compare a column in one file to a column in another file and when there is a match it prints the first column and the corresponding second column. Example File1 ABA ABC ABE ABF File 2 ABA 123 ABB 124 ABD 125 ABC 126 So what I would like printed to a... (6 Replies)
Discussion started by: pcg
6 Replies

8. Shell Programming and Scripting

column value comparison in a file

Hi, Can any one help with my below requirement. i need to compare each line by line and in each line i have to compare some columns values with previous line column values in perl script. Can any one help me........! its very urgent. Thanks (3 Replies)
Discussion started by: jam_prasanna
3 Replies

9. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

10. Shell Programming and Scripting

Looking for AWK Solution for column comparison in a single file

- I am looking for different kind of awk solution which I don't think is mentioned before in these forums. Number of rows in the file are fixed Their are two columns in file1.txt 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 I am looking for 3... (1 Reply)
Discussion started by: softwarekids23
1 Replies
Login or Register to Ask a Question