Unix 'Alias' Fails with 'Awk' Command

Encountered error using awk in an alias command (bash)

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

Alias for awk command

Your problem may just be that your backslash finger is too heavy. Double quotes inside the single quoted string do not need to be escaped unless they're part of a double-quoted string that is inside the single quotes.

But it may also be that you're confusing the bash alias command with the tcsh alias command. Bash notation looks like:

alias thing=command

whereas tcsh looks like:

alias thing command

Note the subtle difference. :-)

The following generates no errors for me:

% alias crd 'awk '\''{$1="";$2="";$3="";$4="";$5="";print}'\'''

The alias even seems to be functional:

% seq -s\  1 8 | crd
6 7 8

You might also find this answer useful, if your goal is actually to remove columns rather than just nullify content.

AWK alias not printing

Don't use aliases. They require an additional layer of quoting, which is troublesome (as here), and they prevent you from being able to usefully parameterize or add conditional logic to your code.

A simple transliteration to a function is:

getperc() { awk '/WORD/ {print $3}' log.log | awk 'BEGIN{c=0} length($0){a[c]=$0;c++}END{p5=(c/100*5); p5=p5%1?int(p5)+1:p5; print a[c-p5-1]}'; }

A slightly more capable one, which will still use log.log by default, but which will also let you provide an alternate input file name (as in getperc alternate.log) or pipe to your function (as in cat alternate.log | getperc):

getperc() {
[[ -t 0 || $1 ]] || set -- - # use "-" (stdin) as input file if not a TTY
# ...this will let you pipe to your function.
awk '/WORD/ {print $3}' "${1:-log.log}" | awk 'BEGIN{c=0} length($0){a[c]=$0;c++}END{p5=(c/100*5); p5=p5%1?int(p5)+1:p5; print a[c-p5-1]}'
}

bash aliases and awk escaping of quotes

You almost have it, the $ will be expanded in double-quotes, so that needs extra escaping:

alias totalmem='ps -u user -o rss,command | grep -v peruser | awk "{sum+=\$1} END {print sum/1024}"'

Or with the pattern inside awk as suggested by iiSeymour:

alias totalmem='ps -u user -o rss,command | awk "!/peruser/ {sum+=\$1} END {print sum/1024}"'

Alias of awk command - too many quotes

try this way of function declaration:

tailMyFile() {
tail -f /path/to/file | awk '
/INFO/ {print "\033[32m" $0 "\033[39m"}
/ERROR/ {print "\033[31m" $0 "\033[39m"}
/WARNING/ {print "\033[33m" $0 "\033[39m"}';
}

However, it seems as though your error is not pertaining to how the functions is defined, but whether your /INFO/ directory is properly seen, can you cd /INFO without any problems?

Unix alias command not working as expected

The command line whereby you're setting up the alias is not quoted correctly. Specifically, the back-quote embedded subcommand is being executed at the time you set up the alias, and not later when you actually want to run the alias.

Try setting it up this way:

alias killaf='kill -9 `psu|grep MF1pp|grep -v grep|awk '\''{print $2}'\''`'

edit: I fixed the quotes around the awk command - it's tricky to embed single-quotes when you're already single-quoting.



Related Topics



Leave a reply



Submit