x=list(open('../fd/0')) i,y,z,b=(0,)*4 a=99 for l in x: if '*'in l: y,z,a,b=y or i+1,i,min(a,l.find('*')),max(b,l.rfind('*')) i+=1 for l in x[y-1:z+1]:print l[a:b+1]
Note that non-ascii characters in the above source code will be escaped (such as \x9f).