LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script to compare two files without using diff command (https://www.linuxquestions.org/questions/linux-newbie-8/script-to-compare-two-files-without-using-diff-command-4175608686/)

Shruthi GM 06-26-2017 11:54 PM

Script to compare two files without using diff command
 
Hi,

Could you please let me know if its possible/feasible to write a unix shell script to compare two files without using diff command or its better to use diff command.

Thanks in advance
Shruthi

chrism01 06-27-2017 12:09 AM

I usually just use diff with these flags
Code:

diff -Nuw  <orig> <new>
but you can always put that in a script if it makes sense to do so.
It really depends on why you are doing this.

syg00 06-27-2017 12:15 AM

There is always another way. Depends on what you want to accomplish.
Also look at the "cmp" command.

hydrurga 06-27-2017 12:52 AM

Why don't you want to use the diff command?

aragorn2101 06-27-2017 05:48 AM

Quote:

Originally Posted by Shruthi GM (Post 5727620)
... to compare two files without using diff command or its better to use diff command.

Hi Shruti and welcome to LQ,

I also wanted to ask, why not simply use diff?

Then I thought, let me just answer your question as a general interrogation. Firstly, yes, we can write a script to compare two files. It will require many lines of codes as you will need to take file names as arguments, test if they exist and are valid files, read line by line and at the same time worry about file format, then finally compare and output the results intelligently. To sum up, it will take a good amount of time to do that and I am not sure it will be better than diff.

So, let's use diff instead. You can do the job very easily with a one line command. People have put considerable effort in developing a nice program as diff so that you don't have to go through all the hassle of writing the program yourself. So, you can be pretty sure it will work quite nicely.

And as the people before you did, if someday you have an original idea for a command or a useful script, you can develop it, so that the people after you can benefit. This is part of the philosophy of open source and "Linuxing" in general.

pan64 06-27-2017 07:32 AM

not to speak about other diff tools, like meld.


All times are GMT -5. The time now is 04:00 PM.