wood burning stoves 2.0*
The moose likes Linux / UNIX and the fly likes Command to compare two files starting from a certain line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Command to compare two files starting from a certain line" Watch "Command to compare two files starting from a certain line" New topic
Author

Command to compare two files starting from a certain line

Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
I'm a novice Linux user. I have a question that might be very trivial for you guys.

What's the command to compare two files starting from a certain line?
For example: I know that two files are always different at line 1 - 10 but I want to check whether they are different from line 11 forward (or line 11 - line 200). Is there anyway I can do this?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Let's see...

The 'tail' command prints the last 10 lines of a file, but it has more options (type 'man tail' for usage instructions). To print a file from line 10 to the end, the command is:

tail -n +10 file.txt

The command to compare files line by line is 'diff'. Now you'll have to combine the commands. I'm no expert with the Unix command line, so I don't know how to do this on one line. You could write the files without the first 10 lines to two temporary files, and compare those, something like this:

tail -n +10 file1.txt > temp1.txt
tail -n +10 file2.txt > temp2.txt
diff temp1.txt temp2.txt


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
As an additional note:
What I'm comparing is zip files. Each consists of an XML file.
Does this mean I need to uncompress before comparing? Or it's not necessary?
[ July 08, 2008: Message edited by: Susan Smith ]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11432
    
  85

Hmmm.

If all you had to do was compare 2 files, then diff would be good. If they were both compressed, then zdiff would work. But the addition of skipping 'x' lines on compressed files makes it a little more interesting.

Do you have to skip lines, or can you specify the number of bytes in those lines? If, for example, both files started with something like the current date/time, then the number of bytes would both be the same, viz:

In this case, while it is true that I might want to skip the first 8 lines as these differ, I can equally skip the first 145 characters - both file 1 and file 2 have the same number of characters in those first 8 lines.

If this is the case, then the cmp tool will let you compare the 2 files from a particular starting byte. And there is a corresponding zcmp for dealing with compressed files.

Unfortunately the cmp tool is not designed for comparing ASCII text, so it's output may not be useful for you. So you probably want to go with Jesper's suggestion on creating interim files and then comparing them.

Given that they are compressed, you will need to decompress them before tailing them. Or combine the decompression and tail commands into one command:

Regards,

Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Command to compare two files starting from a certain line