AWK不打印换行符 [英] awk without printing newline

查看:1574
本文介绍了AWK不打印换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欲变量sum / NR在每次迭代被印刷侧由端。我们如何避免AWK在每次迭代打印新行?在我的code换行默认情况下,在每次迭代中印

 文件中cg_c ep_c is_c头XHPL
printf的\\ n $的文件>>到plot.xls
    在2.54 1.60 800°F
        AWK'{总和+ = $ 3}; END {打印总和/ NR}'$ {}文件_ $ {F} _v1.xls>>对情节p.xls
    DONE
DONE

我想要的输出出现这样的

  cg_c ANS1 ANS2 ANS3
ep_c ANS1 ANS2 ANS3
is_c ANS1 ANS2 ANS3
牛头ANS1 ANS2 ANS3
XHPL ANS1 ANS2 ANS3

我现在放出来就是这个样子

  ** ** cg_c
ANS1
ANS2
ANS3
** ** ep_c
ANS1
ANS2
ANS3
** ** is_c
ANS1
ANS2
ANS3
** **牛头
ANS1
ANS2
ANS3
** ** XHPL
ANS1
ANS2
ANS3


解决方案

的awk'{总和+ = $ 3}; END {printf的%F,总和/ NR}'$ {}文件_ $ {F} _v1.xls>>对情节p.xls

打印将插入默认情况下一个换行符。你不希望这样的事情发生,因此,使用的printf 代替。

I want the variable sum/NR to be printed side-by-side in each iteration. How do we avoid awk from printing newline in each iteration ? In my code a newline is printed by default in each iteration

for file in cg_c ep_c is_c tau xhpl
printf "\n $file" >> to-plot.xls
    for f in 2.54 1.60 800 
        awk '{sum+=$3}; END  {print  sum/NR}' ${file}_${f}_v1.xls >> to-plot-p.xls
    done
done

I want the output to appear like this

cg_c ans1  ans2  ans3  
ep_c ans1  ans2  ans3 
is_c ans1  ans2  ans3
tau  ans1  ans2  ans3 
xhpl ans1  ans2  ans3

my current out put is like this

**cg_c**
ans1
ans2
ans3
**ep_c**
ans1
ans2
ans3
**is_c**
ans1
ans2
ans3
**tau**
ans1
ans2
ans3
**xhpl**
ans1
ans2
ans3

解决方案

awk '{sum+=$3}; END {printf "%f",sum/NR}' ${file}_${f}_v1.xls >> to-plot-p.xls

print will insert a newline by default. You dont want that to happen, hence use printf instead.

这篇关于AWK不打印换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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