Hi ,
I am executing a while loop .The only condition is , as far as the cumulative sum of the 2nd column from each line passed from the txt file reaches value of 12 , echo those lines .But once the cumulative sum reaches 12 , restart the loop setting the cumulative sum variable to 0 . But in the below snapshot , the cumulative sum is getting set to 0 only at the line 5 instead of 4 . The line 4 is skipped due to this reason during the loop iteration. This is also true at line 7 which also getting skipped as the cumulative sum variable is getting reset to 0 at line 8 in the subsequent iteration. Please help with any suggestions
source file : file_size_num.txt
1,3,/informatica/apps/logs/fin/ote/TgtFiles/Destination/CWXX/CW1900
2,4,/informatica/apps/logs/fin/ote/TgtFiles/Destination/CWXX/CW1901
3,5,/informatica/apps/logs/fin/ote/TgtFiles/Destination/CWXX/CW1901
4,1,/informatica/apps/logs/fin/ote/TgtFiles/Destination/CWXX/CW1901
5,2,/informatica/apps/logs/fin/ote/TgtFiles/Destination/CWXX/CW1900
6,6,/informatica/apps/logs/fin/ote/TgtFiles/Destination/CWXX/CW1901
7,7,/informatica/apps/logs/fin/ote/TgtFiles/Destination/CWXX/CW1901
8,8,/informatica/apps/logs/fin/ote/TgtFiles/Destination/CWXX/CW1901
#!/bin/sh
sum=0
while read line
do
var1="$(echo "$line" | awk -F "," '{print $2}')"
sum=$(expr "$sum" + "$var1")
echo $sum
if [ $sum -le 12 ]
then
echo $line
else
sum=0
line=$line
fi
done < file_size_num.txt
output :
in the below snapshot , the cumulative sum is getting set to 0 only at the line 5 instead of 4 . The line 4 is skipped due to this reason during the loop iteration. This is also true at line 7 which also getting skipped as the cumulative sum variable is getting reset to 0 at line 8 in the subsequent iteration.
-bash-4.2$ sh bash.sh
3
1,3,/informatica/apps/logs/fin/ote/TgtFiles/Destination/CWXX/CW1900
7
2,4,/informatica/apps/logs/fin/ote/TgtFiles/Destination/CWXX/CW1901
12
3,5,/informatica/apps/logs/fin/ote/TgtFiles/Destination/CWXX/CW1901
13
2
5,2,/informatica/apps/logs/fin/ote/TgtFiles/Destination/CWXX/CW1900
8
6,6,/informatica/apps/logs/fin/ote/TgtFiles/Destination/CWXX/CW1901
15
8
8,8,/informatica/apps/logs/fin/ote/TgtFiles/Destination/CWXX/CW1901
-bash-4.2$