This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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?
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:
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 ]
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: