如何在 AWK 中使用 getline 和连接在一起的命令
这让我发疯。我想做的就是从 awk 将命令传递到终端,其中命令是由其他变量连接在一起的字符串。
awk 的文档说类似的东西
"echo" $1 | getline var
应该将 $1 的值放入 var 中。但事实并非如此。我在这里缺少什么?
我应该补充一点,我实际上有一个
for ( i = 1; i <=NF ; i=i+1 )
{
"echo" $i | getline var
printf var " "
}
printf "\n"
输入文件循环,比如
0 2
1 2
输出
0 0
0 0
什么鬼。
This is driving me insane. All I want to do is pass a command to the terminal from awk, where the command is a string concatenated together made from other variables.
The documentation for awk says that something like
"echo" $1 | getline var
should put the value of $1 into var. But this is not the case. What am I missing here?
I should add that I actually have a loop
for ( i = 1; i <=NF ; i=i+1 )
{
"echo" $i | getline var
printf var " "
}
printf "\n"
for inputfile like
0 2
1 2
outputs
0 0
0 0
what the hell.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,事实证明这不是一个错误。
发生的事情是 getline 打开一个新文件,并且根据您的系统设置,每个程序只能打开 X 个文件。一旦打开的文件达到最大数量,getline 就无法打开任何新的 fd。解决方案是你必须调用
当然这是一个微妙的点,并且在文档中应该有关于此的巨大警告标志!不管怎样,我很高兴我解决了它。
Well, it turns out its not a bug.
Whats going on is the getline opens a new file, and depending on your system settings you can only have X files open per program. Once you max out open files, getline can't open any new fd's. The solution is you have to call
Certainly this is a subtle point and there should be huge warning signs in the documentation about this! Anyways, I'm just glad I solved it.
我发现你的样本有两个问题。你的“echo”应该是“echo”(至少,对我来说“echo”不起作用),并且你的 printf 缺少格式arg。
I found two problems with your sample. Your "echo" should be "echo " (at least, for me "echo" didn't work), and your printf is missing the format arg.
如果你想连接值
if you want to concatenate values