The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
for loop in perl priyas Shell Programming and Scripting 5 05-01-2009 01:02 PM
help needed with creating challenging bash script with creating directories I-1 Shell Programming and Scripting 7 04-29-2009 05:33 AM
creating a file using Perl chriss_58 Shell Programming and Scripting 1 06-03-2008 07:41 AM
for loop using the value '01' in PERL dangral Shell Programming and Scripting 9 10-01-2003 03:34 PM
help with perl while loop methos Shell Programming and Scripting 2 04-08-2002 03:30 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-09-2009
repinementer repinementer is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 158
Creating loop for a script -Perl

Hi Guyz
I designed a script that can compare 2 columns(values) of single file and gives the closest numbers to the first column by comparing the numbers in first column with second and it works in a single file.
Quote:

INPUT OUTPUT
Singlefile Singlefile
1stC 2ndC 1stC 2ndC
100 512 100 90
200 612 200 245
300 90 300 309
400 309 400 400
500 245 500 512
600 400 600 612
Now I'm trying to design a new script with 2 objectives for 2 files (not a single file like previous one). This new file contains a new column in both files containing A1-A22 and A-X and A-Y symbols randomly (the order of symbols in both files are may be different or may be not).

Objective1. Make a loop that can coonect the same symbols of 2 files and runs the script for closest numbers in those values. for Example
A-X in INPUT1 1st file has 100 - The script has to search every A-X in INPUT2 2nd File and take the closest of those values i.e A-X90

Quote:

INPUT1
1st File
1stC 2ndC
A1 100
A1 200
A-X 300
A-20 400
A-2 500
A-10 600


INPUT2
2nd File
1stC 2ndC
A-X 512
A11 612
A-X 90
A-20 309
A-21 245
A-7 400


OUTPUT
Singlefile
1stC 2ndC
A-X 90
A-20 309
Objective 2. Print the results to a single file OUTPUT

I'm here by including my script and examples of input and output files to make it easy for you to understand.
Thanx in advance and your valuable time.
Hope I'll get clue.

Last edited by repinementer; 05-09-2009 at 12:49 AM..
  #2 (permalink)  
Old 05-09-2009
repinementer repinementer is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 158
Script for Closest numbers in a single file

Script may be useful for further step

Quote:
#!/usr/bin/perl
use strict;

open FH,"<file.txt" or die "Can not open file";

my (@a1,@a2,$gap,$key);
while(<FH>) {
chomp;
my @tmp=split("\t",$_);

if ($tmp[0]>=1) { # only if there is a number greater than 0 present in the first column
push @a1,$tmp[0];
}
if ($tmp[1]>=1) { # only if there is a number greater than 0 present in the second column
push @a2,$tmp[1];
}
}
close FH;

for(my $i=0;$i<=$#a1;$i++) {
$gap=1000000000000;
$key=0;
for(my $j=0;$j<=$#a2;$j++) {
my $tmp=abs($a2[$j]-$a1[$i]);
if ($tmp<=$gap) {
$gap=$tmp;
$key=$a2[$j];
}
}
print $a1[$i],"\t",$key,"\n";
}
  #3 (permalink)  
Old 05-09-2009
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2009
Posts: 518
Quote:
Originally Posted by repinementer View Post
...
Objective1. Make a loop that can coonect the same symbols of 2 files and runs the script for closest numbers in those values. for Example
A-X in INPUT1 1st file has 100 - The script has to search every A-X in INPUT2 2nd File and take the closest of those values i.e A-X90
...
Your perl program is way too convoluted. What you could do here is to sort both files on their keys and use the "join" built-in to retrieve just the rows that are common in both. Then loop through this data and use an associative array/hash/<whatever> to keep track of the minimum diff between 2nd and 3rd fields for the same key.

Code:
$ 
$ cat ip1
A1 100   
A1 200   
A-X 100  
A-20 400 
A-2 500  
A-10 600 
$        
$ cat ip2
A-X 512  
A11 612  
A-X 90   
A-20 309 
A-21 245 
A-7 400  
A-2 501  
A-2 490  
$        
$ sort -k1,1 ip1 > ip1_sorted; sort -k1,1 ip2 > ip2_sorted
$
$ cat ip1_sorted
A1 100
A1 200
A-10 600
A-2 500
A-20 400
A-X 100
$
$ cat ip2_sorted
A11 612
A-2 490
A-2 501
A-20 309
A-21 245
A-7 400
A-X 512
A-X 90
$
$ join ip1_sorted ip2_sorted |
> awk '{
>   x = $2>=$3 ? $2 - $3 : $3 - $2
>   if (mindiff[$1] == "") {mindiff[$1] = $1":"$2":"$3":"x}
>   else {split(mindiff[$1],arr,":"); diff = arr[4]
>         if (x < diff) {mindiff[$1] = $1":"$2":"$3":"x}
>   }}
> END {
>   for (i in mindiff){split(mindiff[i],arr,":"); print arr[1]" "arr[2]" "arr[3]}
> }'
A-X 100 90
A-20 400 309
A-2 500 501
$
$
Hope that helps,
tyler_durden

________________________________________________________________________
"With a gun barrel between your teeth, you speak only in vowels."
  #4 (permalink)  
Old 05-09-2009
stateperl stateperl is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 13
Awesome Awk

Last edited by stateperl; 05-09-2009 at 11:16 AM..
  #5 (permalink)  
Old 05-09-2009
repinementer repinementer is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 158
Hi

Hi
there is a problem
If the first column contain A1 more than 1 time??(There should be all the values in column 1)
Input1
A1tab 20
A1tab30
A1tab50
A2tab50
A2tab90
A3tab80

INPUT2
A1tab30
A2tab45
A4tab100

OUTPUT
A1tab30
A1tab30
A1tab30
A2tab45
A2tab45
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:34 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0