Home Man
Search
Today's Posts
Register

This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in the UNIX for Beginners Questions & Answers forum.

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

Tags
beginners

 

 
Thread Tools Search this Thread
# 1  
Old 07-19-2012
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 following the string "Gene_" in file2. If there's a match, I want to add column 2 from file1 as the first column in file2.

file1
Code:
1    k48_101119
2    k48_103273
3    k48_103895
4    k48_108200
5    k48_11007
[...]

file2
(not all line numbers in file1 are represented in file2)
Code:
"Gene_2" 2.79393133358307e-119
"Gene_5" 3.58660951659928e-42
[...]

Desired output
Code:
k48_103273 "Gene_2" 2.79393133358307e-119
k48_110077 "Gene_5" 3.58660951659928e-42
[...]

Thanks in advance...
# 2  
Old 07-19-2012
Code:
cat file1 | awk '{print $1}' | while read NUM
do
        # echo ${NUM}

        grep "Gene_${NUM}" file2 2>/dev/null  # output to tty or suppress 

        RC=$?

        if [[ ${RC} == 0 ]]
        then
                REP_STR=`grep "${NUM}  " file1 | awk '{print $2}'`

                # echo ${REP_STR}

                cat file2 | sed "s/\"Gene_"${NUM}"\"/${REP_STR} \"Gene_"${NUM}"\"/g" > temp_file
             
                cp temp_file file2
        fi
done

The Following User Says Thank You to aster007 For This Useful Post:
pathunkathunk (07-19-2012)
# 3  
Old 07-19-2012
This will use a bit more memory, but saves heavily on I/O as it requires each file to be read only once. Depending on the size of the files, should run much more quickly.

Code:
awk '
    NR == FNR { f1[$1] = $2; next; }
    {
        split( $1, a, "_" );
        printf( "%s %s\n", f1[a[2]+0], $0 );
    }
'  file1 file2

The Following User Says Thank You to agama For This Useful Post:
pathunkathunk (07-19-2012)
# 4  
Old 07-19-2012
Thanks good people.
 

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk to search field2 in file2 using range of fields file1 and using match to another field in file1 cmccabe Shell Programming and Scripting 6 12-22-2016 09:55 AM
Compare file1 and file2, print matching lines in same order as file1 pathunkathunk UNIX for Dummies Questions & Answers 4 02-10-2015 01:48 AM
Replacing first field of file2 with the second filed of file1 for matching cases ailnilanjan Shell Programming and Scripting 6 11-06-2014 05:14 AM
Compare file1 for matching line in file2 and print the difference in matching lines RasB15 Shell Programming and Scripting 2 11-07-2013 09:04 AM
Based on column in file1, find match in file2 and print matching lines pathunkathunk Shell Programming and Scripting 2 03-27-2013 01:21 AM
Pattern Matching & replacing of content in file1 with file2 siramitsharma Shell Programming and Scripting 4 03-09-2013 05:59 AM
Match one column of file1 with that of file2 polsum Shell Programming and Scripting 4 03-03-2012 12:07 AM
Delete strings in file1 based on the list of strings in file2 roussine UNIX for Dummies Questions & Answers 7 01-23-2011 10:14 PM
print contents of file2 for matching pattern in file1 - AWK i.scientist UNIX for Advanced & Expert Users 6 09-07-2009 11:45 PM
awk/sed search lines in file1 matching columns in file2 floripoint Shell Programming and Scripting 1 12-17-2008 10:36 PM


All times are GMT -4. The time now is 10:52 PM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password