在awk中打​​印变量 [英] printing variable inside awk

查看:74
本文介绍了在awk中打​​印变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此脚本中,我希望awk打印变量$file$f$ordersum/NR(均在单行中)

In this script , I want awk to print the variables $file, $f, $order and sum/NR (all in a single row)

#!/bin/bash
for file in pmb_mpi tau xhpl mpi_tile_io fftw ; do
    for f in 2.54 1.60 800 ;do
    if [ ${f} = 2.54 ]
    then 
        for order in even odd ; do
#       echo ${file}_${f}_${order}_v1.xls >> P-state-summary.xls
        awk '{sum+=$2} END {print ${file}_${f}_${order}_v1.xls, sum/NR}' ${file}_${f}_${order}_v1.xls >> P-state-summary.xls
        done
    else
#       echo ${file}_${f}_v1.xls >>  P-state-summary.xls
        awk '{sum+=$2} END {print  ${file}_${f}_v1.xls , sum/NR}' ${file}_${f}_v1.xls >>  P-state-summary.xls
    fi
    done
done

请问你们当中有人帮助我吗?

Colud anyone of you kindly help me with this ?

推荐答案

您可以执行以下操作:

echo -n "${file}_${f}_${order}_v1.xls " >> P-state-summary.xls
# or printf "${file}_${f}_${order}_v1.xls " >> P-state-summary.xls
awk '{sum+=$2} END {print sum/NR}' "${file}_${f}_${order}_v1.xls" | 
    tee "${file}_${f}_avrg.xls" >> P-state-summary.xls

使用不带\ n的echo -nprintf将输出不带换行符的文本,因此awk命令的输出将在同一行之后.我添加了一个空格作为分隔符,但您可以使用任何东西.

Using echo -n or printf without a "\n" will output the text without a newline so the output of the awk command will follow it on the same line. I added a space as a separator, but you could use anything.

使用tee将允许您使用每个输入(订单)文件一个调用awk的方式将输出写入单个文件和摘要文件.

Using tee will allow you to write your output to the individual files and the summary file using only one awk invocation per input (order) file.

这篇关于在awk中打​​印变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆