UNIT="CCC62"
echo $UNIT |
gawk '{match($0,/(.){4}/,a)}{if (a[1] == 7 || a[1] == 6) print $0}'
如果变量的第4个字符是“ 6”或“ 7”,则上面的结果会产生预期的/期望的结果,然后打印该行,但是有没有办法将变量放入gawk的代码中并获得相同的结果? 并且有一种更聪明的方式仅使用gawk进行以下操作吗?
答案 0 :(得分:1)
能否请您尝试以下操作,也请在代码中解决您的问题。
unit="CCC62"
gawk -v var="$unit" 'BEGIN{split(var,a,"");if (a[4] == 7 || a[4] == 6) print var}'
说明: 添加以上详细说明。
unit="CCC62" ##shell variable
gawk -v var="$unit" ' ##Starting gawk program and sending shell variable unit to awk variable var here -v is ideal way of doing so.
BEGIN{ ##Since there is NO Input_file so work will be done within BEGIN section only.
##Which works without mentioning Input_file name too.
split(var,a,"") ##Splitting var into array a with separator of NULL.
if(a[4] == 7 || a[4] == 6){ ##Checking condition if 4th element of a is either 7 or 6 if yes then do following.
print var ##printing value of var here.
}
}'