Skip to content
Advertisement

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. 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. 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.

$ 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. 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

Advertisement

Answer

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

You don’t have to use grep, single awk can do your job, like below 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
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement