linux - Read multiple arguments per line from file and do arithmetic with shell script -


i have file called input.txt:

a 1 2 b 3 4 

each line of file means a=1*2=2 , b=3*4=12... want output such calculation file output.txt:

a=2 b=12 

and want use shell script calculate.sh finish task:

#!/bin/bash while read name;     $var1=$(echo $name | cut -f1)     $var2=$(echo $name | cut -f2)     $var3=$(echo $name | cut -f3)     echo $var1=(expr $var2 * $var3) done 

and type:

cat input.txt | ./calculate.sh > output.txt 

but approach doesn't work. how task done right?

in bash can do:

while read -r m n; printf "%s=%d\n" $a $((m*n)); done < input.txt > output.txt  cat output.txt a=2 b=12 

Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -