Mapping the values of ids of two columns of file1 from file2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Mapping the values of ids of two columns of file1 from file2
# 1  
Old 07-22-2017
Mapping the values of ids of two columns of file1 from file2

I have of two space separated files:

==> File1 <==
Code:
PT|np_496075.1 st|K92748.1 st|K89648.1 PT|np_001300561.1
PT|np_497284.1 st|K90752.1 st|K90279.1 PT|np_740775.1
PT|np_497749.1 st|K90752.1 st|K92038.1 PT|np_490856.1
PT|np_497284.1 st|K90752.1 st|K88095.1 PT|np_494764.1

==> File 2 <==
Code:
PT|np_490856.1 171608
PT|np_740775.1 171647
PT|np_497749.1 171647
PT|np_494764.1 171646
PT|np_497284.1 171648
PT|np_496075.1 171200
PT|np_001300561.1 171202

I want to map the values for first and fourth column of File1 (begin with PT|np_xxxx) from column 2 of File2. The File2 contains the values of PT|np_xxxx.

The desired output is (mapped values of column1 from file2, column1 file1, column2 file1, column3 file1, column4 file1, mapped values of column4 from file2)

==> File 3 <==
Code:
171200  PT|np_496075.1    st|K92748.1   st|K89648.1   PT|np_001300561.1 171202
171648  PT|np_497284.1    st|K90752.1   st|K90279.1   PT|np_740775.1    171647
171647  PT|np_497749.1    st|K90752.1   st|K92038.1   PT|np_490856.1    171608
171648  PT|np_497284.1    st|K90752.1   st|K88095.1   PT|np_494764.1    171646
171648  PT|np_497284.1    st|K90752.1   st|K88689.1   PT|np_497749.1    171647

# 2  
Old 07-22-2017
Quote:
Originally Posted by sammy777888
I have of two space separated files:
==> File1 <==
Code:
PT|np_496075.1 st|K92748.1 st|K89648.1 PT|np_001300561.1
PT|np_497284.1 st|K90752.1 st|K90279.1 PT|np_740775.1
PT|np_497749.1 st|K90752.1 st|K92038.1 PT|np_490856.1
PT|np_497284.1 st|K90752.1 st|K88095.1 PT|np_494764.1

==> File 2 <==
Code:
PT|np_490856.1 171608
PT|np_740775.1 171647
PT|np_497749.1 171647
PT|np_494764.1 171646
PT|np_497284.1 171648
PT|np_496075.1 171200
PT|np_001300561.1 171202

I want to map the values for first and fourth column of File1 (begin with PT|np_xxxx) from column 2 of File2. The File2 contains the values of PT|np_xxxx.

The desired output is (mapped values of column1 from file2, column1 file1, column2 file1, column3 file1, column4 file1, mapped values of column4 from file2)
==> File 3 <==
Code:
171200  PT|np_496075.1    st|K92748.1   st|K89648.1   PT|np_001300561.1 171202
171648  PT|np_497284.1    st|K90752.1   st|K90279.1   PT|np_740775.1    171647
171647  PT|np_497749.1    st|K90752.1   st|K92038.1   PT|np_490856.1    171608
171648  PT|np_497284.1    st|K90752.1   st|K88095.1   PT|np_494764.1    171646
171648  PT|np_497284.1    st|K90752.1   st|K88689.1   PT|np_497749.1    171647

Hello sammy777888,

Not sure how above highlighted line has come because I can't see any relation of line PT|np_001300561.1 171202 from Input_file2 to Input_file1. If this is a typo then following may help you in same.
Code:
awk -F'[| ]' 'FNR==NR{a[$2]=$0;b[$2]=$NF;next} ($2 in a){print b[$2] "\t" $0 "\t" b[$2];}' OFS="|"   Input_file2  Input_file1

Output will be as follows.
Code:
171200  PT|np_496075.1 st|K92748.1 st|K89648.1 PT|np_001300561.1        171200
171648  PT|np_497284.1 st|K90752.1 st|K90279.1 PT|np_740775.1   171648
171647  PT|np_497749.1 st|K90752.1 st|K92038.1 PT|np_490856.1   171647
171648  PT|np_497284.1 st|K90752.1 st|K88095.1 PT|np_494764.1   171648

If I missed something I apologies for it. If above is as per your expectations then well and good else please show us more clear Input_files with more clear conditions in code tags, so that we could try to help you in same. Have a great weekend ahead.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 07-22-2017
Try:
Code:
awk 'NR==FNR{A[$1]=$2; next} {print A[$1], $0, A[$NF]}' file2 file1

Output:
Code:
171200 PT|np_496075.1 st|K92748.1 st|K89648.1 PT|np_001300561.1 171202
171648 PT|np_497284.1 st|K90752.1 st|K90279.1 PT|np_740775.1 171647
171647 PT|np_497749.1 st|K90752.1 st|K92038.1 PT|np_490856.1 171608
171648 PT|np_497284.1 st|K90752.1 st|K88095.1 PT|np_494764.1 171646

This User Gave Thanks to Scrutinizer 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

awk to search field2 in file2 using range of fields file1 and using match to another field in file1

I am trying to use awk to find all the $2 values in file2 which is ~30MB and tab-delimited, that are between $2 and $3 in file1 which is ~2GB and tab-delimited. I have just found out that I need to use $1 and $2 and $3 from file1 and $1 and $2of file2 must match $1 of file1 and be in the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. UNIX for Dummies Questions & Answers

Compare file1 and file2, print matching lines in same order as file1

I want to print only the lines in file2 that match file1, in the same order as they appear in file 1 file1 file2 desired output: I'm getting the lines to match awk 'FNR==NR {a++}; FNR!=NR && a' file1 file2 but they are in sorted order, which is not what I want: Can anyone... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

3. UNIX for Dummies Questions & Answers

if matching strings in file1 and file2, add column from file1 to file2

I have very limited coding skills but I'm wondering if someone could help me with this. There are many threads about matching strings in two files, but I have no idea how to add a column from one file to another based on a matching string. I'm looking to match column1 in file1 to the number... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

4. Shell Programming and Scripting

Remove lines in file1 with values from file2

Hello, I have two data files: file1 12345 aa bbb cccc 98765 qq www uuuu 76543 pp rrr bbbbb 34567 nn ccc sssss 87654 qq ppp rrrrr file2 98765 34567 I need to remove the lines from file1 if the first field contains a value that appears in file2: output 12345 aa bbb cccc 76543 pp... (2 Replies)
Discussion started by: palex
2 Replies

5. Shell Programming and Scripting

Get values from different columns from file2 when match values of file1

Hi everyone, I have file1 and file2 comma separated both. file1 is: Header1,Header2,Header3,Header4,Header5,Header6,Header7,Header8,Header9,Header10 Code7,,,,,,,,, Code5,,,,,,,,, Code3,,,,,,,,, Code9,,,,,,,,, Code2,,,,,,,,,file2... (17 Replies)
Discussion started by: cgkmal
17 Replies

6. Shell Programming and Scripting

AWK: read values from file1; search for values in file2

I have read another post about this issue and am wondering how to adapt it to my own, much simpler, issue. I have a file of user IDs like so: 333333 321321 546465 ...etc I need to take each number and use it to print records wherein the 5th field matches the user ID pulled from the... (2 Replies)
Discussion started by: Bubnoff
2 Replies

7. UNIX for Dummies Questions & Answers

Extracting 482/300k columns no's with respective info. listed in file2 from file1

Hi, I have 2 files File 1: 1 2 3 4 5 6 .......etc until column 300K 1 23 21 24 12 22 1 23 21 24 12 22 1 23 21 24 12 22 1 23 21 24 12 22 1 23 21 24 12 22 1 23 21 24 12 22 1 23 21 24 12 22 . . etc until row 1411 File 2: (14 Replies)
Discussion started by: sogi
14 Replies

8. UNIX for Dummies Questions & Answers

Replace columns from File1 with columns from File2

Hi all, I would like to replace some columns from file1 with columns from file2. Currently, I'm able to do it with the following command: awk 'NR==FNR{a=$1;b=$2;c=$3;next;} {$2=a;$4=b;$5=c;print}' file2 file1 > temp mv -f temp file1 First, i make the changes and save it as a temp... (1 Reply)
Discussion started by: seijihiko
1 Replies

9. Shell Programming and Scripting

Search values between ranges in File1 within File2

Hi people, I have 2 files, one with a list of non consecutive ranges (File1.txt), where each range begins with the value in column 1 and finishes with the value in column 2 in the same line, as can be seen above. 215312581156279 215312581166279 215312582342558 215312582357758... (4 Replies)
Discussion started by: cgkmal
4 Replies

10. Shell Programming and Scripting

awk/sed search lines in file1 matching columns in file2

Hi All, as you can see I'm pretty new to this board. :D I'm struggling around with small script to search a few fields in another file. Basically I have file1 looking like this: 15:38:28 sz:10001 pr:14.16 15:38:28 sz:10002 pr:18.41 15:38:29 sz:10003 pr:19.28 15:38:30 sz:10004... (1 Reply)
Discussion started by: floripoint
1 Replies
Login or Register to Ask a Question