grep with heredoc in function
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I want to make function that parses from text pasted in a terminal.
$ cat <<eof | grep --color sometext
> foo
> sometext
> sometext1
> a sometext
> asdf
>
> eof
sometext
sometext1
a sometext
While the above works I can make neither an alias nor a function of it.
alias gsi='cat <<eof | grep --color "$1"'
gsi cat <<eof
I thought redirections weren't executed during function definition.
bash here-document bash-functions
add a comment |Â
up vote
2
down vote
favorite
I want to make function that parses from text pasted in a terminal.
$ cat <<eof | grep --color sometext
> foo
> sometext
> sometext1
> a sometext
> asdf
>
> eof
sometext
sometext1
a sometext
While the above works I can make neither an alias nor a function of it.
alias gsi='cat <<eof | grep --color "$1"'
gsi cat <<eof
I thought redirections weren't executed during function definition.
bash here-document bash-functions
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I want to make function that parses from text pasted in a terminal.
$ cat <<eof | grep --color sometext
> foo
> sometext
> sometext1
> a sometext
> asdf
>
> eof
sometext
sometext1
a sometext
While the above works I can make neither an alias nor a function of it.
alias gsi='cat <<eof | grep --color "$1"'
gsi cat <<eof
I thought redirections weren't executed during function definition.
bash here-document bash-functions
I want to make function that parses from text pasted in a terminal.
$ cat <<eof | grep --color sometext
> foo
> sometext
> sometext1
> a sometext
> asdf
>
> eof
sometext
sometext1
a sometext
While the above works I can make neither an alias nor a function of it.
alias gsi='cat <<eof | grep --color "$1"'
gsi cat <<eof
I thought redirections weren't executed during function definition.
bash here-document bash-functions
bash here-document bash-functions
asked Aug 15 at 14:15
1.61803
446615
446615
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
5
down vote
alias 'gsi=<<eof grep --color'
Would work as alias is just like preprocessor text replacement, where the replacement is interpreted as shell code again.
Yours was not working as you had that "$1"
. With gsi file.txt
replaced with cat <<eof | grep --color "$1" file.txt
, then the shell carries on interpreting that command line and $1
at that point is the shell's first positional parameter which is probably empty unless you did a set something
beforehand. So you'd want to remove that "$1"
here. You can remove the UUOC as well.
gsi() cat <<eof
is wrong from a syntax point of view as the eof
line is missing after that cat <<eof
.
Thanks, for emending the alias. I still don't understand whyhere-document
is executed during function definition.
â 1.61803
Aug 15 at 16:14
@1.61803 it's not that the heredoc is executed, it's that the function body has to be syntactically complete so that bash can understand how much of what you have given is the function, unlike aliases which are essentially simple text replacements.
â muru
Aug 16 at 0:31
add a comment |Â
up vote
4
down vote
Your function would not be using a here-document:
gsi ()
grep --color "$@"
This would enable you to pass a pattern as well as any other options to grep
on the common line and grep
would read from standard input.
The end of the input would be signalled by pressing Ctrl+D.
The above would possibly intermingle the pasted contents and the result from grep
. To accumulate the pasted content first and then run grep
, you could use something like
gsi ()
trap 'rm -f "$buffer"' RETURN
local buffer="$(mktemp)"
cat >"$buffer" && grep --color "$@" "$buffer"
Here, the pasted output is put into a temporary file before the grep
is run. The temporary file is removed when the function exits. This is approximately what bash
would do behind the scenes when you paste into a here-document.
2
This works, but is a bit awkward in that if the input is pasted to the terminal, it will get intermixed with output fromgrep
. The here-doc works as a work-around for that, though I suppose one could wrap thegrep
withinstty -echo
andstty echo
to hide the pasted text...
â ilkkachu
Aug 15 at 14:38
@ilkkachu True, the here-document in the question would act like a temporary buffer. I'll try your suggestion and make an update when I'm in front of a computer later.
â Kusalananda
Aug 15 at 15:03
@Kusalananda, ideally, I would have both pasted andgrep
ed text on screen.
â 1.61803
Aug 15 at 16:04
add a comment |Â
up vote
2
down vote
You mentioned you're pasting to terminal. Abandon here-doc altogether, and use a proper clipboard utility, such as xclip
(or pbpaste
on Mac OS X). With xclip
that'd be done as
xclip -o -sel clip | grep --color 'sometext'
See also 'xclip' vs. 'xsel' on the difference between the two utilities.
Thanks for the suggestion, but in this case I rather not use additional software, which by the way isn't that widespread.
â 1.61803
Aug 15 at 16:13
@1.61803 Understandable. Hopefully, this helps to others.xclip
is actually a fairly common software, at least across Linux repositories.
â Sergiy Kolodyazhnyy
Aug 15 at 16:16
repology.org/metapackage/xclip/versions
â 1.61803
Aug 15 at 17:48
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
alias 'gsi=<<eof grep --color'
Would work as alias is just like preprocessor text replacement, where the replacement is interpreted as shell code again.
Yours was not working as you had that "$1"
. With gsi file.txt
replaced with cat <<eof | grep --color "$1" file.txt
, then the shell carries on interpreting that command line and $1
at that point is the shell's first positional parameter which is probably empty unless you did a set something
beforehand. So you'd want to remove that "$1"
here. You can remove the UUOC as well.
gsi() cat <<eof
is wrong from a syntax point of view as the eof
line is missing after that cat <<eof
.
Thanks, for emending the alias. I still don't understand whyhere-document
is executed during function definition.
â 1.61803
Aug 15 at 16:14
@1.61803 it's not that the heredoc is executed, it's that the function body has to be syntactically complete so that bash can understand how much of what you have given is the function, unlike aliases which are essentially simple text replacements.
â muru
Aug 16 at 0:31
add a comment |Â
up vote
5
down vote
alias 'gsi=<<eof grep --color'
Would work as alias is just like preprocessor text replacement, where the replacement is interpreted as shell code again.
Yours was not working as you had that "$1"
. With gsi file.txt
replaced with cat <<eof | grep --color "$1" file.txt
, then the shell carries on interpreting that command line and $1
at that point is the shell's first positional parameter which is probably empty unless you did a set something
beforehand. So you'd want to remove that "$1"
here. You can remove the UUOC as well.
gsi() cat <<eof
is wrong from a syntax point of view as the eof
line is missing after that cat <<eof
.
Thanks, for emending the alias. I still don't understand whyhere-document
is executed during function definition.
â 1.61803
Aug 15 at 16:14
@1.61803 it's not that the heredoc is executed, it's that the function body has to be syntactically complete so that bash can understand how much of what you have given is the function, unlike aliases which are essentially simple text replacements.
â muru
Aug 16 at 0:31
add a comment |Â
up vote
5
down vote
up vote
5
down vote
alias 'gsi=<<eof grep --color'
Would work as alias is just like preprocessor text replacement, where the replacement is interpreted as shell code again.
Yours was not working as you had that "$1"
. With gsi file.txt
replaced with cat <<eof | grep --color "$1" file.txt
, then the shell carries on interpreting that command line and $1
at that point is the shell's first positional parameter which is probably empty unless you did a set something
beforehand. So you'd want to remove that "$1"
here. You can remove the UUOC as well.
gsi() cat <<eof
is wrong from a syntax point of view as the eof
line is missing after that cat <<eof
.
alias 'gsi=<<eof grep --color'
Would work as alias is just like preprocessor text replacement, where the replacement is interpreted as shell code again.
Yours was not working as you had that "$1"
. With gsi file.txt
replaced with cat <<eof | grep --color "$1" file.txt
, then the shell carries on interpreting that command line and $1
at that point is the shell's first positional parameter which is probably empty unless you did a set something
beforehand. So you'd want to remove that "$1"
here. You can remove the UUOC as well.
gsi() cat <<eof
is wrong from a syntax point of view as the eof
line is missing after that cat <<eof
.
answered Aug 15 at 14:26
Stéphane Chazelas
285k53524864
285k53524864
Thanks, for emending the alias. I still don't understand whyhere-document
is executed during function definition.
â 1.61803
Aug 15 at 16:14
@1.61803 it's not that the heredoc is executed, it's that the function body has to be syntactically complete so that bash can understand how much of what you have given is the function, unlike aliases which are essentially simple text replacements.
â muru
Aug 16 at 0:31
add a comment |Â
Thanks, for emending the alias. I still don't understand whyhere-document
is executed during function definition.
â 1.61803
Aug 15 at 16:14
@1.61803 it's not that the heredoc is executed, it's that the function body has to be syntactically complete so that bash can understand how much of what you have given is the function, unlike aliases which are essentially simple text replacements.
â muru
Aug 16 at 0:31
Thanks, for emending the alias. I still don't understand why
here-document
is executed during function definition.â 1.61803
Aug 15 at 16:14
Thanks, for emending the alias. I still don't understand why
here-document
is executed during function definition.â 1.61803
Aug 15 at 16:14
@1.61803 it's not that the heredoc is executed, it's that the function body has to be syntactically complete so that bash can understand how much of what you have given is the function, unlike aliases which are essentially simple text replacements.
â muru
Aug 16 at 0:31
@1.61803 it's not that the heredoc is executed, it's that the function body has to be syntactically complete so that bash can understand how much of what you have given is the function, unlike aliases which are essentially simple text replacements.
â muru
Aug 16 at 0:31
add a comment |Â
up vote
4
down vote
Your function would not be using a here-document:
gsi ()
grep --color "$@"
This would enable you to pass a pattern as well as any other options to grep
on the common line and grep
would read from standard input.
The end of the input would be signalled by pressing Ctrl+D.
The above would possibly intermingle the pasted contents and the result from grep
. To accumulate the pasted content first and then run grep
, you could use something like
gsi ()
trap 'rm -f "$buffer"' RETURN
local buffer="$(mktemp)"
cat >"$buffer" && grep --color "$@" "$buffer"
Here, the pasted output is put into a temporary file before the grep
is run. The temporary file is removed when the function exits. This is approximately what bash
would do behind the scenes when you paste into a here-document.
2
This works, but is a bit awkward in that if the input is pasted to the terminal, it will get intermixed with output fromgrep
. The here-doc works as a work-around for that, though I suppose one could wrap thegrep
withinstty -echo
andstty echo
to hide the pasted text...
â ilkkachu
Aug 15 at 14:38
@ilkkachu True, the here-document in the question would act like a temporary buffer. I'll try your suggestion and make an update when I'm in front of a computer later.
â Kusalananda
Aug 15 at 15:03
@Kusalananda, ideally, I would have both pasted andgrep
ed text on screen.
â 1.61803
Aug 15 at 16:04
add a comment |Â
up vote
4
down vote
Your function would not be using a here-document:
gsi ()
grep --color "$@"
This would enable you to pass a pattern as well as any other options to grep
on the common line and grep
would read from standard input.
The end of the input would be signalled by pressing Ctrl+D.
The above would possibly intermingle the pasted contents and the result from grep
. To accumulate the pasted content first and then run grep
, you could use something like
gsi ()
trap 'rm -f "$buffer"' RETURN
local buffer="$(mktemp)"
cat >"$buffer" && grep --color "$@" "$buffer"
Here, the pasted output is put into a temporary file before the grep
is run. The temporary file is removed when the function exits. This is approximately what bash
would do behind the scenes when you paste into a here-document.
2
This works, but is a bit awkward in that if the input is pasted to the terminal, it will get intermixed with output fromgrep
. The here-doc works as a work-around for that, though I suppose one could wrap thegrep
withinstty -echo
andstty echo
to hide the pasted text...
â ilkkachu
Aug 15 at 14:38
@ilkkachu True, the here-document in the question would act like a temporary buffer. I'll try your suggestion and make an update when I'm in front of a computer later.
â Kusalananda
Aug 15 at 15:03
@Kusalananda, ideally, I would have both pasted andgrep
ed text on screen.
â 1.61803
Aug 15 at 16:04
add a comment |Â
up vote
4
down vote
up vote
4
down vote
Your function would not be using a here-document:
gsi ()
grep --color "$@"
This would enable you to pass a pattern as well as any other options to grep
on the common line and grep
would read from standard input.
The end of the input would be signalled by pressing Ctrl+D.
The above would possibly intermingle the pasted contents and the result from grep
. To accumulate the pasted content first and then run grep
, you could use something like
gsi ()
trap 'rm -f "$buffer"' RETURN
local buffer="$(mktemp)"
cat >"$buffer" && grep --color "$@" "$buffer"
Here, the pasted output is put into a temporary file before the grep
is run. The temporary file is removed when the function exits. This is approximately what bash
would do behind the scenes when you paste into a here-document.
Your function would not be using a here-document:
gsi ()
grep --color "$@"
This would enable you to pass a pattern as well as any other options to grep
on the common line and grep
would read from standard input.
The end of the input would be signalled by pressing Ctrl+D.
The above would possibly intermingle the pasted contents and the result from grep
. To accumulate the pasted content first and then run grep
, you could use something like
gsi ()
trap 'rm -f "$buffer"' RETURN
local buffer="$(mktemp)"
cat >"$buffer" && grep --color "$@" "$buffer"
Here, the pasted output is put into a temporary file before the grep
is run. The temporary file is removed when the function exits. This is approximately what bash
would do behind the scenes when you paste into a here-document.
edited Aug 15 at 16:40
answered Aug 15 at 14:30
Kusalananda
106k14209327
106k14209327
2
This works, but is a bit awkward in that if the input is pasted to the terminal, it will get intermixed with output fromgrep
. The here-doc works as a work-around for that, though I suppose one could wrap thegrep
withinstty -echo
andstty echo
to hide the pasted text...
â ilkkachu
Aug 15 at 14:38
@ilkkachu True, the here-document in the question would act like a temporary buffer. I'll try your suggestion and make an update when I'm in front of a computer later.
â Kusalananda
Aug 15 at 15:03
@Kusalananda, ideally, I would have both pasted andgrep
ed text on screen.
â 1.61803
Aug 15 at 16:04
add a comment |Â
2
This works, but is a bit awkward in that if the input is pasted to the terminal, it will get intermixed with output fromgrep
. The here-doc works as a work-around for that, though I suppose one could wrap thegrep
withinstty -echo
andstty echo
to hide the pasted text...
â ilkkachu
Aug 15 at 14:38
@ilkkachu True, the here-document in the question would act like a temporary buffer. I'll try your suggestion and make an update when I'm in front of a computer later.
â Kusalananda
Aug 15 at 15:03
@Kusalananda, ideally, I would have both pasted andgrep
ed text on screen.
â 1.61803
Aug 15 at 16:04
2
2
This works, but is a bit awkward in that if the input is pasted to the terminal, it will get intermixed with output from
grep
. The here-doc works as a work-around for that, though I suppose one could wrap the grep
within stty -echo
and stty echo
to hide the pasted text...â ilkkachu
Aug 15 at 14:38
This works, but is a bit awkward in that if the input is pasted to the terminal, it will get intermixed with output from
grep
. The here-doc works as a work-around for that, though I suppose one could wrap the grep
within stty -echo
and stty echo
to hide the pasted text...â ilkkachu
Aug 15 at 14:38
@ilkkachu True, the here-document in the question would act like a temporary buffer. I'll try your suggestion and make an update when I'm in front of a computer later.
â Kusalananda
Aug 15 at 15:03
@ilkkachu True, the here-document in the question would act like a temporary buffer. I'll try your suggestion and make an update when I'm in front of a computer later.
â Kusalananda
Aug 15 at 15:03
@Kusalananda, ideally, I would have both pasted and
grep
ed text on screen.â 1.61803
Aug 15 at 16:04
@Kusalananda, ideally, I would have both pasted and
grep
ed text on screen.â 1.61803
Aug 15 at 16:04
add a comment |Â
up vote
2
down vote
You mentioned you're pasting to terminal. Abandon here-doc altogether, and use a proper clipboard utility, such as xclip
(or pbpaste
on Mac OS X). With xclip
that'd be done as
xclip -o -sel clip | grep --color 'sometext'
See also 'xclip' vs. 'xsel' on the difference between the two utilities.
Thanks for the suggestion, but in this case I rather not use additional software, which by the way isn't that widespread.
â 1.61803
Aug 15 at 16:13
@1.61803 Understandable. Hopefully, this helps to others.xclip
is actually a fairly common software, at least across Linux repositories.
â Sergiy Kolodyazhnyy
Aug 15 at 16:16
repology.org/metapackage/xclip/versions
â 1.61803
Aug 15 at 17:48
add a comment |Â
up vote
2
down vote
You mentioned you're pasting to terminal. Abandon here-doc altogether, and use a proper clipboard utility, such as xclip
(or pbpaste
on Mac OS X). With xclip
that'd be done as
xclip -o -sel clip | grep --color 'sometext'
See also 'xclip' vs. 'xsel' on the difference between the two utilities.
Thanks for the suggestion, but in this case I rather not use additional software, which by the way isn't that widespread.
â 1.61803
Aug 15 at 16:13
@1.61803 Understandable. Hopefully, this helps to others.xclip
is actually a fairly common software, at least across Linux repositories.
â Sergiy Kolodyazhnyy
Aug 15 at 16:16
repology.org/metapackage/xclip/versions
â 1.61803
Aug 15 at 17:48
add a comment |Â
up vote
2
down vote
up vote
2
down vote
You mentioned you're pasting to terminal. Abandon here-doc altogether, and use a proper clipboard utility, such as xclip
(or pbpaste
on Mac OS X). With xclip
that'd be done as
xclip -o -sel clip | grep --color 'sometext'
See also 'xclip' vs. 'xsel' on the difference between the two utilities.
You mentioned you're pasting to terminal. Abandon here-doc altogether, and use a proper clipboard utility, such as xclip
(or pbpaste
on Mac OS X). With xclip
that'd be done as
xclip -o -sel clip | grep --color 'sometext'
See also 'xclip' vs. 'xsel' on the difference between the two utilities.
edited Aug 15 at 16:27
answered Aug 15 at 16:07
Sergiy Kolodyazhnyy
7,93511648
7,93511648
Thanks for the suggestion, but in this case I rather not use additional software, which by the way isn't that widespread.
â 1.61803
Aug 15 at 16:13
@1.61803 Understandable. Hopefully, this helps to others.xclip
is actually a fairly common software, at least across Linux repositories.
â Sergiy Kolodyazhnyy
Aug 15 at 16:16
repology.org/metapackage/xclip/versions
â 1.61803
Aug 15 at 17:48
add a comment |Â
Thanks for the suggestion, but in this case I rather not use additional software, which by the way isn't that widespread.
â 1.61803
Aug 15 at 16:13
@1.61803 Understandable. Hopefully, this helps to others.xclip
is actually a fairly common software, at least across Linux repositories.
â Sergiy Kolodyazhnyy
Aug 15 at 16:16
repology.org/metapackage/xclip/versions
â 1.61803
Aug 15 at 17:48
Thanks for the suggestion, but in this case I rather not use additional software, which by the way isn't that widespread.
â 1.61803
Aug 15 at 16:13
Thanks for the suggestion, but in this case I rather not use additional software, which by the way isn't that widespread.
â 1.61803
Aug 15 at 16:13
@1.61803 Understandable. Hopefully, this helps to others.
xclip
is actually a fairly common software, at least across Linux repositories.â Sergiy Kolodyazhnyy
Aug 15 at 16:16
@1.61803 Understandable. Hopefully, this helps to others.
xclip
is actually a fairly common software, at least across Linux repositories.â Sergiy Kolodyazhnyy
Aug 15 at 16:16
repology.org/metapackage/xclip/versions
â 1.61803
Aug 15 at 17:48
repology.org/metapackage/xclip/versions
â 1.61803
Aug 15 at 17:48
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f462759%2fgrep-with-heredoc-in-function%23new-answer', 'question_page');
);
Post as a guest
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
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
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