if condition doesn't work

i want to get the value for column 4rth when i =4. please guide what i am doing wrong. thanks

var=`cat file.csv`
for i in $var; do {
if [ "$i" -eq 4 ]; then
var4=$var4+$i

fi
echo $i
}
done
I am geting this error message "0403-009 The specified number is not valid for this command."

Not clear what you are trying to do. Please post some sample input from file.csv and the matching sample output from the script.

here is csv file
xerox @DDCM READY 2
xerox xerox READY 3
i need to get column 1,3,4 values. but column 4 is differnt so need to get sum.
i would like to get value of column 4 from first line and then sum up with second line (2+3=5)

> cat file23
xerox @DDCM READY 2 
xerox xerox READY 3 

> awk '{sum=sum+$4} END {print sum}' file23
5

One way would be to use the awk command.

var=`awk -F" " 'BEGIN{total=0;}{total+=$4;}END{print total}' testfile`

Then

echo $var
5

You don't need to initialize 'total'. All variable are strings by default and initialized to an empty string (""). Once you start performing arithmetic, an empty string is casted to 0.

Thanks guys both reply works fine.once again thanks a lot for your help.

Hi guys,
the awk command is working fine.can you please let me know how i can get the value of sum.I need to pass this sum value to java program as parameter.
thanks,
Sagii

Now use $var