shell - shell 脚本中的多行 awk 脚本
问题描述
#!/usr/bin/tcsh
cmd='BEGIN{c=0}{
if($1=="Net"){print $0}
if($1=="v14")
{
if($4>=200)
{print "Drop more than 200 at "$1}
}
}'
awk -f "$cmd" input_file.txt > output_file.txt
我正在尝试执行其中包含多行 awk 脚本的 shell 脚本。将 awk 脚本(尤其是多行 awk 脚本)存储到变量cmd中,然后在 awk -f "$cmd" input_file.txt > output_file.txt 中执行该 cmd。
这给出了如下错误
awk: fatal: can't open source file `BEGIN{c=0}{
if($1=="Net"){print $0}
if($1=="v14")
{
if($4>=200)
{print"Drop more than 200 at $1}
}
}' for reading (No such file or directory)
我的问题是如何执行其中包含多行 awk 脚本的 shell 脚本?你能帮我解决这个问题吗,因为即使在谷歌/参考手册中搜索后我也无法弄清楚?
解决方案
awk -f
当您想要传递要执行的脚本的文件名时使用。
在这里,您的awk
脚本是一个内联字符串,因此只需删除该-f
选项即可解决您的问题。
awk "$cmd" input_file.txt > output_file.txt
推荐阅读
- c# - 保留来自“for循环”的用户输入的数据
- javascript - 如何在命令中提及用户
- c# - Unity - 将游戏对象写入变量
- java - 为我的 java 类制作一个更改程序,不知道如何获得硬币总数
- html - page-break-after 不适用于 Chrome 中的表 tr
- vmware - 本地打开的共享虚拟机“GNS3 VM.vmx”是不允许的
- swift - PHPickerViewController:重新打开图像选择器时如何取消选择先前选择的图像
- javascript - 在 wordpress 中更改 URL 参数
- java - CodenameOne - BrowserComponent 不可滚动
- vue.js - User.displayName 在脚本中无法访问,但在 VueJs 的模板中可用?