Column matched in two files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Column matched in two files
# 1  
Old 03-26-2008
Column matched in two files

I have two files with multiple columns separated by commas.

I want to search one column from the first file in the second file;
If there is a matching, will append that matched row to the first file. Let me show an example... (unique values in the search column)
First file
aa,reg1,bb,cc,dd,ff
xx,reg2,dfd,dfc,dfdas
cv,reg3,dfd,dfc,erwqe
df,xcv.df,cv,'',;l

I would like to search the second column of the first file with the fifth column of the second file.
Second file
111,334,77,888,reg7
adf,5435,7652,425,reg3
dfa,1,23,N,
adf,2,56,76,pop

The result should be
aa,reg1,bb,cc,dd,ff
xx,reg2,dfd,dfc,dfdas
cv,reg3,dfd,dfc,erwqe,adf,5435,7652,425
df,xcv.df,cv,'',;l


How can I do this please?

Thank you very much
# 2  
Old 03-26-2008
If sorting the files is not too much of a burden, look at the join command.
# 3  
Old 03-27-2008
how to sort the file in this case? which column is to be sorted?
Thanks
# 4  
Old 03-27-2008
Read the manual page for the join command, it explains what needs to be sorted and provides some examples.
# 5  
Old 03-27-2008
Code:
awk 'NR == FNR {
t = $5
sub(/,[^,]*$/,"")
x[t] = $0
next
}
$2 in x {
$0 = $0FS x[$2]
}1' FS=, file2 file1

Use nawk or /usr/xpg4/bin/awk on Solaris.
# 6  
Old 03-27-2008
column matching

you can use
sort -t, +1 -2d first file > new1
sort -t, +4 -5d second file > new2
join -1 2 -2 5 -t, -o 1.1 2.5 1.2 1.3 1.4 1.5 new1 new2 | sort -t, +1 -2 > new3

Here the -t, is used becasue the "," is used as delimiter.
Njoy Smilie
# 7  
Old 03-28-2008
This should work but u can find a better one

cat file1 | while read line
do
var=`echo $line | cut -d"," -f2`
cat file2 | while read value
do
val=`echo $value | cut -d"," -f5`
val4=`echo $value | cut -d"," -f4`
val3=`echo $value | cut -d"," -f3`
val2=`echo $value | cut -d"," -f2`
val1=`echo $value | cut -d"," -f1`
if [[ $var == $val ]]
then
line=`echo $line,$val1,$val2,$val3,$val4`
fi
done
echo $line
done

Regards,
aajan
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find all matched lines from two files

Hello, everyone I have two files like this: File 1: A B C D E FFile 2:A B 1 A C 2 A K 3 B A 4 D E 3 W X 2 A B 2I want to print all lines (file2) that the first two columns are consist of elements from file1. So, what I expected is : A B 1 A B 2 (2 Replies)
Discussion started by: nengcheng
2 Replies

2. UNIX for Beginners Questions & Answers

Compare two files when pattern matched

I have two files say FILE1 and FILE2. FILE1 contains 80,000 filename in sorted order and another file FILE2 contains 6,000 filenames is also in sorted order. I want to compare the filename for each file and copy them in to a folder when filename is matched. File1.txt contain 80,000... (8 Replies)
Discussion started by: imranrasheedamu
8 Replies

3. Shell Programming and Scripting

How to fetch matched records from files between two different directory?

awk 'NR==FNR{arr;next} $0 in arr' /tmp/Data_mismatch.sh /prd/HK/ACCTCARD_20160115.txt edit by bakunin: seems that one CODE-tag got lost somewhere. i corrected that, but please check your posts more carefully. Thank you. (5 Replies)
Discussion started by: suresh_target
5 Replies

4. Shell Programming and Scripting

If 2nd column is matched

Both codes not working for me. whois nba.com| sed -n '/Registry Registrant ID:/,/Registrant whois ymas.com| sed -n '/Registry Registrant ID:/,/Registrant whois ymas.com.pt| sed -n '/Registry Registrant ID:/,/Registrant whois ymas.com.pt| sed -n '/Registry Admin ID:/,/Admin Email:/p' ... (5 Replies)
Discussion started by: kenshinhimura
5 Replies

5. UNIX for Dummies Questions & Answers

How to merge two tables based on a matched column?

Hi, Please excuse me , i have searched unix forum, i am unable to find what i expect , my query is , i have 2 files of same structure and having 1 similar field/column , i need to merge 2 tables/files based on the one matched field/column (that is field 1), file 1:... (5 Replies)
Discussion started by: karthikram
5 Replies

6. Shell Programming and Scripting

Find matched patterns in a column of 2 files with different size and merge them

Hi, i have input files like below:- input1 Name Seq_ID NewID Scores MT1 A0QZX3 1.65 277.4 IVO A0QZX3 1.65 244.5 HPO A0QZX3 1.65 240.5 RgP A0Q3PP 5.32 241.0 GX1 LPSZ3S 96.1 216.9 MEL LPSS3X 4.23 204.1 LDD LPSS3X 4.23 100.2 input2 Fac AddName NewID ... (9 Replies)
Discussion started by: redse171
9 Replies

7. Shell Programming and Scripting

Find matched patterns in multiple files

Hi, I need help to find matched patterns in 30 files residing in a folder simultaneously. All these files only contain 1 column. For example, File1 Gr_1 st-e34ss-11dd bt-wwd-fewq pt-wq02-ddpk pw-xsw17-aqpp Gr_2 srq-wy09-yyd9 sqq-fdfs-ffs9 Gr_3 etas-qqa-dfw ddw-ppls-qqw... (10 Replies)
Discussion started by: redse171
10 Replies

8. Shell Programming and Scripting

how to fgrep -f two files and get only one instance of each matched line

Hello, I have two files: file1 x v r g file2 aaaa,x,1111 bbbb,v,2222 bbbb,v, cccc,r,3333 dddd,s,4444 eeee,q,5555 ffff,p,6666 (12 Replies)
Discussion started by: smarones
12 Replies

9. Shell Programming and Scripting

How to group matched patterns in different files

Hi, I have a master file that i need to split into multiple files based on matched patterns. sample of my data as follows:- scaff_1 a e 123 130 c_scaff_100 scaff_1 a e 132 138 c_scaff_101 scaff_1 a e 140 150 ... (2 Replies)
Discussion started by: redse171
2 Replies

10. Shell Programming and Scripting

Replace data of one column with data on other file corresponding to transaction ID matched

Hi All, I have two files one of which having some mobile numbers and corresponding value whose sample content as follows: 9058629605,8.0 9122828964,30.0 And in second file complete details of all mobile numbers and sample content as follows and delimeter used is comma(,): ... (8 Replies)
Discussion started by: poweroflinux
8 Replies
Login or Register to Ask a Question