Find file size difference in two files using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find file size difference in two files using awk
# 1  
Old 02-21-2011
Find file size difference in two files using awk

Hi,

Could anyone help me to solve this problem?

I have two files "f1" and "f2" having 2 fields in each, a) file size and b) file name. The data are almost same in both the files except for few and new additional lines. Now, I have to find out and print the output as, the difference in the "file size" for the file names present in the f1 and f2 and also, fields that are not present in both the files.

Note: the number of lines in the files may differ. And the file sizes are in KB.

For eg: suppose if the files are
Code:
  
   f1                   f2
-----------       -----------
2   a.xml          9    a.xml
7   b.xml          7    b.xml
3   c.xml          3    c.xml
6   d.xml          9    d.xml
4   t1.def         8    t3.def
5   f3.jpg

Output should be:
Code:
7    a.xml 
3    d.xml
4    t1.def
8    t3.def
5    f3.jpg


Last edited by royalibrahim; 02-21-2011 at 08:07 AM..
# 2  
Old 02-21-2011
Try:
Code:
awk 'NR==FNR{a[$2]=$1}NR!=FNR{a[$2]-=$1}END{for (i in a){if (a[i]!=0){print (a[i]<0)?-a[i]:a[i],i}}}' f1 f2

# 3  
Old 02-22-2011
Quote:
Originally Posted by bartus11
Try:
Code:
awk 'NR==FNR{a[$2]=$1}NR!=FNR{a[$2]-=$1}END{for (i in a){if (a[i]!=0){print (a[i]<0)?-a[i]:a[i],i}}}' f1 f2

Thank you bartus, it worked perfectly. But, I cannot decode the logic. Could you please explain this to me?

Few doubts in this:
NR!=FNR --> this is needed because I told that counting of the lines may not be same in both the files? Or this is an indicator to parse the second file? If that so, this explicit indication is really needed to make awk to parse the 2nd file? Please clarify me.

Suppose if I want to print the common lines (similar lines not the unique) in both the files also, then I have to just remove the condition if (a[i]!=0)?

Last edited by royalibrahim; 02-22-2011 at 03:42 AM..
# 4  
Old 02-22-2011
Quote:
Originally Posted by royalibrahim
Thank you bartus, it worked perfectly. But, I cannot decode the logic. Could you please explain this to me?

Few doubts in this:
NR!=FNR --> this is needed because I told that counting of the lines may not be same in both the files? Or this is an indicator to parse the second file? If that so, this explicit indication is really needed to make awk to parse the 2nd file? Please clarify me.
You second guess is correct. NR==FNR means parsing 1st file, and NR!=FNR - second. It can be written differently thought:
Code:
'NR==FNR{a[$2]=$1;next}{a[$2]...

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk code to find difference in second file which is not present in first file .

Hi All, I want to find difference between two files and output only lines which are not present in second file .I am using awk and I am getting only the first difference but I want to get all the lines which are not present in file2 .Below is the code I am using . Please help to get the desired... (7 Replies)
Discussion started by: srinivasrao
7 Replies

2. Shell Programming and Scripting

To find difference between two files on a whole

Hi, The requirement is to compare two files that has single column of records each. Comparison is to happen on a whole and not line by line. File1.txt 314589929 315611087 304924413 315989094 301171509 302984393 315609549 314593632 File2.txt 315611087 304924413 315989094 (2 Replies)
Discussion started by: anandek
2 Replies

3. UNIX for Advanced & Expert Users

Find difference between 2 files

I have 2 files as follows. file1.txt <cell>123</cell> <cell>345</cell> file2.txt <cell>123</cell> <cell>456</cell> out out should be output.txt <cell>456></cell> How do we achieve this> The difference betwenn the two files should be wirtten to the output file.. ... (2 Replies)
Discussion started by: kanthrajgowda
2 Replies

4. Shell Programming and Scripting

Find duplicate files by file size

Hi! I want to find duplicate files (criteria: file size) in my download folder. I try it like this: find /Users/frodo/Downloads \! -type d -exec du {} \; | sort > /Users/frodo/Desktop/duplicates_1.txt; cut -f 1 /Users/frodo/Desktop/duplicates_1.txt | uniq -d | grep -hif -... (9 Replies)
Discussion started by: Dirk Einecke
9 Replies

5. Shell Programming and Scripting

Columns comparision of two large size files and printing the difference

Hi Experts, My requirement is to compare the second field/column in two files, if the second column is same in both the files then compare the first field. If the first is not matching then print the first and second fields of both the files. first file (a .txt) < 1210018971FF0000,... (6 Replies)
Discussion started by: krao
6 Replies

6. UNIX for Dummies Questions & Answers

Find difference between two files

I have two files as below File1: a b c d File2: a b When i find the difference the output would be c&d.. How can i get my requirement...pls help... Many thanks in advance (10 Replies)
Discussion started by: jagadish_gaddam
10 Replies

7. Shell Programming and Scripting

find with file size and show the size

Hi All... is the below command be modified in sucha way that i can get the file size along with the name and path of the file the below command only gives me the file location which are more than 100000k...but I want the exact size of the file also.. find / -name "*.*" -size +100000k ... (3 Replies)
Discussion started by: rpraharaj84
3 Replies

8. Emergency UNIX and Linux Support

to find difference between two files

I have a file which gets appended with records daily..for eg. 1st day of the month i get 9 records ,2nd day 9 records .....till the last day in the month...the no of records may vary...i store the previous days file in a variable oldfile=PATH/previousdaysfile....i store the current days file in a... (6 Replies)
Discussion started by: ganesh_248
6 Replies

9. Solaris

command to find out total size of a specific file size (spread over the server)

hi all, in my server there are some specific application files which are spread through out the server... these are spread in folders..sub-folders..chid folders... please help me, how can i find the total size of these specific files in the server... (3 Replies)
Discussion started by: abhinov
3 Replies

10. Filesystems, Disks and Memory

Strange difference in file size when copying LARGE file..

Hi, Im trying to take a database backup. one of the files is 26 GB. I am using cp -pr to create a backup copy of the database. after the copying is complete, if i do du -hrs on the folders i saw a difference of 2GB. The weird fact is that the BACKUP folder was 2 GB more than the original one! ... (1 Reply)
Discussion started by: 0ktalmagik
1 Replies
Login or Register to Ask a Question