LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 07-25-2012, 11:23 AM   #1
dsfreddie
LQ Newbie
 
Registered: Jun 2012
Posts: 11

Rep: Reputation: Disabled
LINUX - How to compare the values in 2 files & exit from the script


Hi All,

I have a requirement where I need to compare 2 files & if the values in the files match, it should proceed, else exit the script without proceeding further.

For e.g : Scenario 1
Quote:
cat File A
20120701,20120702,20120703

cat File B
20120701,20120702
In this case, the script should exit without proceeding further.

Scenario 2
Quote:
cat File A
20120701,20120702,20120703

cat File B
20120701,20120702,20120703
In this case, the script should proceed further.

Scenario 3
Quote:
cat File A
20120701,20120702,20120703

cat File B
20120701,20120702,20120704
In this case, the script should exit.

Can anybody shed some light on how to accomplish this.

Thanks Much
Freddie
 
Old 07-25-2012, 11:36 AM   #2
smilemukul
Member
 
Registered: Jun 2009
Distribution: Redhat,CentOS,Ubuntu,Puppet
Posts: 292

Rep: Reputation: 34
It can be compared & matched with md5sum fileA & md5sum fileB or diff command
 
Old 07-25-2012, 11:44 AM   #3
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,246

Rep: Reputation: 309Reputation: 309Reputation: 309Reputation: 309
What about order of values, are both files always sorted the same way? For example, what should happen on this:
Quote:
cat File A
20120701,20120702,20120703

cat File B
20120703,20120701,20120702
And what about white spaces?
 
Old 07-25-2012, 05:25 PM   #4
Sydney
Member
 
Registered: Mar 2012
Distribution: Scientific Linux
Posts: 147

Rep: Reputation: 36
Adding to Smilemukul's comment you may also want to check out the comm command in bash it has some nice switches.
 
Old 07-25-2012, 06:14 PM   #5
Paddy Landau
LQ Newbie
 
Registered: Jul 2012
Location: United Kingdom
Distribution: Ubuntu
Posts: 2

Rep: Reputation: Disabled
The diff command returns zero if the files are the same, or non-zero if they differ.

You can use an if-statement, or just the following single line:

Code:
diff 'File A' 'File B' || exit
 
  


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
[SOLVED] Script count files in the directory but keep loop not exit untill see 10 files. dotran Linux - Newbie 9 12-22-2014 05:34 AM
script to compare users in files s_linux Programming 7 04-05-2011 11:34 AM
[SOLVED] exit in script files cause konsole terminal itself to exit Star_Gazer Programming 11 04-12-2010 09:32 AM
How to Compare Two files using shell script pooppp Linux - Networking 14 08-05-2008 04:35 AM
How do I compare two values in separate files xmrkite Linux - Software 11 09-20-2007 12:36 AM


All times are GMT -5. The time now is 02:37 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration