Sponsored Content
Top Forums Shell Programming and Scripting Common records after matching on different columns Post 302598424 by jacobs.smith on Wednesday 15th of February 2012 10:36:08 AM
Old 02-15-2012
Hi guys,

Somehow I managed to request one of my other friends for a perl script. He was able to write one that could do my task. But, it was only for 2 files.

I would like to request any of you to edit the following code so that it lets me do the task for multiple number of files and multiple cut-offs.

To be clear, I would like to specify at STDIN or while running the code the number of files and the different cutoff.

Thanks to all of you in advance.


Quote:
#!/usr/bin/perl
$file1="1.txt";
$file2="2.txt";
open(FILE, "$file1") || die "can't: $!";

open(OUTPUT, ">op.txt") || die "can't: $!";
%hash='';
$i=1;
while(<FILE>)
{
$line=$_;
chomp($line);
@a=split(/\s+/,"$line");
open(FILE1,"$file2") || die "can't: $!";
while(<FILE1>)
{
$line1=$_;
chomp($line1);
@b=split(/\s+/,"$line1");
if($a[0] eq $b[0])
{
if($a[1] == $b[1])
{
if($a[2] == $b[2])
{
@x=split(/\./,"$file1");
@y=split(/\./,"$file2");
if(!exists $hash{$line})
{
$hash{$line}=1;
print OUTPUT "$a[0]\t$a[1]\t$a[2]\t$x[0]$y[0].txt\n";
}
}
}
}

if($a[0] eq $b[0])
{
if($a[1] < $b[2])
{
if($a[2] > $b[1])
{
if(!exists $hash{$line})
{
$hash{$line}=1;
print OUTPUT "$a[0]\t$a[1]\t$a[2]\t$file1\n";
}
if(!exists $hash{$line1})
{
$hash{$line1}=1;
print OUTPUT "$b[0]\t$b[1]\t$b[2]\t$file2\n";
}
}
}
}

if($a[0] eq $b[0])
{
if(abs($a[1] - $b[2]) <= 300 || abs($a[2] - $b[1]) <=300)
{
if(!exists $hash{$line})
{
$hash{$line}=1;
print OUTPUT "$a[0]\t$a[1]\t$a[2]\t$file1\n";
}
if(!exists $hash{$line1})
{
$hash{$line1}=1;
print OUTPUT "$b[0]\t$b[1]\t$b[2]\t$file2\n";
}
}
}

}
}
---------- Post updated 02-15-12 at 10:36 AM ---------- Previous update was 02-14-12 at 11:28 AM ----------

Please help me. It is a very important taskSmilieSmilieSmilieSmilieSmilieSmilie
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Common records using AWK

Hi, To be honest, I am really impressed and amazed at the pace I find solutions for un-solved coding mysteries in this forum. I have a file like this input1.txt x y z 1 2 3 a b c 4 -3 7 k l m n 0 p 1 2 a b c 4 input2 x y z 9 0 -1 a b c 0 6 9 k l m 8 o p 1 2 a f x 9 Output... (9 Replies)
Discussion started by: jacobs.smith
9 Replies

2. Shell Programming and Scripting

Matching and Merging csv data fields based on a common field

Dear List, I have a file of csv data which has a different line per compliance check per host. I do not want any omissions from this csv data file which looks like this: date,hostname,status,color,check 02-03-2012,COMP1,FAIL,Yellow,auth_pass_change... (3 Replies)
Discussion started by: landossa
3 Replies

3. Shell Programming and Scripting

Common records

Hi, I have the following files, A M 2 3 B E 4 5 C I 5 6 D O 4 5 A M 3 4 B E 5 2 F U 7 9 J K 2 3 OUTPUT A M 2 3 3 4 B E 4 5 5 2 thanks in advance, (7 Replies)
Discussion started by: jacobs.smith
7 Replies

4. UNIX for Dummies Questions & Answers

keeping last record among group of records with common fields (awk)

input: ref.1;rack.1;1 #group1 ref.1;rack.1;2 #group1 ref.1;rack.2;1 #group2 ref.2;rack.3;1 #group3 ref.2;rack.3;2 #group3 ref.2;rack.3;3 #group3 Among records from same group (i.e. with same 1st and 2nd field - separated by ";"), I would need to keep the last record... (5 Replies)
Discussion started by: beca123456
5 Replies

5. Shell Programming and Scripting

Two columns-Common records - 20 files

Hi Friends, I have an input file like this cat input1 x 1 y 2 z 3 a 2 b 4 c 6 d 9 cat input2 x 7 h 8 k 9 l 5 m 9 d 12 (5 Replies)
Discussion started by: jacobs.smith
5 Replies

6. Shell Programming and Scripting

Compare multiple files, identify common records and combine unique values into one file

Good morning all, I have a problem that is one step beyond a standard awk compare. I would like to compare three files which have several thousand records against a fourth file. All of them have a value in each row that is identical, and one value in each of those rows which may be duplicated... (1 Reply)
Discussion started by: nashton
1 Replies

7. Shell Programming and Scripting

Common values in 2 columns in 2 files

Hello, Suppose I have these 2 tab delimited files, where the second column in first file contains matching values from first column of the second file, I would like to get an output like this: File A 1 A 2 B 3 C File B A Apple C Cinnabon B Banana I would like... (1 Reply)
Discussion started by: Mohamed EL Hadi
1 Replies

8. Shell Programming and Scripting

Shell script to filter records in a zip file that contains matching columns from another file

Not sure if this is the correct forum for this question. I have two files. file1.zip, file2 Input: file1.zip col1, col2 , col3 a , b , 0:0:0:0:0:c436:9346:d40b x, y, 0:0:0:0:0:880:39f9:c9a7 m, n , 0:0:0:0:0:80c7:9161:fe00 file2.txt col1 c4:36:93:46:d4:0b... (1 Reply)
Discussion started by: anil.v
1 Replies

9. UNIX for Beginners Questions & Answers

Finding common entries between 10 columns

Hello, I need to find the intersection across 10 columns. Kindly help. my file (INPUT.csv) looks like this 4_R 4_S 8_R 8_S 12_R 12_S 24_R 24_S LOC_Os01g01010 LOC_Os01g01010 LOC_Os01g01010 LOC_Os04g48290 LOC_Os01g01010 LOC_Os01g01010... (1 Reply)
Discussion started by: Sanchari
1 Replies

10. UNIX for Beginners Questions & Answers

Comparing fastq files and outputting common records

I have two files: File_1: @M04961:22:000000000-B5VGJ:1:1101:9280:7106 1:N:0:86 GGCATGAAAACATACAAACCGTCTTTCCAGAAATTGTTCCAAGTATCGGCAACAGCTTTATCAATACCATGAAAAATATCAACCACACCAGAAGCAGCAT + GGGGGGGGGGGGGGGGGCCGGGGGF,EDFFGEDFG,@DGGCGGEGGG7DCGGGF68CGFFFGGGG@CGDGFFDFEFEFF:30CGAFFDFEFF8CAF;;8F ... (3 Replies)
Discussion started by: Xterra
3 Replies
DIFF3(1)						      General Commands Manual							  DIFF3(1)

NAME
diff3 - 3-way differential file comparison SYNOPSIS
diff3 [ -ex3 ] file1 file2 file3 DESCRIPTION
Diff3 compares three versions of a file, and publishes disagreeing ranges of text flagged with these codes: ==== all three files differ ====1 file1 is different ====2 file2 is different ====3 file3 is different The type of change suffered in converting a given range of a given file to some other is indicated in one of these ways: f : n1 a Text is to be appended after line number n1 in file f, where f = 1, 2, or 3. f : n1 , n2 c Text is to be changed in the range line n1 to line n2. If n1 = n2, the range may be abbreviated to n1. The original contents of the range follows immediately after a c indication. When the contents of two files are identical, the contents of the lower-numbered file is suppressed. Under the -e option, diff3 publishes a script for the editor ed that will incorporate into file1 all changes between file2 and file3, i.e. the changes that normally would be flagged ==== and ====3. Option -x (-3) produces a script to incorporate only changes flagged ==== (====3). The following command will apply the resulting script to `file1'. (cat script; echo '1,$p') | ed - file1 FILES
/tmp/d3????? /usr/lib/diff3 SEE ALSO
diff(1) BUGS
Text lines that consist of a single `.' will defeat -e. Files longer than 64K bytes won't work. DIFF3(1)
All times are GMT -4. The time now is 07:29 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy