Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 3 Nov 2017 14:50:26 +0100
From:      Polytropon <freebsd@edvax.de>
To:        Ian Smith <smithi@nimnet.asn.au>
Cc:        byrnejb@harte-lyne.ca, freebsd-questions@freebsd.org
Subject:   Re: Reconstruct a bash_history file
Message-ID:  <20171103145026.16c4aa5a.freebsd@edvax.de>
In-Reply-To: <20171103232233.P7397@sola.nimnet.asn.au>
References:  <mailman.75.1509710402.96436.freebsd-questions@freebsd.org> <20171103232233.P7397@sola.nimnet.asn.au>

next in thread | previous in thread | raw e-mail | index | archive | help
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@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, ...



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20171103145026.16c4aa5a.freebsd>