Reconstruct a bash_history file

Ian Smith smithi at nimnet.asn.au
Fri Nov 3 12:37:57 UTC 2017


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

cheers, Ian


More information about the freebsd-questions mailing list