tag:blogger.com,1999:blog-6819918850004550847.post8328453895736312464..comments2024-03-18T23:08:38.134-07:00Comments on >The Unix Shell: File comparisons using awk: Match columnsVijayhttp://www.blogger.com/profile/10095262085205803368noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-6819918850004550847.post-13659581191625597602014-12-23T04:51:06.865-08:002014-12-23T04:51:06.865-08:00I want to cmpare two files columnwise in unix usin...I want to cmpare two files columnwise in unix using shell script <br />file1 <br />datasrid BMStrid Mersionid country curr<br />Met_CCD V14121011081 Recent US USD<br />Met_CCD V14121011082 Recent US USD<br />Met_CCD V14121011083 Recent GB GDB<br />Met_CCD V14121011084 Recent IE GDB<br />Met_CCD V14121011085 Recent GB GDB<br />Met_CCD V14121011086 Recent AU AUD<br />Met_CCD V14121011086 Recent HK HKD<br />Met_CCD V14121011087 Recent IE GDB<br /><br /><br />file2 <br />datasrid BMStrid Mersionid country curr<br />Met_CCD V14121011081 Recent US USD<br />Met_CCD V14121011082 Recent US USD<br />Met_CCD V14121011083 Recent GB GDB<br />Met_CCD V14121011088 Recent IE GDB<br />Met_CCD V14121011085 Recent HK GDB<br />Met_CCD V14121011086 Recent AU AUD<br />Met_CCD V14121011086 Recent HK HKD<br />Met_CCD V14121011087 Recent IE GDB<br /><br />Outputfile <br /><br />need to compare file2 wrt file1. <br />change in any cell should get highlighted in output file. <br />like <br />o/p file should contain <br />Met_CCD 'V14121011088' Recent IE GDB<br />Met_CCD V14121011085 Recent 'HK' GDB<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6819918850004550847.post-60439083998955708172013-01-14T22:49:28.126-08:002013-01-14T22:49:28.126-08:00awk 'FNR==NR{a[$0];next}($1 in a)' file3 f...awk 'FNR==NR{a[$0];next}($1 in a)' file3 file4<br /><br />FNR->line number of the file.<br />NR->line number of all collected data of all the files.<br />So the first thing is:<br />FNR==NR->this condition will be a succes untill all the lines in the first file are completed processing.As soon as all the lines in the file3 are completed,FNR will be set back to 1 and NR will continue with its numbering.<br /><br />So untill this condition satisfies the array a keeps on building with $0(which is the complete line of file3 here).So at the end of file3 the array has all the lines of file3.<br />next is like continue in c language it will tell awk to start processing the next line.<br /><br />The rest of the code ($1 in a) will applied only after all the lines in file3 are completed(that is from first line of file4).$1 represents the first field of file4.<br />($1 in a) will check whether ther is a $1 as a key in the array a.If success this will print the lineVijayhttps://www.blogger.com/profile/10095262085205803368noreply@blogger.comtag:blogger.com,1999:blog-6819918850004550847.post-38112171038479154882013-01-13T16:14:36.278-08:002013-01-13T16:14:36.278-08:00Can you please explain how it is working ?Can you please explain how it is working ?Anonymoushttps://www.blogger.com/profile/14033667254910376058noreply@blogger.com