有什么好的方法可以连接几个文件,但是删除标题行(标题行数量未知),并将第一个文件标题行保留为新连接文件的标题?
我想在Python中实现这一点,但awk或其他语言也可以,只要我可以使用子进程调用Unix命令。
注意:所有标题行均以#开头。
(cat file1; sed '/^#/d' file2 file3 file4) > newFile
使用Python编写类似以下代码:
files = ["file1","file2","file3"]
with open("output_file","w") as outfile:
with open(files[0]) as f1:
for line in f1: #keep the header from file1
outfile.write(line)
for x in files[1:]:
with open(x) as f1:
for line in f1:
if not line.startswith("#"):
outfile.write(line)
fileinput
模块:
该模块实现了一个辅助类和函数,以便快速地在标准输入或文件列表上执行循环。
import fileinput
header_over = False
with open("out_file","w") as outfile:
for line in fileinput.input():
if line.startswith("#") and not header_over:
outfile.write(line)
elif not line.startswith("#"):
outfile.write(line)
header_over = True
使用方法:$ python so.py 文件1 文件2 文件3
输入:
文件1:
#header file1
foo
bar
文件2:
#header file2
spam
eggs
文件3:
#header file3
python
file
输出:
#header file1
foo
bar
spam
eggs
python
file
fileinput
模块的解决方案。 - Ashwini Chaudhary试试这个:
def combine(*files):
with open("result.txt","w+") as result:
for i in files:
with open(i,"r+") as f:
for line in f:
if not line.strip().startswith("#"):
result.write(line.rstrip())
combine("file1.txt","file2.txt")
file1.txt
:
#header2
body2
file2.txt
:
#header2
body2
result.txt
body2body
awk '
ARGIND == 1 { print; next }
/^[[:space:]]*#/ { next }
{ print }
' *.txt
/^[[:space:]]*#/
-- 这些命名字符必须放在括号内。 - glenn jackmanARGIND
只在 gawk
中存在。 - jaypal singhshell=True
传递给subprocess.Popen
来调用一个 shell 管道。cat f.1 ; grep -v -h '^#' f.2 f.3 f.4 f.5
import sys, subprocess
p = subprocess.Popen('''cat f.1 ; grep -v -h '^#' f.2 f.3 f.4 f.5''', shell=True,
stdout=sys.stdout)
p.wait()
#!/usr/bin/env python
import sys
for i in range(1, len(sys.argv)):
for line in open(sys.argv[i], "r"):
if i == 1 or not line.startswith("#"):
print line.rstrip('\n')
$ ./combine.py foo.txt bar.txt baz.txt > result.txt
头部信息将从参数列表的第一个文件中获取(在上面的示例中为foo.txt
)。
另一个 awk
版本:
awk '!flag && /#/ { print; flag=1; next } flag && /#/ { next } 1' f1 f2 f3
#
开始。 - user1786283'a'
允许在文件末尾写入吗?我问你这个问题是因为如果你不想保留第一个文件的原样,你可以将所有其他文件依次写入第一个文件中。 - eyquem