Function to simplify grep with an often used log
I'm trying make a function that simplifies grepping a log I have to work with on a regular basis.
I'd like to use extended regexp with it pipe and redirect the output, etc.
But I'm having trouble doing this using the standard grep pattern file syntax in the function.
The way I have it set up now is horrible to look at, but gets the job done:
alias mygrep='cat /path/to/logs/my.log | grep'
This allows me to run the following without trouble
mygrep -i -E "WARN|java" |less
Seems like the correct implementation should be something like the following.
function mygrep () {
args=$*
grep "$args" /path/to/logs/my.log
}
However when I use this with the search and pipe parameters above, grep returns an invalid option error.
What am I missing?
grep pipe cat function
add a comment |
I'm trying make a function that simplifies grepping a log I have to work with on a regular basis.
I'd like to use extended regexp with it pipe and redirect the output, etc.
But I'm having trouble doing this using the standard grep pattern file syntax in the function.
The way I have it set up now is horrible to look at, but gets the job done:
alias mygrep='cat /path/to/logs/my.log | grep'
This allows me to run the following without trouble
mygrep -i -E "WARN|java" |less
Seems like the correct implementation should be something like the following.
function mygrep () {
args=$*
grep "$args" /path/to/logs/my.log
}
However when I use this with the search and pipe parameters above, grep returns an invalid option error.
What am I missing?
grep pipe cat function
6
Try dropping the$args
and just sayinggrep "$@" /path/to/logs/my.log
.
– Tom Hunt
Oct 20 '15 at 17:06
5
You might also be interested in What is the difference between $* and $@?
– terdon♦
Oct 20 '15 at 17:33
1
Not a noobish question at all. This trips up even vets, and at least you did your homework.
– coteyr
Oct 20 '15 at 19:09
add a comment |
I'm trying make a function that simplifies grepping a log I have to work with on a regular basis.
I'd like to use extended regexp with it pipe and redirect the output, etc.
But I'm having trouble doing this using the standard grep pattern file syntax in the function.
The way I have it set up now is horrible to look at, but gets the job done:
alias mygrep='cat /path/to/logs/my.log | grep'
This allows me to run the following without trouble
mygrep -i -E "WARN|java" |less
Seems like the correct implementation should be something like the following.
function mygrep () {
args=$*
grep "$args" /path/to/logs/my.log
}
However when I use this with the search and pipe parameters above, grep returns an invalid option error.
What am I missing?
grep pipe cat function
I'm trying make a function that simplifies grepping a log I have to work with on a regular basis.
I'd like to use extended regexp with it pipe and redirect the output, etc.
But I'm having trouble doing this using the standard grep pattern file syntax in the function.
The way I have it set up now is horrible to look at, but gets the job done:
alias mygrep='cat /path/to/logs/my.log | grep'
This allows me to run the following without trouble
mygrep -i -E "WARN|java" |less
Seems like the correct implementation should be something like the following.
function mygrep () {
args=$*
grep "$args" /path/to/logs/my.log
}
However when I use this with the search and pipe parameters above, grep returns an invalid option error.
What am I missing?
grep pipe cat function
grep pipe cat function
edited Dec 16 at 4:27
Rui F Ribeiro
38.9k1479129
38.9k1479129
asked Oct 20 '15 at 16:55
Kirk Anderson
335
335
6
Try dropping the$args
and just sayinggrep "$@" /path/to/logs/my.log
.
– Tom Hunt
Oct 20 '15 at 17:06
5
You might also be interested in What is the difference between $* and $@?
– terdon♦
Oct 20 '15 at 17:33
1
Not a noobish question at all. This trips up even vets, and at least you did your homework.
– coteyr
Oct 20 '15 at 19:09
add a comment |
6
Try dropping the$args
and just sayinggrep "$@" /path/to/logs/my.log
.
– Tom Hunt
Oct 20 '15 at 17:06
5
You might also be interested in What is the difference between $* and $@?
– terdon♦
Oct 20 '15 at 17:33
1
Not a noobish question at all. This trips up even vets, and at least you did your homework.
– coteyr
Oct 20 '15 at 19:09
6
6
Try dropping the
$args
and just saying grep "$@" /path/to/logs/my.log
.– Tom Hunt
Oct 20 '15 at 17:06
Try dropping the
$args
and just saying grep "$@" /path/to/logs/my.log
.– Tom Hunt
Oct 20 '15 at 17:06
5
5
You might also be interested in What is the difference between $* and $@?
– terdon♦
Oct 20 '15 at 17:33
You might also be interested in What is the difference between $* and $@?
– terdon♦
Oct 20 '15 at 17:33
1
1
Not a noobish question at all. This trips up even vets, and at least you did your homework.
– coteyr
Oct 20 '15 at 19:09
Not a noobish question at all. This trips up even vets, and at least you did your homework.
– coteyr
Oct 20 '15 at 19:09
add a comment |
1 Answer
1
active
oldest
votes
The following code has $args
quoted:
grep "$args" /path/to/logs/my.log
This asks to pass the entire value of that variable as a single
parameter to grep. Thus, if you call mygrep
with parameters -i
and
-E
, grep
will in fact receive a single parameter -i -E
, which is
indeed invalid.
The following should do it:
function mygrep () {
grep "$@" /path/to/logs/my.log
}
Writing "$@"
does the right thing: it's similar to $*
, except that
it properly expands each parameter to a single word.
This works perfectly, thanks! Also thanks to everyone who responded so cordially and helpfully.
– Kirk Anderson
Oct 22 '15 at 21:43
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%2f237448%2ffunction-to-simplify-grep-with-an-often-used-log%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The following code has $args
quoted:
grep "$args" /path/to/logs/my.log
This asks to pass the entire value of that variable as a single
parameter to grep. Thus, if you call mygrep
with parameters -i
and
-E
, grep
will in fact receive a single parameter -i -E
, which is
indeed invalid.
The following should do it:
function mygrep () {
grep "$@" /path/to/logs/my.log
}
Writing "$@"
does the right thing: it's similar to $*
, except that
it properly expands each parameter to a single word.
This works perfectly, thanks! Also thanks to everyone who responded so cordially and helpfully.
– Kirk Anderson
Oct 22 '15 at 21:43
add a comment |
The following code has $args
quoted:
grep "$args" /path/to/logs/my.log
This asks to pass the entire value of that variable as a single
parameter to grep. Thus, if you call mygrep
with parameters -i
and
-E
, grep
will in fact receive a single parameter -i -E
, which is
indeed invalid.
The following should do it:
function mygrep () {
grep "$@" /path/to/logs/my.log
}
Writing "$@"
does the right thing: it's similar to $*
, except that
it properly expands each parameter to a single word.
This works perfectly, thanks! Also thanks to everyone who responded so cordially and helpfully.
– Kirk Anderson
Oct 22 '15 at 21:43
add a comment |
The following code has $args
quoted:
grep "$args" /path/to/logs/my.log
This asks to pass the entire value of that variable as a single
parameter to grep. Thus, if you call mygrep
with parameters -i
and
-E
, grep
will in fact receive a single parameter -i -E
, which is
indeed invalid.
The following should do it:
function mygrep () {
grep "$@" /path/to/logs/my.log
}
Writing "$@"
does the right thing: it's similar to $*
, except that
it properly expands each parameter to a single word.
The following code has $args
quoted:
grep "$args" /path/to/logs/my.log
This asks to pass the entire value of that variable as a single
parameter to grep. Thus, if you call mygrep
with parameters -i
and
-E
, grep
will in fact receive a single parameter -i -E
, which is
indeed invalid.
The following should do it:
function mygrep () {
grep "$@" /path/to/logs/my.log
}
Writing "$@"
does the right thing: it's similar to $*
, except that
it properly expands each parameter to a single word.
answered Oct 20 '15 at 17:08
dhag
11.2k33045
11.2k33045
This works perfectly, thanks! Also thanks to everyone who responded so cordially and helpfully.
– Kirk Anderson
Oct 22 '15 at 21:43
add a comment |
This works perfectly, thanks! Also thanks to everyone who responded so cordially and helpfully.
– Kirk Anderson
Oct 22 '15 at 21:43
This works perfectly, thanks! Also thanks to everyone who responded so cordially and helpfully.
– Kirk Anderson
Oct 22 '15 at 21:43
This works perfectly, thanks! Also thanks to everyone who responded so cordially and helpfully.
– Kirk Anderson
Oct 22 '15 at 21:43
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f237448%2ffunction-to-simplify-grep-with-an-often-used-log%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
6
Try dropping the
$args
and just sayinggrep "$@" /path/to/logs/my.log
.– Tom Hunt
Oct 20 '15 at 17:06
5
You might also be interested in What is the difference between $* and $@?
– terdon♦
Oct 20 '15 at 17:33
1
Not a noobish question at all. This trips up even vets, and at least you did your homework.
– coteyr
Oct 20 '15 at 19:09