match field between 2 files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting match field between 2 files
# 1  
Old 01-21-2008
match field between 2 files

I would like to do the following in bash shell.

file a

a:1
b:2
c:3

file b

a:work:apple
b:baby:banana
c:candy:cat
d:desk:dog

I would like to match field 1 in file a to file b, if there's a match I would like
to append field 2 in file a to field 3 in file b.

Thank you.
# 2  
Old 01-21-2008
Try:
Code:
join -t: -j 1  -o 2.1,2.2,2.3,1.2 fileA fileB

# 3  
Old 01-21-2008
thank you for your quick post. I was wondering if this can be done with awk ?
# 4  
Old 01-21-2008
actually, I want the output appear like this:

a:work:apple1
b:baby:banana2
c:candy:cat3

Thanks
# 5  
Old 01-21-2008
Code:
awk 'FNR==NR { a[$1]=$2;next} $1 in a {print $0a[$1] }' FS=":" file1 file2

# 6  
Old 01-21-2008
MySQL

thank you much, this exactly what I need.
# 7  
Old 01-23-2008
I have same problem with phamp008 .
So if file a :
work
baby
candy

file b

a:work:apple
b:baby:banana
c:candy:cat
d:desk:dog

How can i do to ouput :

a:work:apple
b:baby:banana
c:candy:cat
Thank U!
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 match field between two files and use conditions on match

I am trying to look for $2 of file1 (skipping the header) in $2 of file2 (skipping the header) and if they match and the value in $10 is > 30 and $11 is > 49, then print the line from file1 to a output file. If no match is foung the line is not printed. Both the input and output are tab-delimited.... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

awk to match value to a field within +/- value

In the awk below I use $2 of filet to search filea for a match. If the values in $2 are exact match this works great. However, that is not always the case, so I need to perform the search using a range of + or - 2. That is if the value in filea $2 is within + or - 2 of filet $2 then it is matched.... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

Command/script to match a field and print the next field of each line in a file.

Hello, I have a text file in the below format: Source Destination State Lag Status CQA02W2K12pl:D:\CAQA ... (10 Replies)
Discussion started by: pocodot
10 Replies

4. Shell Programming and Scripting

Match columns from two csv files and update field in one of the csv file

Hi, I have a file of csv data, which looks like this: file1: 1AA,LGV_PONCEY_LES_ATHEE,1,\N,1,00020460E1,0,\N,\N,\N,\N,2,00.22335321,0.00466628 2BB,LES_POUGES_ASF,\N,200,200,00006298G1,0,\N,\N,\N,\N,1,00.30887539,0.00050312... (10 Replies)
Discussion started by: djoseph
10 Replies

5. Shell Programming and Scripting

Match complete field

hello, i have a little problem, i want match the complete field ($1 or $2) with a complete word in another variable. example: i have a file with either one or two words per lane: hsa-mir-4449 hsa-mir-4707 hsa-mir-4707* hsa-mir-4707 novelMiR_3551 novelMiR_3563 novelMiR_4330... (4 Replies)
Discussion started by: ace13
4 Replies

6. UNIX for Dummies Questions & Answers

Match pattern in a field, print pattern only instead of the entire field

Hi ! I have a tab-delimited file, file.tab: Column1 Column2 Column3 aaaaaaaaaa bbtomatoesbbbbbb cccccccccc ddddddddd eeeeappleseeeeeeeee ffffffffffffff ggggggggg hhhhhhtomatoeshhh iiiiiiiiiiiiiiii ... (18 Replies)
Discussion started by: lucasvs
18 Replies

7. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

8. Shell Programming and Scripting

Match two files and divide a field !

Hello, I have two files that have the date field as a common. I request your help with some script that divide the value of the file1 by the value in the file2 only when the field date are the same between both files and create a new text file. This is a sample of the files file1... (1 Reply)
Discussion started by: csierra
1 Replies

9. Shell Programming and Scripting

Script to Match first field of two Files and print

Hi, I want to get script or command in Sun Unix which matches first fields of both the files and print the feilds of one files, example may make it more clear. InputFile1 ================== Alex,1,fgh Menthos,45454,toto Gothica,855,ee Zenie4,77,gg Salvatore,66,oo Dhin,1234,papapa... (3 Replies)
Discussion started by: indian.ace
3 Replies

10. Shell Programming and Scripting

first field match

Hi All, Thanks for your help in advance I am parsing a log file where the first field of each line can have the same key value but not more than 3 times in a row. Varying value of that first field changes as you go through the log but it either appears 3 times or two and sometimes only once.... (1 Reply)
Discussion started by: dragrid
1 Replies
Login or Register to Ask a Question