Search history and get temporal context with specified window size
Clash Royale CLAN TAG#URR8PPP
I often find myself searching in my history by doing something like
history | grep searchterm
This returns a list of all the times that I used searchterm
in a command. However, I would often like to know the n previous commands and the m next commands that I entered before and after that command which contained searchterm
. Often the context in which I ran a command is very useful.
What I would like to be able to do is
> history-search-context searchterm n m
and get separated lists of commands that were entered n commands before and m commands after any history entry that contains searchterm
.
Example:
> history-search-context mycommand 2 1
687 cd some/directory
688 ls -a
689 mycommand -flag arg1
690 touch file
-------------
943 apt-get install something
944 pwd
945 mycommand -otherflag arg1 arg2 arg3
946 rm file
This would tell me that I used mycommand
twice in recorded history with some interesting flags and arguments, and within a window of [-2,+1] commands I had one time run it in some/directory
and had to create a file, and the other time, I installed something
, checked which directory I was in, and then deleted file
.
Is there an existing way to do this? If not, how to accomplish this?
history
add a comment |
I often find myself searching in my history by doing something like
history | grep searchterm
This returns a list of all the times that I used searchterm
in a command. However, I would often like to know the n previous commands and the m next commands that I entered before and after that command which contained searchterm
. Often the context in which I ran a command is very useful.
What I would like to be able to do is
> history-search-context searchterm n m
and get separated lists of commands that were entered n commands before and m commands after any history entry that contains searchterm
.
Example:
> history-search-context mycommand 2 1
687 cd some/directory
688 ls -a
689 mycommand -flag arg1
690 touch file
-------------
943 apt-get install something
944 pwd
945 mycommand -otherflag arg1 arg2 arg3
946 rm file
This would tell me that I used mycommand
twice in recorded history with some interesting flags and arguments, and within a window of [-2,+1] commands I had one time run it in some/directory
and had to create a file, and the other time, I installed something
, checked which directory I was in, and then deleted file
.
Is there an existing way to do this? If not, how to accomplish this?
history
If my answer solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Feb 17 at 13:45
add a comment |
I often find myself searching in my history by doing something like
history | grep searchterm
This returns a list of all the times that I used searchterm
in a command. However, I would often like to know the n previous commands and the m next commands that I entered before and after that command which contained searchterm
. Often the context in which I ran a command is very useful.
What I would like to be able to do is
> history-search-context searchterm n m
and get separated lists of commands that were entered n commands before and m commands after any history entry that contains searchterm
.
Example:
> history-search-context mycommand 2 1
687 cd some/directory
688 ls -a
689 mycommand -flag arg1
690 touch file
-------------
943 apt-get install something
944 pwd
945 mycommand -otherflag arg1 arg2 arg3
946 rm file
This would tell me that I used mycommand
twice in recorded history with some interesting flags and arguments, and within a window of [-2,+1] commands I had one time run it in some/directory
and had to create a file, and the other time, I installed something
, checked which directory I was in, and then deleted file
.
Is there an existing way to do this? If not, how to accomplish this?
history
I often find myself searching in my history by doing something like
history | grep searchterm
This returns a list of all the times that I used searchterm
in a command. However, I would often like to know the n previous commands and the m next commands that I entered before and after that command which contained searchterm
. Often the context in which I ran a command is very useful.
What I would like to be able to do is
> history-search-context searchterm n m
and get separated lists of commands that were entered n commands before and m commands after any history entry that contains searchterm
.
Example:
> history-search-context mycommand 2 1
687 cd some/directory
688 ls -a
689 mycommand -flag arg1
690 touch file
-------------
943 apt-get install something
944 pwd
945 mycommand -otherflag arg1 arg2 arg3
946 rm file
This would tell me that I used mycommand
twice in recorded history with some interesting flags and arguments, and within a window of [-2,+1] commands I had one time run it in some/directory
and had to create a file, and the other time, I installed something
, checked which directory I was in, and then deleted file
.
Is there an existing way to do this? If not, how to accomplish this?
history
history
asked Feb 9 at 20:26
user10108user10108
61
61
If my answer solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Feb 17 at 13:45
add a comment |
If my answer solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Feb 17 at 13:45
If my answer solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Feb 17 at 13:45
If my answer solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Feb 17 at 13:45
add a comment |
1 Answer
1
active
oldest
votes
I would approach it with a helper function and grep
, since (GNU) grep has a natural set of options for "lines before & after the match". The bare-bones version is:
histgrep() grep -B $2 -A $3 "$1"
... and you'd use it just like your history-search-context command above:
$ histgrep jeff 1 1
517 vi go.sh
518 x=jeff
519 y=/tmp
--
832 sudo visudo
833 sudo adduser jeff2
834 man sudoers
--
841 sudo ls -al /var/lib/sudo/lectured
842 sudo rm /var/lib/sudo/lectured/jeff /var/lib/sudo/lectured/schaller
843 sudo ls
--
940 ls *[[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]]*
941 touch jeff߄߅߆߇߈߉.txt
942 ls
--
1235 histgrep a b c
1236 histgrep jeff a 1
1237 histgrep jeff 1 a
1238 histgrep jeff 1 1
After adding some basic error-checking the function would be:
histgrep() grep -B $2 -A $3 "$1"
You could easily enough name it history-search-context
instead of histgrep
if you wanted:
history-search-context()
# ...
This is exactly what I was looking for. Perfect. Thanks so much for the solution.
– user10108
Feb 10 at 21:21
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%2f499685%2fsearch-history-and-get-temporal-context-with-specified-window-size%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
I would approach it with a helper function and grep
, since (GNU) grep has a natural set of options for "lines before & after the match". The bare-bones version is:
histgrep() grep -B $2 -A $3 "$1"
... and you'd use it just like your history-search-context command above:
$ histgrep jeff 1 1
517 vi go.sh
518 x=jeff
519 y=/tmp
--
832 sudo visudo
833 sudo adduser jeff2
834 man sudoers
--
841 sudo ls -al /var/lib/sudo/lectured
842 sudo rm /var/lib/sudo/lectured/jeff /var/lib/sudo/lectured/schaller
843 sudo ls
--
940 ls *[[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]]*
941 touch jeff߄߅߆߇߈߉.txt
942 ls
--
1235 histgrep a b c
1236 histgrep jeff a 1
1237 histgrep jeff 1 a
1238 histgrep jeff 1 1
After adding some basic error-checking the function would be:
histgrep() grep -B $2 -A $3 "$1"
You could easily enough name it history-search-context
instead of histgrep
if you wanted:
history-search-context()
# ...
This is exactly what I was looking for. Perfect. Thanks so much for the solution.
– user10108
Feb 10 at 21:21
add a comment |
I would approach it with a helper function and grep
, since (GNU) grep has a natural set of options for "lines before & after the match". The bare-bones version is:
histgrep() grep -B $2 -A $3 "$1"
... and you'd use it just like your history-search-context command above:
$ histgrep jeff 1 1
517 vi go.sh
518 x=jeff
519 y=/tmp
--
832 sudo visudo
833 sudo adduser jeff2
834 man sudoers
--
841 sudo ls -al /var/lib/sudo/lectured
842 sudo rm /var/lib/sudo/lectured/jeff /var/lib/sudo/lectured/schaller
843 sudo ls
--
940 ls *[[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]]*
941 touch jeff߄߅߆߇߈߉.txt
942 ls
--
1235 histgrep a b c
1236 histgrep jeff a 1
1237 histgrep jeff 1 a
1238 histgrep jeff 1 1
After adding some basic error-checking the function would be:
histgrep() grep -B $2 -A $3 "$1"
You could easily enough name it history-search-context
instead of histgrep
if you wanted:
history-search-context()
# ...
This is exactly what I was looking for. Perfect. Thanks so much for the solution.
– user10108
Feb 10 at 21:21
add a comment |
I would approach it with a helper function and grep
, since (GNU) grep has a natural set of options for "lines before & after the match". The bare-bones version is:
histgrep() grep -B $2 -A $3 "$1"
... and you'd use it just like your history-search-context command above:
$ histgrep jeff 1 1
517 vi go.sh
518 x=jeff
519 y=/tmp
--
832 sudo visudo
833 sudo adduser jeff2
834 man sudoers
--
841 sudo ls -al /var/lib/sudo/lectured
842 sudo rm /var/lib/sudo/lectured/jeff /var/lib/sudo/lectured/schaller
843 sudo ls
--
940 ls *[[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]]*
941 touch jeff߄߅߆߇߈߉.txt
942 ls
--
1235 histgrep a b c
1236 histgrep jeff a 1
1237 histgrep jeff 1 a
1238 histgrep jeff 1 1
After adding some basic error-checking the function would be:
histgrep() grep -B $2 -A $3 "$1"
You could easily enough name it history-search-context
instead of histgrep
if you wanted:
history-search-context()
# ...
I would approach it with a helper function and grep
, since (GNU) grep has a natural set of options for "lines before & after the match". The bare-bones version is:
histgrep() grep -B $2 -A $3 "$1"
... and you'd use it just like your history-search-context command above:
$ histgrep jeff 1 1
517 vi go.sh
518 x=jeff
519 y=/tmp
--
832 sudo visudo
833 sudo adduser jeff2
834 man sudoers
--
841 sudo ls -al /var/lib/sudo/lectured
842 sudo rm /var/lib/sudo/lectured/jeff /var/lib/sudo/lectured/schaller
843 sudo ls
--
940 ls *[[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]]*
941 touch jeff߄߅߆߇߈߉.txt
942 ls
--
1235 histgrep a b c
1236 histgrep jeff a 1
1237 histgrep jeff 1 a
1238 histgrep jeff 1 1
After adding some basic error-checking the function would be:
histgrep() grep -B $2 -A $3 "$1"
You could easily enough name it history-search-context
instead of histgrep
if you wanted:
history-search-context()
# ...
answered Feb 9 at 23:27
Jeff SchallerJeff Schaller
42.9k1159137
42.9k1159137
This is exactly what I was looking for. Perfect. Thanks so much for the solution.
– user10108
Feb 10 at 21:21
add a comment |
This is exactly what I was looking for. Perfect. Thanks so much for the solution.
– user10108
Feb 10 at 21:21
This is exactly what I was looking for. Perfect. Thanks so much for the solution.
– user10108
Feb 10 at 21:21
This is exactly what I was looking for. Perfect. Thanks so much for the solution.
– user10108
Feb 10 at 21:21
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%2f499685%2fsearch-history-and-get-temporal-context-with-specified-window-size%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
If my answer solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Feb 17 at 13:45