Script error.. for comparing 2 files!


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Script error.. for comparing 2 files!
# 1  
Old 09-13-2006
Script error.. for comparing 2 files!

Hi

I am using the below script to compare two files.. i am getting error as mentioned below:
#!/bin/sh
# Script to find the difference between 2 files
# Remember the old file file1 should always be the first argument. Else, the logic would reverse.
# diff.sh <old file> <new file>
if [[ $# -ne 2 ]] ; then
echo "Need just 2 files to compare"
exit 1
fi;

# Take the diff by ignoring the blank and whitespaces.
diff -b -w ${1} ${2} > ${1}.diff
if [[ $? -eq 0 ]] ; then
echo "No files were added/removed"
else
echo "Files were added/removed"
fi;

# process the diff file.
# A line might look like
# < text-which-went-out
# > text-which-came-in

while read line
do
if [[ ${line:0:2} == "< " ]] ; then
echo ${line:2} is removed.
fi;

if [[ ${line:0:2} == "> " ]] ; then
echo ${line:2} is added.
fi;

done < ${1}.diff

when i run:diff.sh <oldfilename> <newfilename>
/home/cvs ->diff.sh 2047files.txt 2048files.txt
Files were added/removed
diff.sh[23]: ${line:0:2}: The specified substitution is not valid for this command.

can some one correct this...

Note; But my puruse is done, i am able to create a another file where i could see the diff, but i get the error... how do i will over come this???

Any sugesstions...
# 2  
Old 09-13-2006
You never mentioned which OS. Try changing #! /bin/sh to #! /bin/bash
# 3  
Old 09-13-2006
Quote:
Originally Posted by vino
You never mentioned which OS. Try changing #! /bin/sh to #! /bin/bash
we r using solaris...

can some one correct the above
# 4  
Old 09-13-2006
If using Solaris, bash may not be installed. Use ksh. Change the #!/bin/sh to #!/bin/ksh. I don't know if what you are trying works with ksh or not though.
# 5  
Old 09-13-2006
Quote:
Originally Posted by blowtorch
If using Solaris, bash may not be installed. Use ksh. Change the #!/bin/sh to #!/bin/ksh. I don't know if what you are trying works with ksh or not though.
The construct ${line:0:2} will not work with ksh. You will have to rewrite that part of the code. Use typeset. Something like
Code:
typeset -L2 string
string=$line
if [[ "$string" == "< " ]] ; then
echo ${line#$string} is removed.
fi;

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Shell script needed for comparing two files

Hi, I need shell script to compare the two files based on certain fields and output should contains the required fields based on result.pls find sample input files and required output file 1 COUNT, BNG_IP,PORT,OVLAN 22 , 10.238.60.129,1/1,2009 144 , 10.238.60.129,1/1,2251 ... (5 Replies)
Discussion started by: surender reddy
5 Replies

2. Shell Programming and Scripting

shell script for comparing two files

Hi, I have 2 files as below FILE1.dat co1|co2|co3 abnd|45.56|AZ dkny|30.2|PA sam|.23|VA FILE.CTL FILENAME|FILE1.dat NO OF RECORDS|3 CHECKSUM|75.99 Could you please help me to write a shell script to compare 1. TOTAL RECORDS from FILE1.dat with NO of RECORDS in FILE.CTL 2.... (8 Replies)
Discussion started by: dreamsportsteam
8 Replies

3. Shell Programming and Scripting

Comparing 2 files using shell script

Hi Experts, I have 2 files 1 file consists of 800 records and 2 file consists of 100 records with matching column as Membership_Num.So i need a script which will compare the 2 files and displays the output.As these are the files the script should take any delimter like (tab,comma) as input... (6 Replies)
Discussion started by: naveen.dasu
6 Replies

4. Shell Programming and Scripting

Comparing the two files using awk script

Hi all, Can you please help me to find out that where is the problem in my script or either my way of writing the shell command on the prompt is not right? Actually, I want to compare the second column "$1" of the file "t1" with all the columns of second file "t2", if there is a match then the... (2 Replies)
Discussion started by: coder83
2 Replies

5. Shell Programming and Scripting

comparing 2 files in shell script

I have 2 files config1h.txt ----------------- BFMU=ENABLE,ID=PM THR=OFF,REP=ALL,CON=IACM,TIM=OFF;GPON collection strategy 1.3.6.1.2.1.2.2:8 1.3.6.1.2.1.2.2:7 1.3.6.1.4.1.637.61.1.35.11.4:4 1.3.6.1.4.1.637.61.1.35.11.4:3 1.3.6.1.4.1.637.61.1.35.10.1:2 1.3.6.1.4.1.637.61.1.35.10.1:43... (7 Replies)
Discussion started by: LavanyaP
7 Replies

6. Shell Programming and Scripting

Awk script / comparing two files

Goal: To combine the data from two files into one file. File1 = 11 fields, the first field is the unique key File2 = 2 fields, the first field is the unique key What I want to do is match File2:column1 with File1:column1 and if it matches, to add the data from File2:column2 to the matching... (2 Replies)
Discussion started by: jmcgranahan
2 Replies

7. Shell Programming and Scripting

comparing two files using shell script

hi experts please help me to compare two files which are in different directory file1<file will be master file> (/home/rev/mas.txt} ex x1 x2 file2 <will be in different folder> (/home/rev/per/.....) ex x3 x4 the filesinside per folder i need to compare with master file and the files... (2 Replies)
Discussion started by: revenna
2 Replies

8. UNIX for Dummies Questions & Answers

shell script for comparing 2 files

Hi, how to read the 2 files and compare each other in shell script? i have 2 files test1 and test2, both files contains 20 character records.we have to compare file 1 records with file2, if exists then reject the record else we have to append it to test2 file. in file test1 around 100 single... (2 Replies)
Discussion started by: prashanth.spl
2 Replies

9. Shell Programming and Scripting

shell script comparing files in a file

There is a text file that contains the data in the following format: COLUMN1 COLUMN2 ABC 1 ABC 2 ABC 3 DEF 4 DEF 5 XYZ 7 We have to create a second text file... (4 Replies)
Discussion started by: raina_nalin
4 Replies
Login or Register to Ask a Question