AWK变量问题 [英] AWK variable issue
本文介绍了AWK变量问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在awk中的变量不返回结果。
我想用awk来得到匹配的价值,从文件的下一行。它工作正常,没有变化。谢谢你。
$猫文件
名称= bobk
SNM = SAHH
EMP = bklc
jdate = 879
$
$ awk的'/名/ {函数getline;打印}'文件
SNM = SAHH --------->得到的结果
$
$出口MYVAR =名称
$
$回声$ MYVAR
名称
$
$ AWK -v AVAR = $ {MYVAR}'/ AVAR / {函数getline;打印}'文件
$ --------->没有结果
解决方案
您需要使用正则表达式匹配运算符〜
与全行 $ 0个
为 / AVAR /
是匹配字符串 AVAR
不是变量 AVAR
:
$ AWK -v AVAR = $ {MYVAR}'$ 0〜AVAR {函数getline;打印}'文件
SNM = SAHH
The variable in awk does not return the result.
I am trying to get the next line of the matched value from file by using awk. It works fine without the variable. Thanks.
$ cat file
name=bobk
snm=sahh
emp=bklc
jdate=879
$
$ awk '/name/{getline; print}' file
snm=sahh ---------> Got the result
$
$ export MYVAR=name
$
$ echo $MYVAR
name
$
$ awk -v AVAR=${MYVAR} '/AVAR/{getline; print}' file
$ ---------> No result
解决方案
You need to use the regexp match operator ~
against the whole line $0
as /AVAR/
is match for the string AVAR
not the variable AVAR
:
$ awk -v AVAR=${MYVAR} '$0~AVAR{getline; print}' file
snm=sahh
这篇关于AWK变量问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文