python - concatenate several file remove header lines -
what'd way concatenate several files, removing header lines (number of header lines not known in advance), , keeping first file header line header in new concatenated file?
i'd in python, awk or other languages work long can use subprocess call unix command.
note: header lines start #.
something using python:
files = ["file1","file2","file3"] open("output_file","w") outfile: open(files[0]) f1: line in f1: #keep header file1 outfile.write(line) x in files[1:]: open(x) f1: line in f1: if not line.startswith("#"): outfile.write(line)
you can use fileinput
module here:
this module implements helper class , functions write loop on standard input or list of files.
import fileinput header_over = false open("out_file","w") outfile: line in fileinput.input(): if line.startswith("#") , not header_over: outfile.write(line) elif not line.startswith("#"): outfile.write(line) header_over = true
usage :$ python so.py file1 file2 file3
input:
file1:
#header file1 foo bar
file2:
#header file2 spam eggs
file3:
#header file3 python file
output:
#header file1 foo bar spam eggs python file
Comments
Post a Comment