Hi,
I am currently trying to work out how to compare one line with the last line I have read in via ksh. I have a file which has sorted output from a previous sort command so all the lines are in order already and the file would look something like show below. Each line has a name and a time taken to perform a certain task so I need to filter on the first column as shown below (aaa,bbb,ccc etc) and average all the data up for each matching one and for each no matching one. I am not interested in the addition part as yet as I do have that working in my script using a perl command due to the decimal places.
I.E. So if the current line does not match the previous one, write out it's name and it's data, however if the current line matches the previous one add up data and so on and so on until it does not match then write out the data. I thought it would be really easy but my scripting is just not working and I keep getting an extra line.
The result
B0000003,1.888
B0000024,0.728
B0000024,3.308
B0000024,1.948
B0000027,1.185
B0000030,3.287
B0000030,2.688
This is what I have tried to do, but does not seem to work and the result (regardless of how I place my if's and else's seems to not put the correct counter for each match.
InputFile=input_file.sort
while read line
do
NAME=`echo $line | cut -d"," -f1`
TIME=`echo $line | cut -d"," -f2`
if [[ "$COUNT" -gt "1" ]] then
if [[ $NAME != $PREV_NAME ]] then
echo "$COUNT:$PREV_NAME"
COUNT=1
fi
fi
if [[ $NAME != $PREV_NAME ]] then
COUNT=1
echo "1,$NAME"
elif [[ $NAME = $PREV_NAME ]] then
let COUNT=$COUNT+1
fi
PREV_NAME=$NAME
done <"$InputFile"
and this is the output I get from a valid input file.
Doc Name, Duration (secs)
1,Doc Name
B0000003,0.192494
1,B0000003 < Error
B0000003,1.740529
B0000024,5.409181
2:B0000003 < Correct
1,B0000024 < Error
B0000024,5.409181
1,B0000024
B0000024,5.524508
B0000024,5.569696
B0000024,6.264533
B0000027,0.265375
4:B0000024 < Correct
1,B0000027 < Error
B0000027,0.298291
B0000027,1.157592
etc
As you can see it does work sort of, but it also puts in an extra line that is not needed.
Any help would be appreciated and Thanks!