From owner-freebsd-questions@freebsd.org Thu Nov 2 20:46:45 2017 Return-Path: Delivered-To: freebsd-questions@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id C4DC1E62C5B for ; Thu, 2 Nov 2017 20:46:45 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from mailrelay14.qsc.de (mailrelay14.qsc.de [212.99.163.154]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "*.antispameurope.com", Issuer "TeleSec ServerPass Class 2 CA" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4AEEC69130 for ; Thu, 2 Nov 2017 20:46:44 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from mx01.qsc.de ([213.148.129.14]) by mailrelay14.qsc.de; Thu, 02 Nov 2017 21:46:35 +0100 Received: from r56.edvax.de (port-92-195-25-5.dynamic.qsc.de [92.195.25.5]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx01.qsc.de (Postfix) with ESMTPS id 84CBC3CBF9; Thu, 2 Nov 2017 21:46:39 +0100 (CET) Received: from r56.edvax.de (localhost [127.0.0.1]) by r56.edvax.de (8.14.5/8.14.5) with SMTP id vA2KkYxA004621; Thu, 2 Nov 2017 21:46:34 +0100 (CET) (envelope-from freebsd@edvax.de) Date: Thu, 2 Nov 2017 21:46:34 +0100 From: Polytropon To: byrnejb@harte-lyne.ca Cc: "James B. Byrne via freebsd-questions" Subject: Re: Reconstruct a bash_history file Message-Id: <20171102214634.dc4877cf.freebsd@edvax.de> In-Reply-To: References: Reply-To: Polytropon Organization: EDVAX X-Mailer: Sylpheed 3.1.1 (GTK+ 2.24.5; i386-portbld-freebsd8.2) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-cloud-security-sender: freebsd@edvax.de X-cloud-security-recipient: freebsd-questions@freebsd.org X-cloud-security-Virusscan: CLEAN X-cloud-security-disclaimer: This E-Mail was scanned by E-Mailservice on mailrelay14.qsc.de with 4A1F56C3647 X-cloud-security-connect: mx01.qsc.de[213.148.129.14], TLS=1, IP=213.148.129.14 X-cloud-security: scantime:.1459 X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 02 Nov 2017 20:46:45 -0000 On Thu, 2 Nov 2017 15:13:50 -0400, James B. Byrne via freebsd-questions wrote: > I wish to reconstruct a bash_history file for a user who has > HISTTIMEFORMAT=%F %T:. I have a history log file having the following > format: > . . . > 2014-06-19 16:09:25: whois playford.de > 2014-06-20 08:24:00: man xpdf > 2014-06-23 08:22:16: su -l > 2014-06-24 09:33:27: cd Desktop > 2014-06-25 09:22:25: su -l > 2014-06-26 12:15:28: su -l > 2014-06-27 08:48:40: su -l > 2014-06-30 08:18:04: > 2014-07-02 15:40:57: history | grep enscript > 2014-07-03 08:38:55: ll > 2014-07-04 09:29:04: su -l > 2014-07-07 09:33:34: ssh -Y xnet241 > 2014-07-08 08:29:59: su -l > 2014-07-09 06:53:46: su -l > 2014-07-10 11:20:20: whois arcom.com.br > 2014-07-11 10:02:24: su -l > 2014-07-14 09:58:27: su -l > 2014-07-15 08:46:38: su -l > 2014-07-16 10:31:52: sshtn xnet241 > 2014-07-17 09:50:23: history | grep PORT > . . . > > Is there a way to recreate a valid bash_history file from this data > and preserve the time stamps? Yes. > If so then how? 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. ;-) For your example input text, I get the following result: #1403186965 whois playford.de #1403245440 man xpdf #1403504536 su -l #1403595207 cd Desktop #1403680945 su -l #1403777728 su -l #1403851720 su -l #1404109084 #1404308457 history | grep enscript #1404369535 ll #1404458944 su -l #1404718414 ssh -Y xnet241 #1404800999 su -l #1404881626 su -l #1404984020 whois arcom.com.br #1405065744 su -l #1405324707 su -l #1405406798 su -l #1405499512 sshtn xnet241 #1405583423 history | grep PORT Does this look correct? -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ...