Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to use my awk shortcut as a function, so that I can pass the column number which then prints me the output. My aliases are:

alias A="| awk '{print \$1}'
alias G="| grep -i'

Instad of typing:

ps -ef | grep mysql | awk 'print $2'

I want to be able to type this:

ps -ef G mysql A 2

Any suggestions? Thank you!

share|improve this question
3  
For a completely different approach, try pgrep -f mysql (assuming pgrep is available). –  jw013 Jul 27 '12 at 17:04
    
nice tool thx!! –  DannyRe Jul 27 '12 at 17:13
    
Indeed. And for arbitrary fields, ps -o <field> $(pgrep mysql). –  Mikel Jul 27 '12 at 17:22
    
or ps h -o %p -C mysqld if you want the PIDs of a particular named process (-C is exact match, not search pattern or regexp). You can have multiple -C args, e.g. ps h -o %p -C mysqld -C mysql to get client and server processes. –  cas Jul 27 '12 at 21:06
add comment

1 Answer

I don't think it's possible. Basically, aliases can't take arguments ($1), and functions can't do macro expansion (|).

The closest options I can think of:

in bash or zsh

C() { col=$1; shift; eval "awkcmd='{ print \$$col }'"; echo "$awkcmd"; "$@" | awk "$awkcmd"; }

C 2 ps -ef G mysql

in zsh

alias -g F="| tr -s '[[:space:]]' | cut -d ' ' -f"

ps -ef G mysql F 2
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.