LINUX - How to compare the values in 2 files & exit from the script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting LINUX - How to compare the values in 2 files & exit from the script
# 1  
Old 07-26-2012
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.

PLS NOTE : The date values in File A & B can be in any order. So, i think a diff command might not work.

Can anybody shed some light on how to accomplish this.

Thanks Much
Freddie

Last edited by dsfreddie; 07-26-2012 at 07:01 PM..
# 2  
Old 07-26-2012
Quote:
Originally Posted by dsfreddie
PLS NOTE : The date values in File A & B can be in any order. So, i think a diff command might not work.
Does that mean that the following is considered a match?
Code:
cat File A
20120701,20120703,20120702
cat File B
20120703,20120702,20120701

Regards,
Alister
# 3  
Old 07-26-2012
Thanks Allister for your reply Smilie

Yes, the example you gave is considered a match.

Regards,
Freddie
# 4  
Old 07-26-2012
The following changes each comma into a newline, effectively converting the file to one that has one date per line. The lines are then sorted and the results are silently compared. cmp will not generate any output, but its exit status can be used to make the decision.
Code:
cmp -s <(tr , \\n < file1 | sort) <(tr , \\n < file2 | sort)

If you prefer using temporary files over process substitution:
Code:
tr , \\n < file1 | sort > file1.sorted
tr , \\n < file2 | sort > file2.sorted
cmp -s file1.sorted file2.sorted
rm file1.sorted file2.sorted

Regards,
Alister

Last edited by alister; 07-27-2012 at 12:47 AM.. Reason: Woops. Forgot to include the sort commands in the second suggestion.
# 5  
Old 07-26-2012
Thanks Alister, that worked like a champ Smilie I am currently testing my script after incorporating this. Will let you know the outcome...

Regards,
Freddie
# 6  
Old 08-19-2012
Hi Alister,

I am getting a syntax error message while trying to compare 2 files using the compare function (LINUX)

Code:
cmp -s <(tr , \\n < $COMMON_TMP/nt_per_gs.done | sort) <(tr , \\n < $COMMON_TMP/nt_per_chg_indx.done | sort)

Error Message:

Code:
 
command substitution: line 79: syntax error near unexpected token `('
command substitution: line 79: `cmp -s <(tr , \n < $COMMON_TMP/nt_per_gs.done | sort) <(tr , \n < $COMMON_TMP/nt_per_chg_indx.done | sort)'
+ file_cmp_chk=1


Code:
 
$ cat /iis_dev_data3/wcc/cpmg/tmp/wcc_cpmg_nt_per_chg_indx.done
20120708,20120707
$cat /iis_dev_data3/wcc/cpmg/tmp/wcc_cpmg_nt_per_gs.done
20120708,20120707

Can you pls help to fix this issue.

Thanks
Freddie

Last edited by methyl; 08-19-2012 at 08:27 PM.. Reason: change quote tags to code tags to stop reaction to backslash characters
# 7  
Old 08-19-2012
Quote:
`cmp -s <(tr , \n < $COMMON_TMP/nt_per_gs.done | sort) <(tr , \n < $COMMON_TMP/nt_per_chg_indx.done | sort)'
Please post the actual line from your script. Unlear where the mismatched leading backtick and trailing vertical quote in this error message came from.

Have you tried @alister's second version? This avoids very modern Shell syntax.
What Operating System and version are you running and what vintage of bash is this?



Moderator's Comments:
Mod Comment Multiple threads open on this subject.
We shall close https://www.unix.com/shell-programmin...-function.html



Moderator's Comments:
Mod Comment @dsfreddie has posted O/S as:
Linux XXXXXXXX.XXXXXXXXX.com 2.6.18-274.el5 #1 SMP Fri Jul 8 17:36:59 EDT 2011 x86_64 x86_64 x86_64 GNU/Linux

Last edited by methyl; 08-19-2012 at 08:50 PM.. Reason: tags error ; blot company name
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare values in two different files

Hello, I have output in one file that looks like: AA 3 BB 1 CC 3 DD 6 EE 2 FF 6 And output in another file that looks like: 1 EE 3 CC 2 AA I basically want to be able to match the counts in each file against the correct corresponding initials (and then obviosuly base a command on... (7 Replies)
Discussion started by: Nik44
7 Replies

2. Shell Programming and Scripting

Linux Script to compare two folders and copy missing files

Hi, I need help in shell scripting. If someone can help me, that would be great! Problem. I want Linux Script to compare two folders and copy missing files. Description. I have two directories /dir1 /dir2 I need to copy all distinct/new/unique/missing files from /dir1 and that... (1 Reply)
Discussion started by: S.Praveen Kumar
1 Replies

3. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

4. Shell Programming and Scripting

open 2 files and compare values script - urgent

Hi gurus I have two csv files that are outputs. The file contains data similar to s.no,number1,number2,date1 -------------------------------- 1, a123,482.29,11/28/07 13:00 2,a124,602.7,9/24/07 14:00 3,a125,266.93,10/9/07 16.48 4,a126,785.15,11/14/07 16:08 <file 2> s.no name... (2 Replies)
Discussion started by: inkyponky
2 Replies

5. UNIX for Dummies Questions & Answers

How to compare 2 files & get specific value & replace it in other file.

Hiiii Friends I have 2 files with huge data. I want to compare this 2 files & if they hav same set of vales in specific rows & columns i need to get that value from one file & replace it in other. For example: I have few set data of both files here: a.dat: PDE-W 2009 12 16 5 29 11.11 ... (10 Replies)
Discussion started by: reva
10 Replies

6. Shell Programming and Scripting

How to compare 2 files & get only few columns based on a condition related to both files?

Hiiiii friends I have 2 files which contains huge data & few lines of it are as shown below File1: b.dat(which has 21 columns) SSR 1976 8 12 13 10 44.00 39.0700 70.7800 7.0 0 0.00 0 2.78 0.00 0.00 0 0.00 2.78 0 NULL ISC 1976 8 12 22 32 37.39 36.2942 70.7338... (6 Replies)
Discussion started by: reva
6 Replies

7. Shell Programming and Scripting

Need to compare values on two CSV files

:( Hello, Having a problem with reading two files using awk/nawk, am new to both them. I need to compare field values between two csv files and arrange for an appropriate output if both the values are equal or not for each feild. $cat File1.csv... (4 Replies)
Discussion started by: pgop
4 Replies

8. Shell Programming and Scripting

Compare values between files

I have two files with same name residing in different directory. Each file has 14 columns. I want to compare column by column for each row. Also, have to take two columns as key identifier;pick a row in File1; retrieve the corresponding row from file2 and then compare the values. Can... (1 Reply)
Discussion started by: Sangtha
1 Replies

9. Shell Programming and Scripting

Sum values & compare with footer

Hi All, I have a file abc.txt with 3 fileds. Field 3 contains amount. Also at the end of file there is a Footer record, which contains total amount. I need to calculate total sum of these fields & need to compare it with footer record. I have serched in thi site, many has asked this... (7 Replies)
Discussion started by: Amit.Sagpariya
7 Replies

10. UNIX for Dummies Questions & Answers

Aggregate values in a file & compare with sql output

Hi, I have a file containing the following data: junk123junk723itemcode001qty01price10total10junkjunk junk123junk723itemcode002qty02price10total20junkjunk .. .. .. could be 5000+ lines I have an algo and need a code to implement this: 1. Linecount = wc -l (should give 5000) 2. For i... (1 Reply)
Discussion started by: shiroh_1982
1 Replies
Login or Register to Ask a Question