Reconstruct a bash_history file

Polytropon freebsd at edvax.de
Fri Nov 3 13:50:34 UTC 2017


On Fri, 3 Nov 2017 23:37:29 +1100 (EST), Ian Smith wrote:
> In freebsd-questions Digest, Vol 700, Issue 5, Message: 7
> On Thu, 2 Nov 2017 21:46:34 +0100 Polytropon <freebsd at edvax.de> wrote:
> 
>  > You need the YYYY-MM-DD HH:MM:SS timestamp to the Epoch format,
>  > prefix it with a #, and put the command on a new line.
>  > 
>  > Maybe like this, if you don't mind a multiple-line one-liner
>  > of regular shell script:
>  > 
>  > 	$ cat history.txt | while read LINE; do DATETIME=`echo $LINE | cut -d ':' -f 1-3`; TIMESTAMP=`date -j -f "%Y-%m-%d %H:%M:%S" "${DATETIME}" "+#%s"`; COMMAND=`echo $LINE | cut -d ':' -f 4-`; echo "${TIMESTAMP}"; echo "${COMMAND}" | sed "s/^ //"; done > bash_history.txt
>  > 
>  > This version is easier to read:
>  > 
>  > 	cat history.txt | while read LINE; do
>  > 		DATETIME=`echo $LINE | cut -d ':' -f 1-3`
>  > 		TIMESTAMP=`date -j -f "%Y-%m-%d %H:%M:%S" "${DATETIME}" "+#%s"`
>  > 		COMMAND=`echo $LINE | cut -d ':' -f 4-`
>  > 		echo "${TIMESTAMP}"
>  > 		echo "${COMMAND}" | sed "s/^ //"
>  > 	done > bash_history.txt
>  > 
>  > It features the "useless use of cat" line the one-liner. ;-)
> 
> Nice work!
> 
> A most useful "useless use of cat", but even that could be avoided with:
> 
> 	while read LINE; do
> 		[..]
> 	done < history.txt > bash_history.txt

Fully right. I thought about it after hitting "Send".
Additionally, the qupting for the echo commands isn't
that efficient, and I'd suggest do move the sed step
to the COMMAND= line. :-)



-- 
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...


More information about the freebsd-questions mailing list