传递变量到AWK从庆典 [英] Passing variables into awk from bash

查看:126
本文介绍了传递变量到AWK从庆典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的,我有打印文件的某一列的shell脚本文件。于是,我试着用awk。列号在脚本计算。 Nprop是一个for循环的变量,即从1变为8。

 平均= 1 + 3 * $ nprop
AWK -v a = $平均'{$打印一个$ A + 1$ A + 2}$ filename5>> neig5.dat

我曾尝试下面还有:

  AWK -v a = $平均'{$打印一个,$(A + 1)$(A + 2)}'$ filename5>> neig5.dat

这导致打印前三列的所有时间。


解决方案

 平均= 1 + 3 * $ nprop

这将设置 $平均 1 + 3 * 4 ,从字面上看,如果 $道具 4的实例。你应该评估的前pression:

 平均= $((1 + 3 * $ nprop))

和使用带括号的 AWK 脚本的版本。

I am writing a shell script file in which I have to print certain columns of a file. So I try to use awk. The column numbers are calculated in the script. Nprop is a variable in a for loop, that changes from 1 to 8.

avg=1+3*$nprop
awk -v a=$avg '{print $a " " $a+1 " " $a+2}' $filename5 >> neig5.dat

I have tried the following also:

awk -v a=$avg '{print $a " " $(a+1) " " $(a+2) }' $filename5 >> neig5.dat

This results in printing the first three columns all the time.

解决方案

avg=1+3*$nprop

This will set $avg to 1+3*4, literally, if $prop is 4 for instance. You should be evaluating that expression:

avg=$(( 1+3*$nprop ))

And use the version of the awk script with parenthesis.

这篇关于传递变量到AWK从庆典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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