0

I want to copy to clipboard something like this

$ command1
$ command2

If you run history you will get the commands in reverse order, so I want to just skip a number of lines from the tail and replace the entry line number with '$'. As you probably suspect this is a very useful shorthand when having to log your workflow or write documentation.

Example:

$ history

 1340  pass
 1341  pass insert m clouds/cloud9
 1342  pass insert -m clouds/cloud9
 1343  sudo service docker start
 1344  history

So how do you turn that into:

$ sudo service docker start
$ pass insert -m clouds/cloud9
...etc
grokkaine
  • 789
  • 2
  • 7
  • 20
  • Do you know the `tac` command? And these other questions? [Q1](http://stackoverflow.com/questions/742466/how-can-i-reverse-the-order-of-lines-in-a-file) [Q2](http://stackoverflow.com/questions/8017456/output-file-lines-from-last-to-first-in-bash) – Jdamian Nov 11 '16 at 12:41
  • I did not use it before. Thanks! – grokkaine Nov 15 '16 at 10:17

2 Answers2

2

Assigning $1 works but it will leave a leading space

history | awk '{$1=""; print}'

If you want to copy this to the clipboard, you can use xclip

history | awk '{$1=""; print}' | xclip

Credit goes to https://stackoverflow.com/a/4198169/2032943

Community
  • 1
  • 1
Jay Rajput
  • 1,813
  • 17
  • 23
1

maybe you can use these;

 history | tac | awk 'NR>1&&NR<=3  {$1="$";print $0}'

tac - concatenate and print files in reverse

NR<=3 : means that the last two commands before history. NR>1 : to delete history command in history $1="$" : to replace line numbers to $

test :

$ echo first
first
$ echo second
second

$ history | tac | awk 'NR>1&&NR<=3  {$1="$";print $0}'
$ echo second
$ echo first
Mustafa DOGRU
  • 3,994
  • 1
  • 16
  • 24