Match columns several files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match columns several files
# 1  
Old 04-08-2013
Match columns several files

Hey fellas!

Here come my problem. I appreciate if you have a look at it.

I have several files with following structure:

file_1:
Code:
1 21
4 45

file_2:
Code:
2 31
4 153
6 341

and so on...

and I have a 'reference' file look like this:

File_ref:
Code:
A 1 
B 2
C 3
D 4 
E 5
F 6


And this is the desired output:

output:
Code:
  file_1 file_2
A_1 21  
B_2       31
C_3     
D_4 45    153
E_5     
F_6       341

My files are much more complicated. I am not just sure which commands should I use. it seems so simple but I haven't managed to make it! :|
Thanks in advance.
# 2  
Old 04-08-2013
Code:
$ awk 'FNR==1{s=count?s"\t"FILENAME:"\t";count++}NR==FNR{A[$2]=$1"_"$2;B=$2;next}NR!=FNR{A[$1,count]=$2;next}END{
print s;
for(i=1;i<=B;i++){printf A[i];
for(j=1;j<=count;j++){printf "\t%s",A[i,j]}printf "\n"}}' file_final file_1 file_2
                file_1  file_2
A_1             21
B_2                     31
C_3
D_4             45      153
E_5
F_6                     341

# 3  
Old 04-08-2013
Thanks Pamu for your time.

here are two problems! :|
1. I have several files. instead of writing the names manually can I use something like *.txt ?

2. I am more dumb than what I thought! :| :| actually the columns that I wanna check similarity are $4 from all files and ref file. and in the output should comes the $3 of ref file to $1 of output and $13 of each file as the values in the right positions.

I tried to modify it myself but it goes forever and then crashes! :|

could you please give me some updates?

Thanks A LOT!
# 4  
Old 04-09-2013
Could you please give me sample input for this....
Not clear from your description.

and for many files you can use sth like this...

Code:
awk {} file_ref *.txt

# 5  
Old 04-09-2013
I am sorry for not being clear...

I have attached one of my several files.
And the reference file.
and based on those the desired output File.
It's OK if you don't have time to make it. But if you do I appreciate it.
# 6  
Old 04-09-2013
Code:
awk '{if (FILENAME="ref.txt"){if (a[$4]) { print $3,a[$4]}}a[$4]=a[$4]"\t"$13;}' *.txt ref.txt

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

Comparing two columns in two files and printing a third based on a match

Hello all, First post here. I did not notice a previous post to help me down the right path. I am looking to compare a column in a CSV file against another file (which is not a column match one for one) but more or less when a match is made, I would like to append a third column that contains a... (17 Replies)
Discussion started by: dis0wned
17 Replies

3. UNIX for Dummies Questions & Answers

Match the columns between two files and output

Hi Help, I have two files namely a.txt and b.txt a.txt looks like a.txt 1 2 2 1 3 3 2 4 4 4 5 6 6 7 7 b.txt looks like, b.txt 1 2 1 1 3 2 2 4 3 3 4 4 4 5 5 (2 Replies)
Discussion started by: Indra2011
2 Replies

4. Shell Programming and Scripting

Match first two columns and average third from multiple files

I have the following format of input from multiple files File 1 24.01 -81.01 1.0 24.02 -81.02 5.0 24.03 -81.03 0.0 File 2 24.01 -81.01 2.0 24.02 -81.02 -5.0 24.03 -81.03 10.0 I need to scan through the files and when the first 2 columns match I... (18 Replies)
Discussion started by: ncwxpanther
18 Replies

5. Shell Programming and Scripting

Return first two columns if match found among two files

Hi, I have FileA with one column. File B with 15 columns separated by comma delimiter. I need to compare the FILEA value with all 15 columns of FILEB... if matches, need to return the 1st, 2nd column values of FILEB. How to achieve this through shell script? Thanks in advance. (5 Replies)
Discussion started by: vamsikrishna928
5 Replies

6. Shell Programming and Scripting

Match the columns between 2 files

I have two files I want to match ids in the 5th column of the file 1 with the first column of the file 2 and get the description for the matched ids as shown in the output sno nm no nm2 ID 1 cc 574372 yyyi |6810|51234| 2 bb 119721 nmjk |6810|51234|51179| ... (4 Replies)
Discussion started by: raj_k
4 Replies

7. Shell Programming and Scripting

Match two columns from two files and print output

Hello, I have two files which are of the following format File 1 which has two columns Protein_ID Substitution NP_997239 T53R NP_060668 V267M NP_058515 P856A NP_001206 T55M NP_006601 D371Y ... (2 Replies)
Discussion started by: nans
2 Replies

8. Shell Programming and Scripting

Match files based on either of the two columns awk

Dear Shell experts, I have 2 files with structure: File 1: ID and count head test_GI_count1.txt 1000094 2 10039307 1 10039641 1 10047177 11 10047359 1 1008555 2 10120302 1 10120672 13 10121776 1 10121865 32 And 2nd file: head Protein_gi_GeneID_symbol.txt protein_gi GeneID... (11 Replies)
Discussion started by: smitra
11 Replies

9. UNIX for Dummies Questions & Answers

Two files; if cells match then copy over other columns

My current issue is dealing with two space delimited files. The first file has column 1 as the sample ID's, then columns 2 - n as the observations. The second file has column 1 as the sample ID's, column 2 as the mother ID's, column 3 as the father ID's, column 4 as the gender, and column 5... (3 Replies)
Discussion started by: Renyulb28
3 Replies

10. Shell Programming and Scripting

Match strings in two files and compare columns of both

Good Morning, I was wondering if anybody could tell me how to achieve the following, preferably with a little commenting for understanding. I have 2 files, each with multiple rows with multiple columns. I need to find each row where the value in column 1 of file 1 matches column 1... (10 Replies)
Discussion started by: GarciasMuffin
10 Replies
Login or Register to Ask a Question

Featured Tech Videos