我有这样的数据,
field1,field2
test,11
test,3
test,14
test,200
test,8
我希望将所有field2相加。我使用shell_exec。
$execute = 'x=0;for i in `cut -d\',\' -f2 /app/tibs/tosweb/CCS1/tos/temp/test2.txt`; do let x+=i; echo $x; done';
$output = shell_exec($execute);
echo "<pre>".var_export($output, TRUE)."</pre>\\n";
我不明白,为什么x的值总是返回0? 请帮我朋友,谢谢你。
*对不起我的英语不好:P
答案 0 :(得分:0)
cut
不接受多字符分隔符(它认为您的转义由于转义而被分隔为多字符)。所以你可以使用未转义的:
x=0;for i in `cut -d"," -f2 data.txt `; do let x+=i; echo $x; done
或awk
代替
x=0;for i in `awk -F\',\' '{ print $1 }' data.txt `; do let x+=i; echo $x; done
为防止转义,请使用doule qoutes封闭命令:
$execute = "x=0;for i in `cut -d',' -f2 /app/tibs/tosweb/CCS1/tos/temp/test2.txt`; do let x+=i; echo $x; done";