Hello, i'm writing a script which should do the following.
1. Open two files and load them into arrays
2. Find same lines in the two files, and then append a line just under the duplicate
Code:
#!/bin/bash
#Declare array
declare -a inputFile
#Open file for reading to inputFile
exec 10<kurac.txt
let count=0
while read LINE <&10; do
inputFile[$count]=$LINE
((count++))
# get number of lines in the inputFile
lajneInput=${#inputFile[@]}
done
#Declare array
declare -a outFile
#Open file for reading to outFile
exec 11<sisa.txt
let count=0
while read LINE <&11; do
outFile[$count]=$LINE
((count++))
# get number of lines in the outFile
lajneOut=${#outFile[@]}
done
echo Broj lajni u ulaznom: ${#inputFile[@]}
echo Broj lajni u izlaznom: ${#outFile[@]}
# Select a line in inputFile and compare it to a line in outputFile
for (( i=0;i<$lajneInput;i++)); do
lajnaIn=${inputFile[${i}]}
y=i+1
lajnaZapis=${inputFile[${y}]}
for (( j=0;j<$lajneOut;j++)); do
lajnaDva=${outFile[${j}]}
# Make sure the line1 and line1 are the same, and they have n as second char...
if [ "${lajnaIn:1:1}" = "n" ] && [ "$lajnaIn" = "$lajnaDva" ]; then
# Append a line to outputFile
printf "%s\n" "${j}a" "$lajnaZapis" "." "w" | ed -s sisa.txt
fi
done
done
The problem seems to be that when it append a line to FILE2, the array which represents FILE2 doesn't "refresh" and shows old data, so when the next itteration comes it messes up :S
How do I make it refresh the array after each itteration?
Thanks for your help!
cheers!