Alexej Magura
Alexej Magura

Reputation: 5119

get a particular line from history, bash

How would I get like line 78 from history in Bash?

Like I know that I could do something along the lines of:

history | grep 78 | \
awk '{idex=2; while (idex <= NF) { printf $idex " "; idex++} print }' | \
sed '2!d; s/\s\s[0-9]*.*$//'

The text that I'm trying to get out of this is:

npm uninstall foo; npm install -g foo

Upvotes: 1

Views: 444

Answers (3)

NeronLeVelu
NeronLeVelu

Reputation: 10039

history | sed -n "`78 {s/^ *[0-9]\{1,\}[[:space:]]*\([^ ]\)/\1/p;q;}"

if you absolutely want sed and use of history command (for example to add a "#" at the start of the line to secure any action of string) or in ksh where !78:p does not work

Upvotes: 1

chepner
chepner

Reputation: 531125

The p modifier will print any history expansion instead of executing it.

!78:p

Upvotes: 4

Alexej Magura
Alexej Magura

Reputation: 5119

To get line 78 from history in bash, run the following:

!78

Upvotes: 0

Related Questions