Unix/Linux Go Back    


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

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

UNIX for Dummies Questions & Answers


 
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-19-2012   -   Original Discussion by pathunkathunk
pathunkathunk's Unix or Linux Image
pathunkathunk pathunkathunk is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 16 May 2017, 5:45 PM EDT
Posts: 57
Thanks: 23
Thanked 0 Times in 0 Posts
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...
Sponsored Links
    #2  
Old Unix and Linux 07-19-2012   -   Original Discussion by pathunkathunk
aster007's Unix or Linux Image
aster007 aster007 is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 28 September 2015, 11:22 AM EDT
Location: Dallas, TX
Posts: 52
Thanks: 17
Thanked 10 Times in 8 Posts


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-20-2012)
Sponsored Links
    #3  
Old Unix and Linux 07-19-2012   -   Original Discussion by pathunkathunk
agama's Unix or Linux Image
agama agama is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 7 April 2014, 3:02 PM EDT
Location: earth>US>UTC-5
Posts: 1,466
Thanks: 110
Thanked 511 Times in 490 Posts
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-20-2012)
    #4  
Old Unix and Linux 07-20-2012   -   Original Discussion by pathunkathunk
pathunkathunk's Unix or Linux Image
pathunkathunk pathunkathunk is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 16 May 2017, 5:45 PM EDT
Posts: 57
Thanks: 23
Thanked 0 Times in 0 Posts
Thanks good people.
Sponsored Links
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Match one column of file1 with that of file2 polsum Shell Programming and Scripting 4 03-03-2012 01:07 AM
Delete strings in file1 based on the list of strings in file2 roussine UNIX for Dummies Questions & Answers 7 01-23-2011 11:14 PM
Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 rydz00 Shell Programming and Scripting 7 11-09-2010 11:28 AM
print contents of file2 for matching pattern in file1 - AWK i.scientist UNIX for Advanced & Expert Users 6 09-08-2009 12:45 AM
awk/sed search lines in file1 matching columns in file2 floripoint Shell Programming and Scripting 1 12-17-2008 11:36 PM



All times are GMT -4. The time now is 03:42 AM.