awk命令接受两个变量作为参数和返回值 [英] awk command to accept two variables as parameters and return a value

查看:158
本文介绍了awk命令接受两个变量作为参数和返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有50行的文件。每行是由三列组成。前两列是变量,这将作为参数,返回第3列的值被传递。
外汇..
command_file.txt是文件,它包含

I have a file which has 50 rows. Each row is made up of three columns. The first two columns are the variables and this will be passed as parameters to return the 3rd column's value. for ex.. command_file.txt is the file and it contains


A B 10
C D 20
E F 30
G H 50
I J 70
...

我有以下命令的脚本。

I have a script with the following command.


#!/user/bin/sh
READ_FILE=/export/home/user/command_file.txt
VA1=A
VA2=B
GET_VALUE=`awk '/ -v var="$VA1" '$1 ~ var' -v var1="$VA2" '$1 ~ var1''/ $READ_FILE l awk '{print $3}'`
echo $GET_VALUE

当我把这个脚本传递A和B作为参数我期望returned.But值10它返回的错误。
但是,如果我硬code值在下面的命令,它会工作。

When I call this script passing A and B as parameters I expect a value of 10 to be returned.But it returned errors. But if I hard code the value on the below command it will work.

GET_VALUE=`awk '/A B'/ $READ_FILE lawk '{print $3}'`

有什么建议?
谢谢你。

Any suggestions? Thanks.

推荐答案

您必须使用 awk的 awk的脚本之前变量传递开始避免毛茸茸的报价,再加上修复等问题:

You have to use awk's variable passing before the awk script begins to avoid hairy quoting, plus fix other problems:

#!/user/bin/sh
READ_FILE=/export/home/user/command_file.txt
VA1=A
VA2=B
GET_VALUE=$(awk -v var="$VA1" -v var1="$VA2" '$1 ~ var &&  $2 ~ var1 {print $3}' $READ_FILE)
echo $GET_VALUE

这篇关于awk命令接受两个变量作为参数和返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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