Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何计算shell中不同列之间的值_Linux_Bash_Shell - Fatal编程技术网

Linux 如何计算shell中不同列之间的值

Linux 如何计算shell中不同列之间的值,linux,bash,shell,Linux,Bash,Shell,在我的项目中,我有一个文件 此文件的某些内容如下所示: user=bio-zhaoy group=bio-zhaijx queue=gpu start=1551321260 total_execution_slots=12 end=1551370299 user=mse-tangchm group=mse-chengch queue=cal-l start=1551351386 total_execution_slots=96 end=1551372665 user=chem-quanm gro

在我的项目中,我有一个文件

此文件的某些内容如下所示:

user=bio-zhaoy group=bio-zhaijx queue=gpu start=1551321260 total_execution_slots=12 end=1551370299
user=mse-tangchm group=mse-chengch queue=cal-l start=1551351386 total_execution_slots=96 end=1551372665
user=chem-quanm group=chem-jiangw queue=fat start=1551318056 total_execution_slots=18 end=1551374993
user=cs-likm group=cs-hisao queue=cal-l start=1551341318 total_execution_slots=12 end=1551376362
user=cs-likm group=cs-hisao queue=cal-l start=1551340870 total_execution_slots=12 end=1551379856
user=phy-shizs1 group=phy-shizs1 queue=cal-s start=1551372666 total_execution_slots=96 end=1551380535
user=ose-guoz group=ose-guoz queue=cal-s start=1551380539 total_execution_slots=96 end=1551380546
user=mse-tangchm group=mse-chengch queue=cal-l start=1551351334 total_execution_slots=96 end=1551381367
user=bio-zhaoy group=bio-zhaijx queue=gpu start=1551357949 total_execution_slots=24 end=1551384269
user=mee-xud group=mee-hucz queue=cal-l start=1551184507 total_execution_slots=48 end=1551386704
user=mse-tangchm group=mse-chengch queue=cal-l start=1551351362 total_execution_slots=96 end=1551388501
user=maad-linys group=maad-wanmp queue=cal-l start=1551129996 total_execution_slots=36 end=1551389224
user=maad-linys group=maad-wanmp queue=cal-l start=1551129996 total_execution_slots=36 end=1551389224
user=maad-zhoukc group=maad-wanmp queue=fat start=1551336869 total_execution_slots=144 end=1551391828
user=maad-liuzj group=maad-chensy queue=ser start=1551323808 total_execution_slots=1 end=1551392934
user=maad-liuzj group=maad-chensy queue=ser start=1551323868 total_execution_slots=1 end=1551392989
user=maad-linys group=maad-wanmp queue=cal-l start=1551134144 tot al_execution_slots=36 end=1551393349
user=maad-linys group=maad-wanmp queue=cal-l start=1551134144 total_execution_slots=36 end=1551393354
user=maad-linys group=maad-wanmp queue=cal-l start=1551134144 total_execution_slots=36 end=1551393376
user=phy-shaoxj group=phy-wangkd queue=cal-l start=1551134144 total_execution_slots=120 end=1551393380
user=maad-linys group=maad-wanmp queue=cal-l start=1551134144 total_execution_slots=36 end=1551393385
user=chem-wangyg group=chem-wangyg queue=cal-l start=1551324604 total_execution_slots=48 end=1551394118
user=mee-xud group=mee-z queue=cal-l start=1551254511 total_execution_slots=48 end=1551397837
user=mse-xiaow group=mse-g queue=fat start=1551358601 total_execution_slots=90 end=1551398698
user=phy-liugw group=phyd queue=cal-l start=1551352409 total_execution_slots=360 end=1551400256
user=mee-xud group=mee-huz queue=cal-l start=1551310151 total_execution_slots=48 end=1551400565
user=mse-xiaow group=mse-l queue=fat start=1551400769 total_execution_slots=90 end=1551401158
现在,我想计算每一行的时间值。 计算方法为:

(end-start)*total_execution_slots
例如,第一行:

(1551370299-1551321260)*12
我需要在bashshell中求解。我想把计算出的值作为 最后一栏

我试过:

for i in $(cat file)
do
 starttime=`awk '{print $4}' $i | awk -F '=' '{print $2}'`
 endtime=`awk '{print $6}' $i | awk -F '=' '{print $2}'`
 slots=`awk '{print $5}' $i | awk -F '=' '{print $2}'`
 totaltime=($endtime-$starttime)*$slots
 echo $i" "$totaltime >> resultinfo
done
但是当我运行这个脚本时,它是挂起的


谁能帮助我?

您可以使用以下代码:

while read -r val_user val_group val_queue val_start val_total_execution_slots val_end; do 
{
  num_val_start=$(echo $val_start | cut -d'=' -f 2)
  num_val_end=$(echo $val_end | cut -d'=' -f 2)
  num_val_total_execution_slots=$(echo $val_total_execution_slots | cut -d'=' -f 2)
  awaited_res=$(( ( $num_val_end - $num_val_start ) * $num_val_total_execution_slots ))
  echo $awaited_res
}
done < yourfile
读取时-r val\u用户val\u组val\u队列val\u开始val\u总执行val\u插槽val\u结束;做
{
num_val_start=$(echo$val_start | cut-d'='-f2)
num_val_end=$(echo$val_end | cut-d'='-f2)
num_val_total_execution_slots=$(echo$val_total_execution_slots | cut-d'='-f2)
等待的\u res=$(($num\u val\u end-$num\u val\u start)*$num\u val\u总执行时间)
echo$waiting_res
}
完成你的文件

您应该用文件名替换文件,它将提供操作的结果。您可以缩短脚本,但我更喜欢使用变量来保持清晰。第一部分读取多个变量中的文件内容,这些变量只取“=”字符后的值进行处理。然后,$((允许获取expr命令的结果以处理您想要的公式。

您尝试了什么?
awk-F'[=]''{print($12-$8)*$10}'file
@oguzismail,太好了,非常感谢much@stack:请注意,如果字段顺序更改或插入其他字段,oguzismail的解决方案将中断。在更稳定的解决方案中,您将显式查找名称(
start
)。这对于维护来说也比依赖字段号更好。