I'm trying to create an alias for a command to see the memory use,
ps -u user -o rss,command | grep -v peruser | awk '{sum+=$1} END {print sum/1024}'
but, the naive,
#.bash_aliases
alias totalmem='ps -u user -o rss,command | grep -v peruser | awk '{sum+=$1} END {print sum/1024}''
gives errors:
-bash: alias: END: not found
-bash: alias: {print: not found
-bash: alias: sum/1024}: not found
I've tried with double quotes,
totalmem ="ps ... |awk '{sum+=$1} END {print sum/1024}'"
, or
totalmem ='ps ... |awk "{sum+=$1} END {print sum/1024}"'
,
escaping,
totalmem ='ps ... |awk \'{sum+=$1} END {print sum/1024}\''
,
or escaping double quotes ... but I can't seem to make it work.
totalmem ='ps ... |awk \"{sum+=$1} END {print sum/1024}\"'
,
gives the error
awk: "{sum+=}
awk: ^ unterminated string
Any tips appreciated.
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}"'