I have the following problem:
I compare two arrays, the first is a file and the second array stores the previous data from the first array. The idea is that when comparing the arrays a repeated data is not inserted to perform the operation only once.
Contents of the file to be read:
11.11.11.1
22.22.22.2
11.11.11.1
33.33.33.3
Script content:
DATOS=( 'cat archivo ' )
CONT5=${#DATOS[@]}
A="1"
for i in "$DATOS"
do
for (( i=0 ; i<$CONT5 ; i=i+1 )); do
for (( r=0; r<$CONT5; r=r+1 )); do
if [ -z "${LOG[r]}" ]; then
echo "Vacío" > /dev/null
else
if [ "$DATOS[i]" == "$LOG[r]" ] ; then
echo "" > /dev/null
echo "${DATOS[i]} ES IGUAL A ${LOG[r]} "
A="0"
else
echo "${DATOS[i]} NO ES ${LOG[r]}"
fi
done
if [ $A -eq 1 ] ; then
echo "Realizar operación"
fi
fi
LOG[$i]=${DATOS[i]}
A="1"
done
done
The problem is that you compare " 11.11.11.1 " of $DATOS
with " 11.11.11.1 " of $LOG
and interpret it as different strings.
Any ideas?
Thanks
EDIT: After adding the line of alo Malbarez the result of the dump is as follows:
Realizar operación
0000000 2 2 . 2 2 . 2 2 . 2 N O E S
062 062 056 062 062 056 062 062 056 062 040 116 117 040 105 123
0000020 1 1 . 1 1 . 1 1 . 1 \n
040 061 061 056 061 061 056 061 061 056 061 012
0000034
Realizar operación
0000000 1 1 . 1 1 . 1 1 . 1 N O E S
061 061 056 061 061 056 061 061 056 061 040 116 117 040 105 123
0000020 1 1 . 1 1 . 1 1 . 1 \n
040 061 061 056 061 061 056 061 061 056 061 012
0000034
0000000 1 1 . 1 1 . 1 1 . 1 N O E S
061 061 056 061 061 056 061 061 056 061 040 116 117 040 105 123
0000020 2 2 . 2 2 . 2 2 . 2 \n
040 062 062 056 062 062 056 062 062 056 062 012
0000034
Realizar operación
0000000 3 3 . 3 3 . 3 3 . 3 N O E S
063 063 056 063 063 056 063 063 056 063 040 116 117 040 105 123
0000020 1 1 . 1 1 . 1 1 . 1 \n
040 061 061 056 061 061 056 061 061 056 061 012
0000034
0000000 3 3 . 3 3 . 3 3 . 3 N O E S
063 063 056 063 063 056 063 063 056 063 040 116 117 040 105 123
0000020 2 2 . 2 2 . 2 2 . 2 \n
040 062 062 056 062 062 056 062 062 056 062 012
0000034
0000000 3 3 . 3 3 . 3 3 . 3 N O E S
063 063 056 063 063 056 063 063 056 063 040 116 117 040 105 123
0000020 1 1 . 1 1 . 1 1 . 1 \n
040 061 061 056 061 061 056 061 061 056 061 012
0000034
Realizar operación
EDIT2: With this patch works although I still do not understand why it fails in the other way.
... Z=${DATOS[i]}
X=${LOG[r]}
if [ "$Z" == "$X" ]; then
#if [ "$DATOS[i]" == "$LOG[r]" ] ; then
...
EDIT3: I have tried to clean it as you indicate in the answer but it does not work.
...
#Z=${DATOS[i]}
#X=${LOG[r]}
#if [ "$Z" == "$X" ]; then
LOGCLEAN=${LOG[r]//[$'\t\r\n']}
echo "LOGCLEAN: $LOGCLEAN"
if [ "$DATOS[i]" == "$LOGCLEAN" ] ; then
...
The result is repeated:
Realizar operación
0000000 2 2 . 2 2 . 2 2 . 2 N O E S
062 062 056 062 062 056 062 062 056 062 040 116 117 040 105 123
0000020 1 1 . 1 1 . 1 1 . 1 \n
040 061 061 056 061 061 056 061 061 056 061 012
0000034
Realizar operación
0000000 1 1 . 1 1 . 1 1 . 1 N O E S
061 061 056 061 061 056 061 061 056 061 040 116 117 040 105 123
0000020 1 1 . 1 1 . 1 1 . 1 \n
040 061 061 056 061 061 056 061 061 056 061 012
0000034
0000000 1 1 . 1 1 . 1 1 . 1 N O E S
061 061 056 061 061 056 061 061 056 061 040 116 117 040 105 123
0000020 2 2 . 2 2 . 2 2 . 2 \n
040 062 062 056 062 062 056 062 062 056 062 012
0000034
Realizar operación
0000000 3 3 . 3 3 . 3 3 . 3 N O E S
063 063 056 063 063 056 063 063 056 063 040 116 117 040 105 123
0000020 1 1 . 1 1 . 1 1 . 1 \n
040 061 061 056 061 061 056 061 061 056 061 012
0000034
0000000 3 3 . 3 3 . 3 3 . 3 N O E S
063 063 056 063 063 056 063 063 056 063 040 116 117 040 105 123
0000020 2 2 . 2 2 . 2 2 . 2 \n
040 062 062 056 062 062 056 062 062 056 062 012
0000034
0000000 3 3 . 3 3 . 3 3 . 3 N O E S
063 063 056 063 063 056 063 063 056 063 040 116 117 040 105 123
0000020 1 1 . 1 1 . 1 1 . 1 \n
040 061 061 056 061 061 056 061 061 056 061 012
0000034
Realizar operación
FINAL EDIT: Here I leave the functional script:
A="1" #El primer dato es aceptado aunque no haya comparación con $LOG
DATOS=( 'cat archivo ' )
CONT5=${#DATOS[@]}
for i in "$DATOS"
do
for (( i=0 ; i<$CONT5 ; i=i+1 )); do
for (( r=0; r<$CONT5; r=r+1 )); do
if [ -z "${LOG[r]}" ]; then
echo "Vacío" > /dev/null
else
if [ "${DATOS[i]}" == "${LOG[r]}" ] ; then
#echo "${DATOS[i]} ES IGUAL A ${LOG[r]} "
A="0"
else
echo "" > /dev/null
#echo "${DATOS[i]} NO ES ${LOG[r]}"
fi
fi
done
if [ $A -eq 1 ] ; then
echo "Realizar operación"
fi
LOG[$i]=${DATOS[i]}
A="1"
done
done
The fact of blocking the operation in the case that a match is found is that when you operate with a file that has many items, and some are repeated, you must filter by coincidence to avoid a bad filtering. You can do the test in this latest fully functional script.
Thank you all for the answers