Matching same columns and finding the smallest match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Matching same columns and finding the smallest match
# 1  
Old 06-03-2009
Matching same columns and finding the smallest match

Hi all,

I am wondering if its possible to solve my problem with a simple code.

Basically I have a file that looks like this (tab delimited)

bob 8 250 tina 8 225
sam 8 225 ellen 9 315
kyle 9 275 sally 9 135

So what I want to do is match columns 2 and 5. If columns 2 and 5 match, the next thing would be to determine the shortest distance between column 3 and 6. Basically I want it to print the columns with the shortest distance.

So from the above, it will print:
bob 8 250 tina 8 225 25
kyle 9 275 ellen 9 315 40

thanks

-----Post Update-----

awk -F"\t" '$2==$5 {print $0}' filename


this is what I have so far. This could only match columns but does not do the subtraction and finding the shortest distance. I guess this is where im stuck Smilie
# 2  
Old 06-03-2009
Code:
awk -F"\t" '$2==$5 {print $0, $3 - $6}' filename

# 3  
Old 06-04-2009
another perl code:

Code:
while(<DATA>){
	chomp;
	my @tmp=split;
	print $_," ",$tmp[2]-$tmp[5],"\n" if $tmp[1]==$tmp[4];
}
__DATA__
bob 8 250 tina 8 225
sam 8 225 ellen 9 315
kyle 9 275 sally 9 135

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Data match 2 files based on first 2 columns matching only and join if match

Hi, i have 2 files , the data i need to match is in masterfile and i need to pull out column 3 from master if column 1 and 2 match and output entire row to new file I have tried with join and awk and i keep getting blank outputs or same file is there an easier way than what i am... (4 Replies)
Discussion started by: axis88
4 Replies

2. Shell Programming and Scripting

Finding matching patterns in two files

Hi, I have requirement to find the matching patterns of two files in Unix. One file is the log file and the other is the error list file. If any pattern in the log file matches the list of errors in the error list file, then I would need to find the counts of the match. For example, ... (5 Replies)
Discussion started by: Bobby_2000
5 Replies

3. Shell Programming and Scripting

Request: How to Parse dynamic SQL query to pad extra columns to match the fixed number of columns

Hello All, I have a requirement in which i will be given a sql query as input in a file with dynamic number of columns. For example some times i will get 5 columns, some times 8 columns etc up to 20 columns. So my requirement is to generate a output query which will have 20 columns all the... (7 Replies)
Discussion started by: vikas_trl
7 Replies

4. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns match on two rows

Hi all, I know this sounds suspiciously like a homework course; but, it is not. My goal is to take a file, and match my "ID" column to the "Date" column, if those conditions are true, add the total number of minutes worked and place it in this file, while not printing the original rows that I... (6 Replies)
Discussion started by: mtucker6784
6 Replies

5. Shell Programming and Scripting

awk script - need help finding matching entries

I have a config file like below JOHN 1,5,6,7,4,999,222,666,555 KELLY 54,77,33 ABC 98,22,11 name and then comma separated unique values. In my code I will have a number eg: 6 and I want to find out matching name eg: JOHN How do i do ? Please advise. Thanks. (5 Replies)
Discussion started by: vegasluxor
5 Replies

6. Shell Programming and Scripting

Match on columns and replace other columns

Hi Friends, I have the following input file cat input chr1 100 200 0.1 0.2 na 1 na nd chr1 105 200 0.1 0.2 1 1 na 98 chr1 110 290 nf 1 na nd na 1 chr2 130 150 12 3 na 1 na 1 chr3 450 600 nf nf na 10 na nd chr4 300 330 1 1 10 11 23 34 My requirement is 1. If $6 is na make $7 nd and... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

7. UNIX for Dummies Questions & Answers

finding the nth match

I have a file that has information for a person....each person gets 3 or more lines to describe them. I was hoping to match person 1, then person 2.....BUT I do not know how to tell grep I only want the first (2nd, 3rd or nth) match. The alternative is doing line by line logic, which is fine... (8 Replies)
Discussion started by: countryStyle
8 Replies

8. UNIX for Dummies Questions & Answers

Help with finding matching position on strings

I have a DNA file like below and I am able to write a short program which finds/not an input motif, but I dont understand how I can include in the code to report which position the motif was found. Example I want to find the first or all "GAT" motifs and want the program to report which position... (12 Replies)
Discussion started by: pawannoel
12 Replies

9. Shell Programming and Scripting

Finding the line number of matching braces

Hi,I am new to shell scripting and i want to find the line numbers of matching braces. The file contents are as follows File XXX.dat 1 ( CLASS "FRUIT" 2 (TYPE "PERSISTENT") 3 (MESSAGE_TYPE "M") 4 (GET_REQRD "Y") 5 (SET_REQRD "Y") 6 ) 7 ( CLASS... (3 Replies)
Discussion started by: Rajendra_1510
3 Replies

10. Shell Programming and Scripting

finding duplicate files by size and finding pattern matching and its count

Hi, I have a challenging task,in which i have to find the duplicate files by its name and size,then i need to take anyone of the file.Then i need to open the file and find for more than one pattern and count of that pattern. Note:These are the samples of two files,but i can have more... (2 Replies)
Discussion started by: jerome Sukumar
2 Replies
Login or Register to Ask a Question