awk function with a number parameter for the column you want to print
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?
shell alias function
add a comment |
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?
shell alias function
4
For a completely different approach, trypgrep -f mysql
(assumingpgrep
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
1
orps 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 a comment |
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?
shell alias function
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?
shell alias function
shell alias function
edited Aug 23 '16 at 13:14
John Militer
6592928
6592928
asked Jul 27 '12 at 16:52
DannyReDannyRe
1114
1114
4
For a completely different approach, trypgrep -f mysql
(assumingpgrep
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
1
orps 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 a comment |
4
For a completely different approach, trypgrep -f mysql
(assumingpgrep
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
1
orps 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
4
4
For a completely different approach, try
pgrep -f mysql
(assuming pgrep
is available).– jw013
Jul 27 '12 at 17:04
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
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
Indeed. And for arbitrary fields,
ps -o <field> $(pgrep mysql)
.– Mikel
Jul 27 '12 at 17:22
1
1
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
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 a comment |
2 Answers
2
active
oldest
votes
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
add a comment |
Putting pipes into aliases is awkward. Instead, create a shell function that you can pipe things to:
G () {
grep -i "$@"
}
A () {
awk -v col="$1" '{ print $col }'
}
Then,
ps -ef | G mysql | A 2
But this particular pipeline would, on a Linux system, be more or less the same as
pgrep mysql
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f44135%2fawk-function-with-a-number-parameter-for-the-column-you-want-to-print%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
add a comment |
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
add a comment |
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
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
answered Jul 27 '12 at 17:10
MikelMikel
40k10103127
40k10103127
add a comment |
add a comment |
Putting pipes into aliases is awkward. Instead, create a shell function that you can pipe things to:
G () {
grep -i "$@"
}
A () {
awk -v col="$1" '{ print $col }'
}
Then,
ps -ef | G mysql | A 2
But this particular pipeline would, on a Linux system, be more or less the same as
pgrep mysql
add a comment |
Putting pipes into aliases is awkward. Instead, create a shell function that you can pipe things to:
G () {
grep -i "$@"
}
A () {
awk -v col="$1" '{ print $col }'
}
Then,
ps -ef | G mysql | A 2
But this particular pipeline would, on a Linux system, be more or less the same as
pgrep mysql
add a comment |
Putting pipes into aliases is awkward. Instead, create a shell function that you can pipe things to:
G () {
grep -i "$@"
}
A () {
awk -v col="$1" '{ print $col }'
}
Then,
ps -ef | G mysql | A 2
But this particular pipeline would, on a Linux system, be more or less the same as
pgrep mysql
Putting pipes into aliases is awkward. Instead, create a shell function that you can pipe things to:
G () {
grep -i "$@"
}
A () {
awk -v col="$1" '{ print $col }'
}
Then,
ps -ef | G mysql | A 2
But this particular pipeline would, on a Linux system, be more or less the same as
pgrep mysql
answered Feb 28 at 20:57
KusalanandaKusalananda
137k17258426
137k17258426
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f44135%2fawk-function-with-a-number-parameter-for-the-column-you-want-to-print%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
For a completely different approach, try
pgrep -f mysql
(assumingpgrep
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
1
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