在 awk 中访问 shell 变量但未解释
我对 awk 编程非常陌生...
这是我的代码,我试图在 awk 中访问 shell 变量 count
ivlen=`cat record.txt | awk -F " " '{printf "%s",$10}'`
echo $ivlen
count=` expr $ivlen / 2 `
echo $count
echo "\nInitialization Vector : (Value) "
// This one needs attention
编辑:
iv=`awk -v count=$count 'BEGIN {RS=" ";ORS=" ";}
{if (NR > 4 && NR < count+4 )print $0}' esp_payload.txt`
echo $iv
输入:
$cat esp_payload.txt
0000 5FB4 0000 0041
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 5361 6C74 6564 5F5F
D678 E0DA A075 5361 02B4 6273 D970 2F72
输出:(必需)(我想要这些0000
字符串12个
)
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
// this is what I want not what is displayed
输出:(显示在屏幕上)
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 5361
现在出了什么问题?为什么没有打印 0000
以及为什么打印出 5361
I am very new to awk programming ...
Here is my code where I am trying to access shell variable count
in awk
ivlen=`cat record.txt | awk -F " " '{printf "%s",$10}'`
echo $ivlen
count=` expr $ivlen / 2 `
echo $count
echo "\nInitialization Vector : (Value) "
// This one needs attention
Edit :
iv=`awk -v count=$count 'BEGIN {RS=" ";ORS=" ";}
{if (NR > 4 && NR < count+4 )print $0}' esp_payload.txt`
echo $iv
Input:
$cat esp_payload.txt
0000 5FB4 0000 0041
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 5361 6C74 6564 5F5F
D678 E0DA A075 5361 02B4 6273 D970 2F72
Output:(required) (I want those 0000
strings 12 in number
)
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
// this is what I want not what is displayed
output : (displayed on screen)
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 5361
Now what is going wrong ?? why one 0000
not printed and why 5361
printed out
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的脚本将
0041\n0000
计为单个记录,因为其中没有空格字符。您还将0000\n0000
作为输出中的单个字段,但您无法分辨,因为您echo $iv
而不是echo "$iv "
将
RS=" "
更改为RS="[ \n]"
。Your script is counting
0041\n0000
as a single record because it has no space character in it. You're also getting0000\n0000
as a single field in your output, but you can't tell because youecho $iv
instead ofecho "$iv"
Change
RS=" "
toRS="[ \n]"
.您可以使用
-v
将变量传递给awk
,并且您的脚本可以稍微简化,因为{print $0}
是默认操作:You can pass variables to
awk
by using-v
and your script can be simplified a bit, because{print $0}
is the default action:您的脚本用单引号括起来,bash 不会替换单引号字符串中的变量。
将参数传递给 awk 的最简洁方法:
awk 'script.....' count=$count
。Your script is in single quotes, bash doesn't substitue variables in single quoted strings.
The cleanest way to pass parameters to awk:
awk 'script.....' count=$count
.