How to merge two different files in Linux

17 Feb, 2010

sdiff command

In yesterday’s post, we learned how to find the discrepancies between two similar files using a command called diff. With it, we were able to compare them and identify the actual lines containing differences.

Once you have identified the differences between the two, you have a couple of options. If only one file is different, you can delete it, but if both files have slight differences and you need the changes from both, you will need to include the changes from both. That would normally involve creating a third file or revision containing both changes.

With the command sdiff you can display the file differences side-by-side and can also merge the two file changes into a third output file. To run sdiff, use the following command string:

sdiff -o outfile file1.txt file2.txt

It will display the two differences (see the image above). Press Enter, and it will then provide you with options. Choose the one you want and then Quit.

(0) Comment Categories : Software, VPS & Dedicated
Tag: , , , , , ,

How to compare two files in Linux

16 Feb, 2010

diff command
Question: I have two scripts in a directory on my server. One is the right one and one is not, but I do not remember which is which. How can I compare the two files?

Answer: Linux and UNIX-like operating systems usually come with a comparison command called “diff”. This command will display line-by-line differences between two files. It is useful for software developers, but web developers and web application managers will find it useful as well.

To use it, just enter:

diff script1.php script2.php

It will search through both PHP scripts and find any lines that are different. The output will look something like this:

if(!function_exists('add_action')){
header('HTTP/1.0 404 Not Found');
header('Location: ../../');
> exit();
}

In the first file “exit();” is not present, while the second file correctly contains it. Tomorrow we will learn how to merge the two differing files into one correct file.

(0) Comment Categories : Software, VPS & Dedicated
Tag: , , , , ,