Replace column that matches specific pattern, with column data from another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace column that matches specific pattern, with column data from another file
# 1  
Old 05-15-2012
Replace column that matches specific pattern, with column data from another file

Can anyone please help with this? I have 2 files as given below.
If 2nd column of file1 has pattern foo1@a, find the matching 1st column in file2 & replace 2nd column of file1 with file2's value.

Code:
file1

abc_1    foo1@a   ....
abc_1    soo2@a ...
def_2    soo2@a  ....
def_2    foo1@a .....
ghi_3    foo1@a .....
ghi_3    soo1@a

file2

Code:
def_2    foo2@a
def_2    soo2@a
abc_1     foo3@a
abc_1     soo1@a        
ghi_3      foo2@a
ghi_3    soo1@a

Required output ( file1 modified to be):

Code:
abc_1    foo3@a
abc_1    soo2@a
def_2    soo2@a
def_2    foo2@a
ghi_3    foo2@a
ghi_3    soo1@a

Moderator's Comments:
Mod Comment Code tags for code, please.

Last edited by Corona688; 05-15-2012 at 02:24 PM..
# 2  
Old 05-15-2012
Code:
awk 'NR==FNR { A[$1]=$2; O[++L]=$1; next }; ($1 in A) { A[$1]=$2 }; END { for(N=1; N<=L; N++) print O[N], A[O[N]]; }' file1 file2 > new_file1

# 3  
Old 05-15-2012
Hi! Thanks for the reply.It does not produce the desired output, though.
What I end up with is duplicate column2 for the same column1, in file1.
For example, the o/p from the awk code gives:
Code:
abc_1 soo1@a
abc_1 soo1@a
def_2 soo2@a
def_2 soo2@a
ghi_3 soo1@a
ghi_3 soo1@a


Last edited by Scrutinizer; 05-15-2012 at 02:38 PM.. Reason: code tags
# 4  
Old 05-15-2012
Code:
$ cat repcol.awk

NR==FNR && $1 { I[$1]++;        A[$1,I[$1]]=$2; next    }

$1 {    O[$1]++;        if( ($1 SUBSEP O[$1]) in A)     A[$1,O[$1]]=$2; }

END {
        for(X in A)
        {
                split(X, B, SUBSEP)
                print B[1],     A[X];
        }
}

$ awk -f repcol.awk data1 data2

abc_1 foo3@a
abc_1 soo1@a
def_2 foo2@a
ghi_3 foo2@a
def_2 soo2@a
ghi_3 soo1@a

$

# 5  
Old 05-15-2012
Thank you, it works! But it re-arranges the lines in file1.I'd like to keep the same order. Also, if there are multiple columns after column2( say column 3, column 4... column 8) in file1 that needs to be retained,how would the code have to be modified?
Your code o/p is now
Code:
ghi_3 foo2@a
ghi_3 soo1@a
abc_1 foo3@a
abc_1 soo1@a
def_2 foo2@a
def_2 soo2@a

---------- Post updated at 02:06 PM ---------- Previous update was at 01:12 PM ----------

Would you by any chance be able to modify the existing code to get that done?Also, would you pls. explain what you have here?

Last edited by Scrutinizer; 05-15-2012 at 03:13 PM.. Reason: code tags
# 6  
Old 05-15-2012
Bumping up posts or double posting is not permitted in these forums.

Please read the rules, which you agreed to when you registered, if you have not already done so.

You may receive an infraction for this. If so, don't worry, just try to follow the rules more carefully. The infraction will expire in the near future

Thank You.

The UNIX and Linux Forums.
# 7  
Old 05-15-2012
FYI, your bumping didn't even bring me here. I was reviewing my own posts for replies as I sometimes do.

Quote:
Originally Posted by prashali
Thank you, it works! But it re-arranges the lines in file1.I'd like to keep the same order. Also, if there are multiple columns after column2( say column 3, column 4... column 8) in file1 that needs to be retained,how would the code have to be modified?
Your original output didn't keep the columns, so neither did I.

Working on it.

Last edited by Corona688; 05-15-2012 at 05:18 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

If pattern in column 3 matches pattern in column 2 (any row), print value in column 1

Hi all, I have searched and searched, but I have not found a solution that quite fits what I am trying to do. I have a long list of data in three columns. Below is a sample: 1,10,8 2,12,10 3,13,12 4,14,14 5,15,16 6,16,18 Please use code tags What I need to do is as follows: If a... (4 Replies)
Discussion started by: bleedingturnip
4 Replies

2. Shell Programming and Scripting

Overwrite specific column in xml file with the specific column from adjacent line

I have an xml file dumped from rrd file, that I want to "patch" so the xml file doesn't contain any blank hole in the resulting graph of the rrd file. Here is the file. <!-- 2015-10-12 14:00:00 WIB / 1444633200 --> <row><v> 4.0419731265e+07 </v><v> 4.5045912770e+06... (2 Replies)
Discussion started by: rk4k
2 Replies

3. Shell Programming and Scripting

Remove entire line from a file if 1st column matches a pattern

I have one requirement to delete all lines from a file if it matches below scenario. File contains three column. Employee Number, Employee Name and Employee ID Scenario is: delete all line if Employee Number (1st column) contains below 1. Non-numeric Employee Number 2. Employee Number that... (3 Replies)
Discussion started by: anshu ranjan
3 Replies

4. Shell Programming and Scripting

CSV file REPLACE COLUMN if it matches

I have a file cat 1.txt AAAA , BBBB , CCCC , DDDD DFDF , DFDF , DFDF , FDDD AA11 , DFDF , 0000 , UTIO ADSD , WERT, 0000 , JKJL If the 3rd column is not equal to "0000" , then it should replace "0000" with "XXXX" and if its equal to "0000" then print the line as it is. need help. (9 Replies)
Discussion started by: aravindj80
9 Replies

5. Shell Programming and Scripting

Difference of the same column when two other column matches and one column differs less than 1 hour

This is my input file : # cat list 20130430121600, cucm, location,76,2 20130430121600,cucm1,location1,76,4 20130430122000,cucm,location,80,8 20130430122000,cucm1,location1,90,8 20130430140000,cucm1,location1,87,11 20130430140000, cucm,location,67,9 This is the required output ... (1 Reply)
Discussion started by: Lakshmikumari
1 Replies

6. Shell Programming and Scripting

Help with replace column data with specific word

Input file: Populus_trichocarpa 30 0 50 0 0 US Vitis_vinifera 1 18 2 8 6 US Populus_trichocarpa 1 5 100 0 0 US Arabidopsis_lyrata_subsp._lyrata 0 90 0 0 0 US Glycine_max 0 2 3 0 70 UK Desired output file: Populus_trichocarpa YES NO YES NO NO US Vitis_vinifera YES YES YES YES YES US... (4 Replies)
Discussion started by: perl_beginner
4 Replies

7. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

8. Shell Programming and Scripting

Print out specific pattern column data

Input file: adc_0123 haa_1000 bcc_520 adc_0150 bcc_290 adc_0112 haa_8000 adc_0139 haa_7000 Output file: adc_0123 adc_0123 haa_1000 bcc_520 adc_0150 adc_0150 bcc_290 (3 Replies)
Discussion started by: patrick87
3 Replies

9. Shell Programming and Scripting

replace a column in a file if it matches certain pattern

Hi, I want to replace a column in a file if it matches certain pattern. Can you help me on this. Here is the file content. 000000 1111111 2222222 011111 0123445 1234556 023445 1111111 2343455 if second column contains 1111111 i need to replace it with 0000000 Can you... (6 Replies)
Discussion started by: Krrishv
6 Replies

10. Shell Programming and Scripting

Replace if regex on specific column matches expression?

I am attempting to convert rewrite rules to Nginx, and since due to the mass amount of rewrites we must convert, I've been trying to write a script to help me on a specific part, easily. So far I have this: rewrite ^action/static/(+)/$ staticPage.php?pg=$1&%$query_string; What I want done... (5 Replies)
Discussion started by: EXT3FSCK
5 Replies
Login or Register to Ask a Question