Script to Match first field of two Files and print


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to Match first field of two Files and print
# 1  
Old 10-16-2009
Bug 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
==================
Code:
Alex,1,fgh
Menthos,45454,toto
Gothica,855,ee
Zenie4,77,gg
Salvatore,66,oo
Dhin,1234,papapa
Maria,2311,fcfc


InputFile2
===================
Code:
Salvatore,pop
Alex,ofgh
Amaan,iiwer
Guulu,ee
Zenie4,gg
Menthos,era

OutputFile
====================
Code:
Alex,1,fgh,Alex,ofgh
Menthos,,45454,toto,Menthos,era
Zenie4,77,gg,Zenie4,gg
Salvatore,66,oo,Salvatore,pop

First field of first line is searched in second file as first field, and found so both lines(FIles A and File B) are printed in output.

Thanks in advance.

Last edited by radoulov; 10-16-2009 at 10:44 AM.. Reason: please use code tags
# 2  
Old 10-16-2009
Hi.

Something like:

Code:
awk -F, 'NR == FNR { A[$1] = $0; next } A[$1] { print $0 FS A[$1] } ' file2 file1

Alex,1,fgh,Alex,ofgh
Menthos,45454,toto,Menthos,era
Zenie4,77,gg,Zenie4,gg
Salvatore,66,oo,Salvatore,pop

# 3  
Old 10-16-2009
Code:
#! /bin/ksh

sort -d -t, +0 -1 InputFile1 >> InputFile1_sort
sort -d -t, +0 -1 InputFile2 >> InputFile2_sort


join -1 1 -2 1 -t, -o 1.1 1.2 1.3 2.1 2.2 InputFile1_sort InputFile2_sort >> OutputFile

# 4  
Old 10-16-2009
Code:
join -t',' -j 1 <(sort infile1) <(sort infile2)

Alex,1,fgh,ofgh
Menthos,45454,toto,era
Salvatore,66,oo,pop
Zenie4,77,gg,gg

The result is with only a single name column, plus it's sorted but you might prefer it that way.
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 print text in field if match and range is met

In the awk below I am trying to match the value in $4 of file1 with the split value from $4 in file2. I store the value of $4 in file1 in A and the split value (using the _ for the split) in array. I then strore the value in $2 as min, the value in $3 as max, and the value in $1 as chr. If A is... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. 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

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 and print specific field

Hello, I have data in following format. ... (6 Replies)
Discussion started by: Pushpraj
6 Replies

5. Shell Programming and Scripting

Match two columns from two files and print output

Hello, I have two files which are of the following format File 1 which has two columns Protein_ID Substitution NP_997239 T53R NP_060668 V267M NP_058515 P856A NP_001206 T55M NP_006601 D371Y ... (2 Replies)
Discussion started by: nans
2 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

Print last occurrence if first field match

Hi All, I have an input below. If the term in the 1st column is equal, print the last row which 1st column is equal.In the below example, it's " 0001 k= 27 " and " 0004 k= 6 " (depicted in bold). Those terms in 1st column which are not repetitive are to be printed as well. Can any body help me... (9 Replies)
Discussion started by: Raynon
9 Replies

10. Shell Programming and Scripting

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. (8 Replies)
Discussion started by: phamp008
8 Replies
Login or Register to Ask a Question