首页 > 解决方案 > 在别名命令 (bash) 中使用 awk 时遇到错误

问题描述

再会,

我试图使用 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 输出的第 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

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

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

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

问候,迈克

标签: linuxbashawkgrepalias

解决方案


这是实现目标的一种方法

[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,singleawk可以完成你的工作,如下所示 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

推荐阅读