LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-26-2017, 11:54 PM   #1
Shruthi GM
LQ Newbie
 
Registered: Jun 2017
Posts: 2

Rep: Reputation: Disabled
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
 
Old 06-27-2017, 12:09 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 18,233

Rep: Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708
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.
 
Old 06-27-2017, 12:15 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 20,823

Rep: Reputation: 4004Reputation: 4004Reputation: 4004Reputation: 4004Reputation: 4004Reputation: 4004Reputation: 4004Reputation: 4004Reputation: 4004Reputation: 4004Reputation: 4004
There is always another way. Depends on what you want to accomplish.
Also look at the "cmp" command.
 
Old 06-27-2017, 12:52 AM   #4
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924
Why don't you want to use the diff command?
 
1 members found this post helpful.
Old 06-27-2017, 05:48 AM   #5
aragorn2101
Member
 
Registered: Dec 2012
Location: Mauritius
Distribution: Slackware
Posts: 567

Rep: Reputation: 301Reputation: 301Reputation: 301Reputation: 301
Quote:
Originally Posted by Shruthi GM View Post
... 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.

Last edited by aragorn2101; 06-27-2017 at 05:49 AM.
 
Old 06-27-2017, 07:32 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 20,196

Rep: Reputation: 6830Reputation: 6830Reputation: 6830Reputation: 6830Reputation: 6830Reputation: 6830Reputation: 6830Reputation: 6830Reputation: 6830Reputation: 6830Reputation: 6830
not to speak about other diff tools, like meld.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: How to Use Diff to Compare Files in Linux LXer Syndicated Linux News 0 01-03-2017 03:15 AM
kconfig-diff - tool to compare kernel .config files atelszewski Slackware 2 10-31-2015 12:43 PM
How to compare/diff a range of lines from two text files jedibrand Linux - Software 1 03-26-2010 01:54 PM
Recursive diff ( File compare ) script sharathkv25 Programming 5 02-22-2007 08:39 AM
Diff compare 2 files DavidTempler Linux - Newbie 2 11-01-2004 10:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration