History list without timestamp and unique the results

7,278

Solution 1

Simply, history | sed 's/.[ ]*.[0-9]*.[ ]*//' | uniq | grep -i "heroku"

the sed will remove the any [spaces][numbers][spaces] at the start of each line

for optimization make it

history | grep -i "heroku" | sed 's/.[ ]*.[0-9]*.[ ]*//' | uniq

Solution 2

Addition to answer by @devav2

Clear/nullify the history timestamp environment variable

HISTTIMEFORMAT="";

Export the following command,

export HISTCONTROL=ignoredups --> This will ignore the duplicates that are executed in sequence

Solution 3

The timestamp can be suppressed (or modified) on a per-command basis:

If the HISTTIMEFORMAT variable is set and not null, its value is used as a format string for strftime(3) to print the time stamp associated with each displayed history entry. No time stamps are printed otherwise.

That just leaves the line numbers, which can't be suppressed, but which are padded to a fixed width (for line numbers < 100,000):

printf("%5d%c %s%s\n", line_number, modified ? '*' : ' ', timestamp, line);

Thus, to remove the prefix from each line, we need to snip the first 7 characters (5 digits, 1 marker character, and 1 space) e.g.:

$ HISTTIMEFORMAT= history 10 | cut -c8-

This can easily be added to an alias:

$ alias h='HISTTIMEFORMAT= history | cut -c8-'

Then invoked with e.g.:

$ h | grep whatever
$ h | head -n 10
Share:
7,278

Related videos on Youtube

justingordon
Author by

justingordon

Updated on September 18, 2022

Comments

  • justingordon
    justingordon over 1 year

    Using bash, I save my history with the timestamp.

    How do print the history omitting the timestamp?

    alias h=history
    alias g=grep -i
    

    To find lines that I used for heroku, I type:

    > h | g heroku
    

    I'd like to unique the results without the time-stamp, naturally.

    This question is somewhat related: How to avoid duplicate entries in .bash_history

    However, sometimes I want to see the duplicate in the history to see the context under which a command was run.

    • devav2
      devav2 over 11 years
      history | cut -d " " -f7-1000 | sort | uniq | grep heroku try this out.
    • justingordon
      justingordon over 11 years
      Awesome -- I was hoping that history had an option to do what the cut command does. Only issue with the above is that all entries get sorted alphabetically. I'd rather the results get sorted such that the most recently used commands are last, as I'm more likely to use more recently used commands.
  • justingordon
    justingordon about 10 years
    ignoredups makes up error work poorly, because if the recent command is further up in the list, it doesn't get added again.
  • GC 13
    GC 13 about 10 years
    @justingordon, Yes, using above comment by devav2, I have just added few information useful towards it.
  • GC 13
    GC 13 about 10 years
    @justingordon for your 1st commment, ignoredups will remove duplicates that are in sequence so if you want your history in timeline order, use following command export HISTCONTROL=ignoredups history | cut -d " " -f7-1000 | grep heroku further, if you want to see timestamp of a particular command, export HISTTIMEFORMAT=" %F %T " Hope this seems to be helpful
  • justingordon
    justingordon about 10 years
    Raj, this does what I need: "history | cut -d " " -f7-1000 | sort | uniq | grep heroku", but the uniq command only removes duplicate lines adjacent. A simple program could substitute for sort | uniq and do the trick. Just wondering if there was a clever built-in way to do this.
  • GC 13
    GC 13 about 10 years
    @justingordon sort command will break the relevance which can be useful for finding the context where a command was run.
  • dc95
    dc95 over 7 years
    To make this work in TTY terminal, remove the space between HISTTIMEFORMAT and =""; i.e. run HISTTIMEFORMAT="";
  • Zanna
    Zanna over 7 years
    that will only take the history entry number - if there are actually timestamps like 2 2016-10-05 00:26:00 vim .bashrc this is not going to remove them.