Computing.Net > Forums > Unix > Compare two files using awk or sed, add value

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Compare two files using awk or sed, add value

Reply to Message Icon

Name: yerruhari
Date: November 6, 2009 at 23:47:58 Pacific
OS: Sun Solaris
Subcategory: General
Comment:

Hi All,

I have two files

file1:

abc,def,ghi,5,jkl,mno
pqr,stu,ghi,10,vwx,xyz
cba,ust,ihg,4,cdu,oqw

file2:

ravi,def,kishore
ramu,ust,krishna
joseph,stu,mike

I 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 follows

ouput:

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



Sponsored Link
Ads by Google

Response Number 1
Name: tvc
Date: November 11, 2009 at 04:00:21 Pacific
Reply:

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


0

Response Number 2
Name: ghostdog
Date: November 14, 2009 at 00:03:44 Pacific
Reply:

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

GNU win32 packages | Gawk


0

Response Number 3
Name: tvc
Date: November 14, 2009 at 06:56:11 Pacific
Reply:

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.


0

Response Number 4
Name: ghostdog
Date: November 14, 2009 at 08:22:19 Pacific
Reply:

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,krishna

i don't understand the 2nd part, so not going to do it.

GNU win32 packages | Gawk


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Compare two files using awk or sed, add value

Editing a file without awk or sed www.computing.net/answers/unix/editing-a-file-without-awk-or-sed/5437.html

Increment a number using awk or sed www.computing.net/answers/unix/increment-a-number-using-awk-or-sed/6827.html

trouble merging two files with awk www.computing.net/answers/unix/trouble-merging-two-files-with-awk/7937.html