I have some files that I want to process, and I know how to do it in sed/awk (for each one):
awk '{if (index($0,"#")!=1) {line++; if (line%3==1) {print $2,$3}}}' q.post > q
or
grep -v "#" q.post | awk '{if (NR%3==1) {print $2,$3}}'
It's one line, and rather beautiful and clear.
Now, my main program is in python (2.7). Calling sed/awk from python is a bit tedious—I get some error—and I'd rather use a nice pythonic way to do it.
So far I have:
pp_files = glob.glob("*gauss.post")
for pp in pp_files:
ppf = open(pp)
with open(pp[:pp.rfind(".post")] + "_clean.post", "w") as outfile:
counter = 0
temp = []
for line in ppf.readlines():
if not line.startswith("#"):
temp.append(line)
for line in temp:
if counter % 3 == 0:
outfile.write(" ".join(line.split()[1:3]) + '\n')
counter += 1
ppf.close()
Meh.
It works, but it's not beautiful. Is there a pythonic way, preferentially a clear one liner (not 10 imbricated list comprehension, to replace awk and sed ?
Thanks