import sys
import numpy as nm

# sys.argv - command file arguments
print 'Processing filename ', sys.argv[1]
filename = sys.argv[1]

npat = len(sys.argv) - 2
narg = len(sys.argv)

fid = open(filename,'r')

header = fid.readline()
print 'Header:', header
header = header.split()
nhead = len(header)

print 'Searching for patterns ', sys.argv[2:] 

ncol = []
nnam = []
for i in range(npat):
        for j in range(nhead):
                print 'Looking at ', header[j]
                if sys.argv[2+i] in header[j]:
                        ncol.append(j)
                        nnam.append(header[j])

print 'Found columns', nnam
print 'Their numbers', ncol
ncols = len(ncol)

#Opening new file
fout = open(filename+'_','w')
#Writing header in new file
fout.write(' '.join(nnam)+'\n')


line = fid.readline()
while line != '':
        line = line.split()
        lineout = ''
        for i in range(ncols):
                lineout = lineout+line[ncol[i]]+' '
        fout.write(lineout+'\n')
        line = fid.readline()




