The Computer Oracle

How to browse bash history between certain line numbers

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Darkness Approaches Looping

--

Chapters
00:00 How To Browse Bash History Between Certain Line Numbers
00:20 Answer 1 Score 10
00:41 Accepted Answer Score 23
00:59 Answer 3 Score 1
01:25 Answer 4 Score 1
01:45 Thank you

--

Full question
https://superuser.com/questions/1105016/...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#linux #commandline #bash #history

#avk47



ACCEPTED ANSWER

Score 23


You can use something like

history | grep -A 135 -w 321

It starts with line 321 and shows the next 135 lines, so it will show lines 321 to 456.




ANSWER 2

Score 10


Use head and tail:

history | head -n 456 | tail -n 136

Which will get the first 456 (up to the end you want) and then you get the last 136 (which computes as 456 - 136 = 320, but will fetch from the 321st record from history).




ANSWER 3

Score 1


I assume you mean command numbers and not line numbers. I would use sed to give me the lines in the file between the two command numbers.

$ history | sed -n '/^ 321 /,/^ 456 /p'

^ character matches at the start of the line. Also a space after the number to make sure ^ 321 doesn't match line equalling 32155 ls




ANSWER 4

Score 1


The solution below strives for the least dependencies, least mathematical calculation, maximal readability(tested under GNU grep):

# Display history lines exactly from line321 to line456
history | grep -A $((456-321)) '321\ \ '

# Or throw away 'Space Escaping', a even 
# more simple command we'd get , but the output 
# will contain one more line, which is this command itself.
history | grep -A $((456-321)) '321  '

with output:

321 some-command
322 some-command
...
...
...
456 some-command