Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

compare two text files on single column

Hi,
I've saved the output of two separate runs of 'ls -l' to two text files - file1 and file2.  The listing in file2 is a superset of file1.  I'd like to compare the contents of the two files based on column 7 only (filesize) to roughly find the lines in file2 which are not in file1.  I've tried several awk one liners but they fail so miserably that it doesn't make sense to post them here.
Thank you,
Curt
0
97WideGlide
Asked:
97WideGlide
  • 2
  • 2
1 Solution
 
simon3270Commented:
Is filesize the only important value?  To compare entire lines, and only list the ones in file2 but not file1, use:

    comm -13 file1 file2
0
 
97WideGlideAuthor Commented:
Thanks for your response but my goal is to roughly find duplicate files based filesize so I would need to compare column 7 of each file only.
0
 
simon3270Commented:
Field 7?  In my "ls -l" output, the size is field 5 - field 7 is the day of the month!

Anyway, the following awk script reads in the contents of file1 and stores all of the sizes in an array (larr).  It then reads in file2 line by line, and if the size of the file in file2 doesn't match the size of any files in file1, the entire line is printed out.
awk 'BEGIN{j=0; while (0 != getline l1 < "file1") {split(l1,l1s); larr[j++] = l1s[5]}}
{f=0; for (l in larr){if ($5 == larr[l]) f=1}; if (f == 0) print}' file2

Open in new window

Please change all 5s to 7s if your "ls" is different.  Also change the "file1" and file2 to match your file names ("file1" must be in double quotes like here, file2 doesn't have to be).
0
 
97WideGlideAuthor Commented:
Thank you!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now