在别名命令中使用 awk 时遇到错误 (bash)
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 '>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
</code> 是 grep 输出的第 5 列,而 <code>15
是设置的限制。当我设置别名命令时,它抛出
我设置的任何限制的只读文件系统。我尝试使用双引号更改单引号,但随后出现了不同的问题。
$ alias SB108='grep "SoftBin 108" wmap*CP1 | awk '>15''
-bash: 15: Read-only file system
$ alias SB108="grep "SoftBin 108" wmap*CP1 | awk '>15'"
-bash: alias: 108 wmap*CP1 | awk '>15': not found
我在论坛上看到过一些类似的案例,建议使用函数代替,但我不熟悉
接着就,随即。我尝试这样做,但提示另一个错误。
$ SB108(){grep "SoftBin 108" wmap*CP1 | awk '>15';}
-bash: syntax error near unexpected token `('
感谢您对我的这个问题的帮助。提前致谢。
问候,迈克
这是实现目标的一种方法
[akshay@c1 tmp]$ tail -5 ~/.bashrc
test_awk() {
grep "SoftBin 108" "$@" | awk '>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
Refer Export Man Page
不一定要用grep
,单awk
就可以了,如下图
awk '/SoftBin 108/ && >15' "$@"
下面测试一个
[akshay@gold tmp]$ awk '/SoftBin 108/ && >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
美好的一天,
我正在尝试使用 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 '>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
</code> 是 grep 输出的第 5 列,而 <code>15
是设置的限制。当我设置别名命令时,它抛出
我设置的任何限制的只读文件系统。我尝试使用双引号更改单引号,但随后出现了不同的问题。
$ alias SB108='grep "SoftBin 108" wmap*CP1 | awk '>15''
-bash: 15: Read-only file system
$ alias SB108="grep "SoftBin 108" wmap*CP1 | awk '>15'"
-bash: alias: 108 wmap*CP1 | awk '>15': not found
我在论坛上看到过一些类似的案例,建议使用函数代替,但我不熟悉 接着就,随即。我尝试这样做,但提示另一个错误。
$ SB108(){grep "SoftBin 108" wmap*CP1 | awk '>15';}
-bash: syntax error near unexpected token `('
感谢您对我的这个问题的帮助。提前致谢。
问候,迈克
这是实现目标的一种方法
[akshay@c1 tmp]$ tail -5 ~/.bashrc
test_awk() {
grep "SoftBin 108" "$@" | awk '>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
Refer Export Man Page
不一定要用grep
,单awk
就可以了,如下图
awk '/SoftBin 108/ && >15' "$@"
下面测试一个
[akshay@gold tmp]$ awk '/SoftBin 108/ && >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