converting the hash tag timestamps in history file to desired string

user1356163 picture user1356163 · May 19, 2012 · Viewed 7.4k times · Source

when i store the output of history command via ssh in a file i get something like this

ssh -i private_key user@ip 'export HISTFILE=~/.bash_history; export HISTTIMEFORMAT="%D-%T "; set -o history; history' > myfile.txt

OUTPUT

#1337431451
command

as far as ive learnt this hash string represents a timestamp. how do i change this to a string of my desired format

P.S- using history in ssh is not outputting with timestamps. Tried almost everything. So i guess the next best thing to do would be to convert these # timestamps to a readable date time format myself. How do i go about it?

Answer

tmf glzkv picture tmf glzkv · Apr 20, 2016

you can combine rows with paste command:

paste -sd '#\n' .bash_history

and convert date with strftime in awk:

echo 1461136015 | awk '{print strftime("%d/%m/%y %T",$1)}'

as a result bash history with timestamp can be parsed by the next command:

paste -sd '#\n' .bash_history | awk -F"#" '{d=$2 ; $2="";print NR" "strftime("%d/%m/%y %T",d)" "$0}'

which converts:

#1461137765
echo lala
#1461137767
echo bebe

to

1 20/04/16 10:36:05   echo lala
2 20/04/16 10:36:07   echo bebe

also you can create script like /usr/local/bin/fhistory with content:

#!/bin/bash

paste -sd '#\n' $1 | awk -F"#" '{d=$2 ; $2="";print NR" "strftime("%d/%m/%y %T",d)" "$0}'

and quickly parse bash history file with next command:

fhistory .bash_history