awk 中打印变量
在此脚本中,我希望 awk 打印变量 $file
、$f
、$order
和 sum/NR
>(全部排成一行)
#!/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
你们中有人能帮我解决这个问题吗?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
awk
不会为您获取 shell 变量,您必须将它们作为awk
变量传递:还有另一种方法,即使用双引号而不是单引号(这样
bash
在awk
看到它们之前替换这些值),但随后你必须开始转义$
符号和各种其他awk
命令中的内容:我更喜欢使用显式变量创建。
顺便说一句,这里还有另一个解决方案可以解决您之前的相关问题应该有效。
awk
doesn't go out and get shell variables for you, you have to pass them in asawk
variables:There is another way of doing it by using double quotes instead of single quotes (so that
bash
substitutes the values beforeawk
sees them), but then you have to start escaping$
symbols and all sorts of other things in yourawk
command:I prefer to use explicit variable creation.
By the way, there's another solution to your previous related question here which should work.
您可以这样做:
使用不带“\n”的
echo -n
或printf
将输出不带换行符的文本,因此awk 命令将在同一行中跟随它。我添加了一个空格作为分隔符,但您可以使用任何内容。
使用
tee
将允许您对每个输入(订单)文件仅使用一次awk
调用将输出写入各个文件和摘要文件。You can do this:
Using
echo -n
orprintf
without a "\n" will output the text without a newline so the output of theawk
command will follow it on the same line. I added a space as a separator, but you could use anything.Using
tee
will allow you to write your output to the individual files and the summary file using only oneawk
invocation per input (order) file.