• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Command to compare two files starting from a certain line

 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11914
209
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic