This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
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 REST with Spring (video course) this week in the Spring 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

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: 15038

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 8 API documentation
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: 11765


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:



The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Consider Paul's rocket mass heater.
subject: Command to compare two files starting from a certain line
It's not a secret anymore!