Win a copy of High Performance Python for Data Analytics this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Command to compare two files starting from a certain line

 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
What a stench! Central nervous system shutting down. Save yourself tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic