Search history and get temporal context with specified window size

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












1















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?










share|improve this question






















  • 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















1















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?










share|improve this question






















  • 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













1












1








1


0






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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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

















  • 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










1 Answer
1






active

oldest

votes


















0














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() 
# ...






share|improve this answer























  • This is exactly what I was looking for. Perfect. Thanks so much for the solution.

    – user10108
    Feb 10 at 21:21










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
);



);













draft saved

draft discarded


















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









0














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() 
# ...






share|improve this answer























  • This is exactly what I was looking for. Perfect. Thanks so much for the solution.

    – user10108
    Feb 10 at 21:21















0














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() 
# ...






share|improve this answer























  • This is exactly what I was looking for. Perfect. Thanks so much for the solution.

    – user10108
    Feb 10 at 21:21













0












0








0







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() 
# ...






share|improve this answer













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() 
# ...







share|improve this answer












share|improve this answer



share|improve this answer










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

















  • 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay