Hi,
I am writting a script which will take all the files present in 2 directories. (the 2 directories have the same files with same name but some different content)
i need 2 compare 2 files and then copy the contents of file1 to file2. the contents which are missing in file1 and nothing from file 1 shud be deleted.
see below:-
File 1 contents :- File 2 Contents:-
DE_DAS
m3=DE_DS|OMER01|3 m3=DE_DS|OMER01|3
m3=DE_DS|OMER02|4 m3=DE_DS|OMER02|4
m3=DE_DS|OMER03|5 m3=DE_DS|OMER03|5
m3=DE_DS|OMER06|7
m3=DE_D1S|OMER02|25
and the result of the file shud be like this :-
File 1 contains after the operation is done:-
DE_DAS
m3=DE_DS|OMER01|3
m3=DE_DS|OMER02|4
m3=DE_DS|OMER03|5
m3=DE_DS|OMER03|5
m3=DE_DS|OMER06|7
m3=DE_D1S|OMER02|25
Can anyone please help me with this. kinda need it urgently...
You can use similar to following :
#!/usr/bin/bash
printf "ENTER THE FULL NAME OF FIRST DIRECTORY : "
read d1
echo
printf "ENTER THE FULL NAME OF SECOND DIRECTORY : "
read d2
echo
ls -ld $d1 $d2 >/dev/null 2>&1 || echo "PLEASE CHECK... FULL PATH NAME OF EITHER DIRECTORY 1 OR DIRECTORY 2 IS NOT PROVIDED..."
ls -ld $d1 $d2 >/dev/null 2>&1 || echo "PLEASE RERUN THE SCRIPT...."
echo
ls -ld $d1 $d2 >/dev/null 2>&1 || exit 2
for i in `find $d1/* -prune -type f|xargs`
do
for j in `find $d2/* -prune -type f|xargs`
do
A=`basename $i`
B=`basename $j`
if [ "$A" == "$B" ] ; then
echo "$d1/out.txt"
echo $d1 $d2 $i $j
(comm $i $j|awk '{print $1,$2,$3}'|uniq) > $d1/out.txt
mv $d1/out.txt $i
fi
done
done
1 Like
Can you post the file contents separately?
In your output there is one m3=DE_DS|OMER03|5 too much, right?
its not working..... errrr
i thought it wud work thats why i said thanks. bt its comparing in a wrong way and i m getting below error....
./do_it.ksh[14]: dead.txt: bad number
mv: d1/out.txt: cannot access: No such file or directory
./do_it.ksh[14]: dead.txt: bad number
mv: d1/out.txt: cannot access: No such file or directory
mv: d1/out.txt: cannot access: No such file or directory
./do_it.ksh[14]: do_it.ksh: bad number
mv: d1/out.txt: cannot access: No such file or directory
./do_it.ksh[14]: alive.txt: bad number
mv: d1/out.txt: cannot access: No such file or directory
---------- Post updated at 06:53 AM ---------- Previous update was at 06:15 AM ----------
In your output there is one m3=DE_DS|OMER03|5 too much, right?
yeh bt all the contents that r uniq in file and file 2
Could you post the exect script which you are using...
Because its working for me...
i have done a lil bit of modification. i am not checking this for different directories. i have pasted both the files in same directory and doing it ... see the below script. (i m pasing my script as u said.. sorry for late response)
#!/usr/bin/ksh
echo "enter the full name of the first file:"
read f1
echo "enter the full name of the second file:"
read f2
for i in `find $f1 -prune -type f| xargs`
do
for j in `find $f2 -prune -type f | xargs `
do
A=`basename $i`
B=`basename $j`
if [ "$A" -eq "$B" ]
then
(comm $i $j|awk '{print $1,$2}'|uniq) > out.txt
#(cmp $i $j | awk '{print $1, $2}' | uniq ) >log.txt
fi
mv out.txt dodo.txt
done
done
i even tried using it with cmp but cudnt go anywhere.. i am getting the below error.
./do_it.ksh[14]: CAM.cfg: bad number
mv: out.txt: cannot access: No such file or directory
i am also pasting both the files i am using ..
file1 - CAM.cfg
[DS] - DOSE
vmsa3=DS101_DS|OMER101|3
vmsa3=DSA101_DS|OMER201|9
vmsa3=DSA102_DS|OMER301|12
vmsa3=DSA102_DS|OMER401|11
[DS2] - DOSE
vmsa4=DSA2_DS|OMER01|2
vmsa4=DSA2_DS|OMER02|3
[DSA300] - DOSE
vmsa4=DSA300_DS|OMER01|3
vmsa4=DSA300_DS|OMER02|4
vmsa4=DSA300_DS|OMER03|5
vmsa4=DSA300_DS|OMER05|7
[VAL0] - VALID
vmsa4=VAL0_DS|OMER01|3
m3vmsa4=VAL00_DS|OMER02|4
m3vmsa4=VAL00_DS|OMER04|6
[INT00] - INT
pps02=INT01_DS|OMER101|3
pps02=INT01_DS|OMER401|9
pps02=INT01_DS|OMER501|11
[VT1] - MAP
vmsa2=VT10_DS|OMER102|4
vmsa2=VT11_DS|OMER202|6
vmsa2=VT12_DS|OMER302|8
vmsa2=VT12_DS|OMER402|9
vmsa2=VT13_DS|OMER501|11
vmsa2=VT14_DS|OMER601|12
attaching my 2nd file ........
the 2nd file is RAM1
vmsa4=VAL0_DS|LOG_DB|1
vmsa4=VAL0_DS|MIN_DB|2
vmsa4=VAL0_DS|OMER01|3
vmsa4=VAL0_DS|OMER02|4
vmsa4=VAL0_DS|OMER03|5
vmsa4=VAL0_DS|OMER04|6
vmsa4=VAL0_DS|OMER90|90
vmsa3=DSA10_DS|LOG|1
vmsa3=DSA10_DS|MIN_DB|2
vmsa3=DSA10_DS|OMER101|3
vmsa3=DSA10_DS|OMER201|9
vmsa3=DSA10_DS|OMER301|12
vmsa3=DSA1_DS|OMER401|11
vmsa3=DSA10_DS|OMER4401|90
vmsa4=DSA2_DS|LOG|1
vmsa4=DSA2_DS|OMER01|2
vmsa4=DSA2_DS|OMER01|3
vmsa4=DSA30_DS|LOG_DB|1
vmsa4=DSA30_DS|MIN_DB|2
vmsa4=DSA30_DS|OMER01|3
vmsa4=DSA30_DS|OMER02|4
vmsa4=DSA30_DS|OMER03|5
vmsa4=DSA30_DS|OMER04|6
vmsa4=DSA30_DS|OMER05|7
vmsa4=DSA30_DS|OMER90|90
pps02=INT0_DS|LOG|1
pps02=INT0_DS|MIN|2
please let me know if there is anything i am missing in execution...
Following are the assumptions for the new script :
1) You are providing the absolute path of both the FILES.
Now modify your script as follow :
#!/usr/bin/ksh
echo "enter the full name of the first file:"
read f1
echo "enter the full name of the second file:"
read f2
comm $f1 $f2|awk '{print $1,$2,$3,$4}' > out.txt
mv out.txt dodo.txt
Please let us know if still problem...
yep i am providing the absolute path and i m still facing the error. i am not sure what to do ..
Are you using following script...???
#!/usr/bin/ksh
echo "enter the full name of the first file:"
read f1
echo "enter the full name of the second file:"
read f2
comm $f1 $f2|awk '{print $1,$2,$3,$4}' > out.txt
mv out.txt dodo.txt