b_o_n_e_z
October 30th, 2013, 07:49 AM
I have a textfile which contains the following
try.txt
john,partTime,3
susan,fullTime,2.6
mary,partTime,2.8
dan,fullTime,2
first I tried to print all the partTime student which is successful
#!/bin/bash
while IFS, read -r -a array; do
[[ "${array[1]}" == "partTime" ]] || continue
printf "%s \n" "${array[0]}" is a ${array[1]} student"
done < "try.txt"
output
john is a partTime student
mary is a partTime student
next I also want to sum up all the numbers of the third column in the text file after printing the types of student and it was not successful
#!/bin/bash
while IFS, read -r -a array; do
[[ "${array[1]}" == "partTime" ]] || continue
printf "%s \n" "${array[0]}" is a ${array[1]} student"
for n in "${array[2]}"; do
total=$( "$total +=$n" | bc )
echo $total
done
done < "try.txt"
output
john is a partTime student
+=3: command not found
mary is a partTime student
+=2.8: command not found
expected output
john is a partTime student
mary is a partTime student
total = 5.8
As I am new to bash scripting , I need to seek help from you guys. thanks in advance
try.txt
john,partTime,3
susan,fullTime,2.6
mary,partTime,2.8
dan,fullTime,2
first I tried to print all the partTime student which is successful
#!/bin/bash
while IFS, read -r -a array; do
[[ "${array[1]}" == "partTime" ]] || continue
printf "%s \n" "${array[0]}" is a ${array[1]} student"
done < "try.txt"
output
john is a partTime student
mary is a partTime student
next I also want to sum up all the numbers of the third column in the text file after printing the types of student and it was not successful
#!/bin/bash
while IFS, read -r -a array; do
[[ "${array[1]}" == "partTime" ]] || continue
printf "%s \n" "${array[0]}" is a ${array[1]} student"
for n in "${array[2]}"; do
total=$( "$total +=$n" | bc )
echo $total
done
done < "try.txt"
output
john is a partTime student
+=3: command not found
mary is a partTime student
+=2.8: command not found
expected output
john is a partTime student
mary is a partTime student
total = 5.8
As I am new to bash scripting , I need to seek help from you guys. thanks in advance