Skip to content

Instantly share code, notes, and snippets.

@pklaus pklaus/hist_to_pandas.py
Last active Aug 31, 2015

Embed
What would you like to do?
Read your Bash history into a Pandas DataFrame with this Python script.
#!/usr/bin/env python
# -*- encoding: UTF8 -*-
""" Read all entered bash commands ('the history') into a Pandas dataframe
http://blog.philippklaus.de/2015/04/enhancing-and-enlarging-the-bash-history/
"""
import pandas as pd
import matplotlib.pyplot as plt
from IPython import embed
from os import path
from datetime import datetime
MSG = """
Find out the called tool:
df.tool = df.command.apply(lambda x: x.split()[0])
print("The most frequenctly used commands:")
print("df.tool.value_counts()[:30]")
print(df.tool.value_counts()[:30])
Plot daily stats:
dfdt = df.set_index('dt')
dfdt.resample('D', how='count').plot()
plt.show()
"""
def main():
import argparse
parser = argparse.ArgumentParser(description='Hist to DB')
args = parser.parse_args()
dt = None
command = ''
commands = dict(dt=[], command=[])
# Open the history file as UTF8 (or check os.environ['LANG'] ?)
for line in open(path.expanduser('~/.bash_history'),'rb'):
try:
line = line.decode('utf8')
except:
print('The following line cannot be decoded with UTF-8 - ignoring it:')
print(repr(line))
line = line.decode('utf8', 'ignore')
line = line.strip()
if len(line)>0 and line[0]=='#':
try:
dt = datetime.fromtimestamp(int(line.replace("#","")))
continue
except:
dt = None
if line == '': continue
commands['dt'].append(dt)
commands['command'].append(line)
dt = None
df = pd.DataFrame.from_dict(commands)
print(MSG)
embed()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.