简体   繁体   English

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

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

Good day,再会,

Im trying to create an alias using awk to filter based on a column which has number greater than a set limit.我尝试使用 awk 创建别名,以根据数量大于设定限制的列进行过滤。 Its ok when used as a command line but when I assign it as an alias, it prompts error.用作命令行时可以,但是当我将其分配为别名时,它会提示错误。

$ 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 is column 5 on the grep output while 15 is the set limit. $5是 grep output 上的第 5 列,而15是设定的限制。 When I set the alias command, it threw read only file system for any limit i set.当我设置别名命令时,它会针对我设置的任何限制抛出只读文件系统。 I tried changing single qoute using double qoute but then it prompted a different issue.我尝试使用双 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

Ive seen some similar cases in the forum which suggest using function instead but im not familiar with that.我在论坛中看到了一些类似的案例,建议使用 function 代替,但我不熟悉。 I tried doing it but another error was prompted.我尝试这样做,但提示另一个错误。

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

Appreciate the help on this issue of mine.感谢我对这个问题的帮助。 Thanks a lot in advance.提前非常感谢。

regards, Mike问候,迈克

Here is one way to achieve your goal这是实现目标的一种方法

[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

Please note The export -f feature is specific to Bash Refer Export Man Page请注意The export -f feature is specific to Bash参考导出手册页

You don't have to use grep , single awk can do your job, like below awk '/SoftBin 108/ && $5>15' "$@"您不必使用grep ,单个awk就可以完成您的工作,如下面awk '/SoftBin 108/ && $5>15' "$@"

Test below one试一试以下

[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

your sample file for testing您用于测试的示例文件

[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

call称呼

[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