I have a text file with several key words that I am trying to isolate. I have grepped for the unknowns in the text file, but each unknown has a corresponding location. I have created an array that holds all the unknowns and another array that holds all of the locations and compares them based on the line number in the report. the locations are always before the unknowns, so I compare positions in the array to check that the location line number is less than the unknown line number. However, the problem is there are multiple locations and multiple unknowns, so if the unknown is above several locations it prints all of those locations whereas I want the closes one. Please see the code:
typeset -i i=0
typeset -i j=0
grep -h -n "LOCATION" $R_FILE | cut -d ':' -f 1 | while read value
do
arr="$value"
i=$(( $i + 1))
done
grep -h -n "unknown" $R_FILE | cut -d ':' -f 1 | while read value
do
uarr[j]="$value"
j=$(( $j + 1))
done
for j in ${uarr[@]};
do
for i in ${arr[@]};
do
if (($j > $i))
then
LOC=$(awk "NR==$i" $R_FILE)
UNK=$(awk "NR==$j" $R_FILE)
print $LOC","$UNK | awk -F"," '{print $1","$2","$4","$5","$3}' >> output
fi
done
done
Output is as follows:
LOCATION: M1,19,8300,unknown,
LOCATION: M1,13,eeee,unknown,eooeo
LOCATION: B,13,eeee,unknown,eooeo
LOCATION: M1,5,TICKLISH,unknown,83838388383
LOCATION: B,5,TICKLISH,unknown,83838388383
LOCATION: CL 1-2,5,TICKLISH,unknown,83838388383
LOCATION: CL 1-1,5,TICKLISH,unknown,83838388383
LOCATION: CL 2-2,5,TICKLISH,unknown,83838388383
LOCATION: CL 2-1,5,TICKLISH,unknown,83838388383
Output should be:
LOCATION: M1,19,8300,unknown,
LOCATION: B,13,eeee,unknown,eooeo
LOCATION: CL 2-1,5,TICKLISH,unknown,83838388383