loop through two files based on a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting loop through two files based on a variable
# 15  
Old 02-24-2009
ouput of file1, IN10-----chrom--count---start

chr1 10

176716364
24737792
41368822
200251548
28707214
198709839
93419080
52270366
151846111
168277851
chr10 7

62870352
104039249
31255458
978073
6869853
17678914
4274540

output file 2,IN11----------chrom...count...middle

chr1 2463

19135.5
125206.5
317872.5
427520.5
439771.5
523529.5
535556.5
704128.5
752793.5
778900
chr2 1150

84913.5

109885.5

112361

171517

336666

358442.5
# 16  
Old 02-24-2009
output must have some algorithm like this but it has a bug, the following algorithm has file1 only. I dont know how to create foreach loop for both file1 and file2

foreach my $chrom (sort keys %chromes){

if ( $chrom =~ /^chr1/){

print closest of start and middle of chr1 in both files
This is the point where I have to insert the script you replied before

}
elsif { $chrome =~ (^/chr2/)
print ........................................chr2..... and so on
}
else(not recognized)
}

Last edited by nogu0001; 02-24-2009 at 03:23 AM..
# 17  
Old 02-24-2009
Code:
#!/usr/bin/perl
use strict;
open FH,"<a.txt" or die "Can not open file";
my (@a1,@a2,$gap,$key);
while(<FH>){
	chomp;
	my @tmp=split(",",$_);
	push @a1,$tmp[0];
	push @a2,$tmp[1];
}
close FH;
for(my $i=0;$i<=$#a1;$i++){
	$gap=0;
	$key=0;
	for(my $j=0;$j<=$#a1;$j++){
		my $tmp=abs($a2[$j]-$a1[$i]);
		if ($gap==0||$tmp<=$gap){
			$gap=$tmp;
			$key=$a2[$j];
		}
	}
	print $a1[$i],",",$key,"\n";
}

# 18  
Old 02-24-2009
Awessssssssssssome Dude
I have no words for it u just used the logic I'm just thinking of

what if column1 values are few and column2 values are more and still has comma with it
73.924598,40.879010
73.924506,40.878978
73.924506,40.878978
................,40.878178
................,50.878178
................,60.878578
# 19  
Old 02-24-2009
Here is my answer for you, but as you subverted your Read Only status, which was a result of your persistently breaking the forum rules, you are banned.

Code:
# For each key in %file1,
#   1. split the key into name/start parts
#   2. search for the record in file2 that BOTH:
#     (a) the corresponding records have the same "name" field
#     (b) has the smallest difference between $start and $middleno
#         of any of the records
#   3. Print out both records in one line
#   4. Delete these record from the structures (so they cannot be matched again)
#
foreach $key (sort {$a <=> $b} keys %file1) {
  my ($name, $start) = split(":",$key);
  my $min = 2 ** 31;  # start as maximum integer
  my $smallest_key = undef;
  for (my $i=0; $i <= $#file2_middle_keys; ++$i) {
      my $current_key = $name .":". $file2_middle_keys[$i];

      # skip entries that do not match the $name in file2 (see (1a), above)
      next unless (exists $file2{ $current_key });

      # calculate difference and see  (see (1b), above)
      my $diff = abs($file2_middle_keys[$i] - $start);
      if ($i > 0 && $diff > $min) {
        # stop -- difference is getting bigger. No need to proceed.
        last;
      }
      if ($min > $diff) {
        $min = abs($file2_middle_keys[$i] - $key);
        $smallest_key = $current_key;
      }
  }
  # answer of (2b) is in $smallest_key
  if (defined $smallest_key) {
    # (3)
    print join(" ",
      @{ $file1{$key} }[1,2,3],
      @{ $file2{$smallest_key } }[3,1,2],
      @{ $file1{$key} }[0],
    )."\n";

    # (4)
    delete $file1{$key};
    delete $file2{$smallest_key };
  }
}
# - Licenced under AGPL (http://www.gnu.org/licenses/agpl.txt)
# - Author: Otheus [https://www.unix.com/members/302022384.html]

# 20  
Old 02-24-2009
Quote:
Here is my answer for you, but as you subverted your Read Only status, which was a result of your persistently breaking the forum rules, you are banned.
oops..... so long nogu0001
# 21  
Old 02-26-2009
Code:
nawk '{
if(NR==FNR)
{
  arr[$1]=$2
  brr[$1]=0
}
else
{
  a=$2-arr[$1]
  b=brr[$1]-arr[$1]
  c1=(a<0)?(-1)*a:a
  c2=(b<0)?(-1)*b:b
  if( (brr[$1]==0) || (a<b))
       brr[$1]=$2
}
}
END{
  for(i in arr)
    print i" "arr[i]" "brr[i]
}' file1 file2

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare two files and print based on common variable value.

Hi All, i have below two files. FILE: NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root" GROUP="disk" MODE="brw-rw----" PKNAME="" MOUNTPOINT="" NAME="/dev/sda1" TYPE="part" SIZE="500M" OWNER="root" GROUP="disk" MODE="brw-rw----" PKNAME="/dev/sda" MOUNTPOINT="/boot" NAME="/dev/sda2"... (3 Replies)
Discussion started by: balu1234
3 Replies

2. Shell Programming and Scripting

Naming output files based on variable parameters and input filenames

Hello, I have a series of files in sub-directories that I want to loop through, process and name according to the input filename and the various parameters I'm using to process the files. I have a number of each, for example file names like AG005574, AG004788, AG003854 and parameter values like... (2 Replies)
Discussion started by: bdeads
2 Replies

3. Shell Programming and Scripting

Calling a Variable based on a Variable

Hi all, I have a source config file with variables like so: eth1_ip=192.168.1.99 eth2_ip=192.168.1.123 eth3_ip=172.16.1.1 I am trying to run a script which loops based on the number of eth interfaces on a machine and therefore modifies the variable it calls in the environment based on the... (5 Replies)
Discussion started by: landossa
5 Replies

4. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

5. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

6. Shell Programming and Scripting

printing variable with variable suffix through loop

I have a group of variables myLINEcnt1 - myLINEcnt10. I'm trying to printout the values using a for loop. I am at the head banging stage since i'm sure it has to be a basic syntax issue that i can't figure out. For myIPgrp in 1 2 3 4 5 6 7 8 9 10; do here i want to output the value of... (4 Replies)
Discussion started by: oly_r
4 Replies

7. Shell Programming and Scripting

[SHELL: /bin/sh] For loop using variable variable names

Simple enough problem I think, I just can't seem to get it right. The below doesn't work as intended, it's just a function defined in a much larger script: CheckValues() { for field in \ Group_ID \ Group_Title \ Rule_ID \ Rule_Severity \ ... (2 Replies)
Discussion started by: Vryali
2 Replies

8. Shell Programming and Scripting

Using awk to create files based on a variable name

Hey all, I am parsing a file which have records containing one of a number of files names: ".psd", ".cr2", ".crw" , ".cr", ".xi", ".jpg", ".xif" etc Somewhere on each line there is a value "Namex.psd" "Namex.crw" etc. The position of this name is highly variable I need to output all the ".psd"... (4 Replies)
Discussion started by: C0ppert0p
4 Replies

9. Shell Programming and Scripting

create variable name based on another variable's value

Hello, I am needing to create a variable and assign it a value based on the value of a previosly defined variable... I am using KSH.. Example: VAR1=COMPUTER1 I need another variable like ${VAR1}_FLAG="Y", so it would actually be COMPUTER1_FLAG="Y". I will be looping through many values in... (2 Replies)
Discussion started by: benefactr
2 Replies
Login or Register to Ask a Question