繁体   English   中英

在别名命令 (bash) 中使用 awk 时遇到错误

[英]Encountered error using awk in an alias command (bash)

再会,

我尝试使用 awk 创建别名,以根据数量大于设定限制的列进行过滤。 用作命令行时可以,但是当我将其分配为别名时,它会提示错误。

$ grep "SoftBin 108" wmap*CP1
wmap_01_CP1:DISP_OB:    SoftBin 108 is 19 dice exceeded Bin Reject Control limit of 7 dice
wmap_02_CP1:DISP_OB:    SoftBin 108 is 13 dice exceeded Bin Reject Control limit of 7 dice
wmap_03_CP1:DISP_OB:    SoftBin 108 is 11 dice exceeded Bin Reject Control limit of 7 dice
wmap_04_CP1:DISP_OB:    SoftBin 108 is 20 dice exceeded Bin Reject Control limit of 7 dice
wmap_05_CP1:DISP_OB:    SoftBin 108 is 7 dice exceeded Bin Reject Control limit of 7 dice
wmap_06_CP1:DISP_OB:    SoftBin 108 is 14 dice exceeded Bin Reject Control limit of 7 dice
wmap_07_CP1:DISP_OB:    SoftBin 108 is 14 dice exceeded Bin Reject Control limit of 7 dice
wmap_08_CP1:DISP_OB:    SoftBin 108 is 18 dice exceeded Bin Reject Control limit of 7 dice

$ grep "SoftBin 108" wmap*CP1 | awk '$5>15'
wmap_01_CP1:DISP_OB:    SoftBin 108 is 19 dice exceeded Bin Reject Control limit of 7 dice
wmap_04_CP1:DISP_OB:    SoftBin 108 is 20 dice exceeded Bin Reject Control limit of 7 dice
wmap_08_CP1:DISP_OB:    SoftBin 108 is 18 dice exceeded Bin Reject Control limit of 7 dice

$5是 grep output 上的第 5 列,而15是设定的限制。 当我设置别名命令时,它会针对我设置的任何限制抛出只读文件系统。 我尝试使用双 qoute 更改单 qoute,但随后它提示了一个不同的问题。

$ alias SB108='grep "SoftBin 108" wmap*CP1 | awk '$5>15''
-bash: 15: Read-only file system
$ alias SB108="grep "SoftBin 108" wmap*CP1 | awk '$5>15'"
-bash: alias: 108 wmap*CP1 | awk '>15': not found

我在论坛中看到了一些类似的案例,建议使用 function 代替,但我不熟悉。 我尝试这样做,但提示另一个错误。

$ SB108(){grep "SoftBin 108" wmap*CP1 | awk '$5>15';}
-bash: syntax error near unexpected token `('

感谢我对这个问题的帮助。 提前非常感谢。

问候,迈克

这是实现目标的一种方法

[akshay@c1 tmp]$ tail -5 ~/.bashrc

test_awk() {
    grep "SoftBin 108" "$@" | awk '$5>15' 
}
export -f test_awk


# source it or logout and login back
[akshay@c1 tmp]$ source ~/.bashrc

请注意The export -f feature is specific to Bash参考导出手册页

您不必使用grep ,单个awk就可以完成您的工作,如下面awk '/SoftBin 108/ && $5>15' "$@"

试一试以下

[akshay@gold tmp]$ awk '/SoftBin 108/ && $5>15' wmap* 
wmap_01_CP1:DISP_OB:    SoftBin 108 is 19 dice exceeded Bin Reject Control limit of 7 dice
wmap_04_CP1:DISP_OB:    SoftBin 108 is 20 dice exceeded Bin Reject Control limit of 7 dice
wmap_08_CP1:DISP_OB:    SoftBin 108 is 18 dice exceeded Bin Reject Control limit of 7 dice

您用于测试的示例文件

[akshay@c1 tmp]$ cat >wmap_12345_CP1<<EOF
> wmap_01_CP1:DISP_OB:    SoftBin 108 is 19 dice exceeded Bin Reject Control limit of 7 dice
> wmap_02_CP1:DISP_OB:    SoftBin 108 is 13 dice exceeded Bin Reject Control limit of 7 dice
> wmap_03_CP1:DISP_OB:    SoftBin 108 is 11 dice exceeded Bin Reject Control limit of 7 dice
> wmap_04_CP1:DISP_OB:    SoftBin 108 is 20 dice exceeded Bin Reject Control limit of 7 dice
> wmap_05_CP1:DISP_OB:    SoftBin 108 is 7 dice exceeded Bin Reject Control limit of 7 dice
> wmap_06_CP1:DISP_OB:    SoftBin 108 is 14 dice exceeded Bin Reject Control limit of 7 dice
> wmap_07_CP1:DISP_OB:    SoftBin 108 is 14 dice exceeded Bin Reject Control limit of 7 dice
> wmap_08_CP1:DISP_OB:    SoftBin 108 is 18 dice exceeded Bin Reject Control limit of 7 dice
> EOF

称呼

[akshay@c1 tmp]$ test_awk wmap*
wmap_01_CP1:DISP_OB:    SoftBin 108 is 19 dice exceeded Bin Reject Control limit of 7 dice
wmap_04_CP1:DISP_OB:    SoftBin 108 is 20 dice exceeded Bin Reject Control limit of 7 dice
wmap_08_CP1:DISP_OB:    SoftBin 108 is 18 dice exceeded Bin Reject Control limit of 7 dice

[akshay@c1 tmp]$ echo $BASH_VERSION
4.1.2(1)-release

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM