Sponsored Content
Top Forums Shell Programming and Scripting Creating loop for a script -Perl Post 302314659 by durden_tyler on Saturday 9th of May 2009 10:00:22 AM
Old 05-09-2009
Quote:
Originally Posted by repinementer
...
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."
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

help needed with creating challenging bash script with creating directories

Hi, Can someone help me with creating a bash shell script. I need to create a script that gets a positive number n as an argument. The script must create n directories in the current directory with names like map_1, map_2 etcetera. Each directory must be contained within its predecessor. So... (7 Replies)
Discussion started by: I-1
7 Replies

2. UNIX for Dummies Questions & Answers

Foreach loop to run a perl script on multiple files

Hi, I have thousands of files in a directory that have the following 2 formats: 289620178.aln 289620179.aln 289620180.aln 289620183.aln 289620184.aln 289620185.aln 289620186.aln 289620187.aln 289620188.aln 289620189.aln 289620190.aln 289620192.aln.... and: alnCDS_1.fasta (1 Reply)
Discussion started by: greptastic
1 Replies

3. Shell Programming and Scripting

while loop in perl script

#! /usr/bin/perl $exp = "y"; while ($exp !="n") { system "clear"; # clear the window print "\nEnter the number of Widgets ordered: "; $widgetcount = <STDIN>; #vairable to save the number of total widgerts ordered chop $widgetcount; print "\nEnter the number of Gidgets ordered:... (2 Replies)
Discussion started by: navjot99cheema
2 Replies

4. Shell Programming and Scripting

Creating a loop in csh

I have the following code and want to use a loop to output the results to the fparams file. if ($optparams == 1) then # Set the tdarwin parameters set txt01 = "Call to raytrac.csh" set txt02 = "" set txt03 = "./Scripts/raytrac.csh $*" set txt04 = "" set txt05 =... (0 Replies)
Discussion started by: kristinu
0 Replies

5. Shell Programming and Scripting

Creating variable by for loop

i have a file 'detail' which contains cat detail 111111 222222 333333 444444 but detail may be 4 line file.6 line file or 8 line file like cat detail 111111 222222 333333 444444 555555 666666 777777 888888 so i want a declare a loop which assign the value of first line in one... (11 Replies)
Discussion started by: rakeshtomar82
11 Replies

6. Shell Programming and Scripting

Need help with perl script with a while loop reading file

Good morning, I appreciate any assistance that I can get from the monks out there. I am able to get this to work for me so that I can do a hostname lookup if I only specify one hostname in the script. What I want to do is have a file with hostnames and do lookups for each name in the file. Here is... (1 Reply)
Discussion started by: brianjb
1 Replies

7. Shell Programming and Scripting

Splitting a file and creating new files using Perl script

Hi All, I am new to Scripting language. I want to split a file and create several subfiles using Perl script. Example : File format : Sourcename ID Date Nbr SU IMYFDJ 9/17/2012 5552159976555 SU BWZMIG 9/14/2012 1952257857887 AR PEHQDF 11/26/2012 ... (13 Replies)
Discussion started by: Deepak9870
13 Replies

8. Shell Programming and Scripting

Calling an interactive perl script from within a while-read loop

Hi, I have a perl script that prompts for a user to enter a password before doing what it does. This works well if I call it directly from a bash script #!/bin/bash /path/to/perl/script $arg1 $arg2 But, when I try to enclose this within a while read loop, the perl script is called but... (1 Reply)
Discussion started by: prafulnama
1 Replies

9. Programming

Perl: restrict perl from automaticaly creating a hash branches on check

My issue is that the perl script (as I have done it so far) created empty branches when I try to check some branches on existence. I am using multydimentional hashes: found it as the best way for information that I need to handle. Saing multidimentional I means hash of hashes ... So, I have ... (2 Replies)
Discussion started by: alex_5161
2 Replies

10. Shell Programming and Scripting

PERL script loop problem

I have written the below PERL script to reprocess messages from a failure queue. It basically browses all the messages in the failure queue to individual files in a directory and then scans those files to determine the originating queue. The script will then move each message in turn from the... (0 Replies)
Discussion started by: chris01010
0 Replies
COLRM(1)						    BSD General Commands Manual 						  COLRM(1)

NAME
colrm -- remove columns from a file SYNOPSIS
colrm [start [stop]] DESCRIPTION
The colrm utility removes selected columns from the lines of a file. A column is defined as a single character in a line. Input is read from the standard input. Output is written to the standard output. If only the start column is specified, columns numbered less than the start column will be written. If both start and stop columns are spec- ified, columns numbered less than the start column or greater than the stop column will be written. Column numbering starts with one, not zero. Tab characters increment the column count to the next multiple of eight. Backspace characters decrement the column count by one. ENVIRONMENT
The LANG, LC_ALL and LC_CTYPE environment variables affect the execution of colrm as described in environ(7). EXIT STATUS
The colrm utility exits 0 on success, and >0 if an error occurs. SEE ALSO
awk(1), column(1), cut(1), paste(1) HISTORY
The colrm command appeared in 3.0BSD. BSD
August 4, 2004 BSD
All times are GMT -4. The time now is 08:13 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy