• Post Reply Bookmark Topic Watch Topic
  • New Topic

diff in two files in UNIX,, help me  RSS feed

 
ramesh sk
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi , i have two sorted files having huge records( may be in lacs), both are in sorted order, and some records are missing in file2 when compared to file1. I used diff command in UNIX, it displaying the missing records,
but i want that missing records in another file in sorted order, please help me in this matter,
thanks,
Ramesh
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this other file, you want the missing ones in sorted order in, a third file, just containing the missings?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That file will contain information about those lines, but it will contain entire diff records, not just the lines of interest. If he wants to process those lines somehow, this won't do.

The only way I could think of to print just those lines is to combine the files, sort them together, then use "uniq -u" to print only the non-duplicated lines:

cat file1 file2 | sort | uniq -u > file3

I bet there's a better way to do it, but I can't think of it right now (of course, you could write a little program in the language of your choice to do it; but thinking of ways to do it with shell tools is more fun!)
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right, efh, - diff displays informations about line-numbers.

But we could filter only the content with grep, and cut off the marks with sed:

 
Tim Holloway
Saloon Keeper
Posts: 18789
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is gnu diff, and may not be available for things like Solaris diff, but I think that it might work:

You might have to override --old-line-format and --unchanged-line-format to not emit anything.

However, you said both files are sorted and that file2 has everything that file1 has plus more. Uniq can handle that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!