Saturday, June 1, 2013

Bash to Python migration cheat sheet

Explanation Bash Python
change directory cd os.chdir(“/some/path/”)
print working directory pwd os.getdir()
print 4th line sed -n “4p” somefile linecache.getline(‘somefile’, 4)
list files ls subprocess.call(“ls”, shell=True)
remove/delete a file rm somefile subprocess.call(“rm somefile”, shell=True)
print 3rd column in a line echo ‘this is a line’ | awk ‘{ print $3 }’ “this is a line”.split()[3]
print 3rd column in a file awk ‘{ print $3 }’ somefile
for line in somefile
    line.split()[2]