Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi All,
I have two files
file1:
abc,def,ghi,5,jkl,mno
pqr,stu,ghi,10,vwx,xyz
cba,ust,ihg,4,cdu,oqwfile2:
ravi,def,kishore
ramu,ust,krishna
joseph,stu,mikeI need two output files as follows
In my above example, each row in file1 has 6 fields and each row in file2 has 3 fields. I should compare field2 in both the files. If field2 is same in both the files then i should get the third field in file2 as the last field in file1.
output:
abc,def,ghi,5,jkl,mno,kishore
pqr,stu,ghi,10,vwx,xyz,mike
cba,ust,ihg,4,cdu,oqw,krishna
Also, If field3 in file1 is same as field3 in the next line then the field4 should add upto field4 in the next line and i should get a unique output as followsouput:
abc,def,ghi,15,jkl,mno
cba,ust,ihg,4,cdu,oqw
can somebody please help me with this as i require it pretty urgently. I am using sun solaris. If we can get the output using sed or awk that would be great. Any other way is also appreciated.Thanks in advance..........

This is getting pretty complex ... OS scripting is not really made for this kind of stuff, although I am sure it can be done. You should consider other means of programming here : database manipulation sounds much better suited for this kind of reporting (based on 2 tables with 6 and 3 columns), or you could use some kind of C language programming...

@tvc, its true that if OP tackles the problem at the source(ie, the database) it would be easier, but most of the time, we don't have this luxury due to various reasons. In those case, hardcore scripting can still be done. Also there's really no need to go into C programming either because the algorithm to do this task can be programmed into any language.

Still, OS programming will be the hardest way for these kind of things ... go ahead, be my guest. I don't see any code yet, I wonder why.

since you asked for it, this is for the first part, using gawk
awk -F"," 'FNR==NR{ a[$2] = $NF next } ($2 in a){ print $0,a[$2] } ' OFS="," file1 file
output# ./shell.sh abc,def,ghi,5,jkl,mno,kishore pqr,stu,ghi,10,vwx,xyz,mike cba,ust,ihg,4,cdu,oqw,krishnai don't understand the 2nd part, so not going to do it.

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |