find common lines using just one column to compare and result with all columns


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers find common lines using just one column to compare and result with all columns
# 1  
Old 04-02-2009
find common lines using just one column to compare and result with all columns

Hi.


If we have this file

A B C
7 8 9
1 2 10


and this other file

A C D F
7 9 2 3
9 2 3 4

The result i´m looking for is intersection with A B C D F

so the answer here will be

7 8 9 2 3

So how do we do a intersection using to compare just the column 1 .


I think what I want is very similiar to this topic, but didn´t work for me

https://www.unix.com/unix-dummies-que...two-files.html

I´m lost for my next step.
I´m using solaris 10

Thanks very much. Smilie
# 2  
Old 04-02-2009
Hmm.... I don't quite understand the logic how you determine the 'intersection'...
What does that mean:
Quote:
The result i´m looking for is intersection with A B C D F

so the answer here will be

7 8 9 2 3

So how do we do a intersection using to compare just the column 1 .
Can you try explaining again, please.

Last edited by vgersh99; 04-02-2009 at 11:23 AM..
# 3  
Old 04-02-2009
What does 'just the column 1' mean? If column 1 is a small number your intersection will be large.

This is a start, using only column 1 to compare, giving a union:
Code:
awk ' FILENAME=="file1" { one[$1]=one[$1] $0}
        FILENAME=="file2" { two[$1]=two[$1] $0}
        END { for (i in one) { if (i in two) {print one[i], two[i]} }  }

If this is really what you want we can go on to creating "unique" output - an intersection.
# 4  
Old 04-02-2009
Looks like vgersh99 and I are equally confused...
# 5  
Old 04-02-2009
Sorry, I will try to explain more detailed


the intersection is all lines that have the numbers in the column A equal.

I will compare lines from set1 to set2, but just using the members of column A.

something like this

FOR each lines OF set1
FOR each line of set2
if first member of this line (columnA) of set1 == first member of the line of setB
print line set1 + of setB
end IF
end FOR
end FOR


So if I have
set1
columns A B C
first line 7 9 10
second line 8 10 12


set2
columns A D E
first line 6 9 10
second line 8 13 12


I will compare all lines of this two sets and look for lines that have the equal A, when I find the equal A, print this line of set1 + set2.


It is better now? Sorry for the english, it is my second language.

Last edited by alcalina; 04-02-2009 at 11:46 AM..
# 6  
Old 04-02-2009
That is what the awk code I wrote for you does. Try it. The code assumes the A B C header you wrote is not in the file.
# 7  
Old 04-02-2009
I´m getting error.
awk: syntax error near line 1
awk: bailing out near line 1


here is a example of things that have inside my files.
set1
pts/86 3:14 angelinajolie
pts/93 1:05 jessicaalba
pts/96 1:12 danielelima

set2

pts/90 3499 panda
pts/92 2301 jimcarey
pts/93 4313 jessicalba
pts/94 23878 brasil
pts/95 5186 ferrari
pts/97 19685 porshe


the answer I desire is
pts/93 1:05 jessicaalba 4313 ( no problem if repeat column)


and OMG, the idea is simple but is so hard to explain in english
This User Gave Thanks to alcalina For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Join columns across multiple lines in a Text based on common column using BASH

Hello, I have a file with 2 columns ( tableName , ColumnName) delimited by a Pipe like below . File is sorted by ColumnName. Table1|Column1 Table2|Column1 Table5|Column1 Table3|Column2 Table2|Column2 Table4|Column3 Table2|Column3 Table2|Column4 Table5|Column4 Table2|Column5 From... (6 Replies)
Discussion started by: nv186000
6 Replies

2. UNIX for Beginners Questions & Answers

Sort by record column, Compare with conditons and export the result

Hello, I am new to Unix and would like to seek a help, please. I have 2 files (file_1 and file_2), I need to perform the following actions. 1 ) Sort the both file by the column 26-36 (which is Invoice number) what is sort command with the column sort? 2) Compare the file_1.sorted and... (3 Replies)
Discussion started by: Usagi
3 Replies

3. Shell Programming and Scripting

Paste columns based on common column: multiple files

Hi all, I've multiple files. In this case 5. Space separated columns. Each file has 12 columns. Each file has 300-400K lines. I want to get the output such that if a value in column 2 is present in all the files then get all the columns of that value and print it side by side. Desired output... (15 Replies)
Discussion started by: genome
15 Replies

4. Shell Programming and Scripting

Compare 2 columns from the same file and print a value depending on the result

Hello Unix gurus, I have a file with this format (example values): label1 1 0 label2 1 0 label3 0.4 0.6 label4 0.5 0.5 label5 0.1 0.9 label6 0.9 0.1 in which: column 1 is a row label column 2 and 3 are values I would like to do a simple operation on this table and get the... (8 Replies)
Discussion started by: ksennin
8 Replies

5. Shell Programming and Scripting

Compare two columns and replacing it with value from third column!!

Hi, I am new to Unix and I am finding it hard to fix a particular logic. The context is as below. File 1 : This contains of 5 fields : Type | Bank Code | Account | Name | Date/Time 60|ABC123|TX123456|XXXX|YYYYMMDDHH:MM:SS 72|ABC123|MYD34561|XXXX|YYYYMMDDHH:MM:SS... (4 Replies)
Discussion started by: DJose
4 Replies

6. Shell Programming and Scripting

Seperated by columns, merge in a file, sort them on common column

Hi All, I have 4 files in below format. I took them as an example. File 1: Cut from position 1-4 then 6-7 then 8-14 then rest left and make them as columns in one new file. Inserting character H to the initial of all line like HCTOT. CTOT 456787897 Low fever CTOR 556712345 High fever... (2 Replies)
Discussion started by: Mannu2525
2 Replies

7. Shell Programming and Scripting

Compare two sample files and find common

Hi I have two sample files attached here one file contain entries in one column and second file contains entries in many columns I have to match entries of first file with entries in secon d file form secon column onwards and if matches write "match" in front of it. I tried several... (11 Replies)
Discussion started by: manigrover
11 Replies

8. Shell Programming and Scripting

Compare a common field in two files and append a column from File 1 in File2

Hi Friends, I am new to Shell Scripting and need your help in the below situation. - I have two files (File 1 and File 2) and the contents of the files are mentioned below. - "Application handle" is the common field in both the files. (NOTE :- PLEASE REFER TO THE ATTACHMENT "Compare files... (2 Replies)
Discussion started by: Santoshbn
2 Replies

9. Ubuntu

How to compare two columns and fetch the common data with additional column

Dear All, I am new to this forum and please ignore my little knowledge :p I have two types of data (a subset is given below) data version 1: 439798 2 1 451209 1 2 508696 2 1 555760 2 1 582757 1 2 582889 1 2 691827... (2 Replies)
Discussion started by: evoll
2 Replies

10. Shell Programming and Scripting

To find all common lines from 'n' no. of files

Hi, I have one situation. I have some 6-7 no. of files in one directory & I have to extract all the lines which exist in all these files. means I need to extract all common lines from all these files & put them in a separate file. Please help. I know it could be done with the help of... (11 Replies)
Discussion started by: The Observer
11 Replies
Login or Register to Ask a Question