简体   繁体   中英

Linux : How can I print line number and column number when values do not match for tab separated files using AWK in linux

Compare File 1 vs File2 and print line no. for difference record and column no of difference present in file2.

In file1:

User_ID   First_name    Last_name   Address                       Postal_code 
User_1    fistname      Lastname    35, Park Lake, California           32068    
user2     Johnny        Depp        32, Park Lake, California               
user3     Tom           Cruise      5322 Otter Lane Middleberge         32907    
user4     Leonardo      DiCaprio    Half-Way Pond, Georgetown           1230    
user5     Sylvester     Stallone    6762,33 Ave N,St. Petersburg        33710   
user6     Srleo         Stallone    6762,33 Ave N,St. Petersburg        33700

and

In file2:

User_ID   First_name    Last_name   Address                       Postal_code     
User_1    fistname      Lastname    35, Park Lake, California           32068    
user2     Johnny        Depp        32, NEW Street, California          96206    
user30    Tom           Cruise      5322 Otter Lane Middleberge         32907   
user4     Leonardo      DiCaprio'   Half-Way Pond, Georgetown           00000    
user5     Sylvester     Stallone    6762,33 Ave N,St. Petersburg        33710   
user7     Nicolas       Cage                                            55010    
user6     Srleo         Stallone    6762,33 Ave N,St. Petersburg        33700

**Expected Result:-

Difference in file2 is line number followed by column number (where the values do not match)**

Line No. 2 COLUMN NO- 4,5    
Line No. 3 COLUMN NO-1    
Line No. 4 COLUMN NO 3,5    
Line No. 5 COLUNN NO 5    
Line No. 6 COLUMN NO 1,2,3,4,5

Note: File size to be compare is in GB and File is tab separated and has more than 400 tab separated column.

I am using-

awk 'NR==FNR{Arr[$0]++;next}!($0 in Arr){print FNR}' file1 file2

However, it gives me the line numbers and not the Column Numbers

this should do, however doesn't match your expected result

paste f1 f2 | 
awk -F'\t' 'NR==1 {n=NF/2} 
                  {for(i=1;i<=n;i++) 
                     if($i!=$(i+n)) 
                       {c=c s i; s=","} 
                   if(c) 
                     {print "Line No. " NR-1 " COLUMN NO " c;
                      c="";s=""}}'

Line No. 2 COLUMN NO 4,5
Line No. 3 COLUMN NO 1
Line No. 4 COLUMN NO 3,5
Line No. 6 COLUMN NO 1,2,3,4,5
Line No. 7 COLUMN NO 1,2,3,4,5

either you're not comparing line by line or some following some other unwritten spec.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM