Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 连接多个文件删除头行_Python_Unix_Awk - Fatal编程技术网

Python 连接多个文件删除头行

Python 连接多个文件删除头行,python,unix,awk,Python,Unix,Awk,连接多个文件的好方法是什么,但删除头行(头行的数量事先不知道),并将第一个文件头行保留为新连接文件的头行 我希望用python来实现这一点,但只要我可以使用subprocess调用unix命令,awk或其他语言也可以工作 注意:标题行都以#开头。使用Python时类似于此: files = ["file1","file2","file3"] with open("output_file","w") as outfile: with open(files[0]) as f1:

连接多个文件的好方法是什么,但删除头行(头行的数量事先不知道),并将第一个文件头行保留为新连接文件的头行

我希望用python来实现这一点,但只要我可以使用subprocess调用unix命令,awk或其他语言也可以工作


注意:标题行都以#开头。

使用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)
您也可以在此处使用该模块:

此模块实现了一个帮助器类和函数,用于快速编写 循环标准输入或文件列表

用法:
$python so.py file1 file2 file3

输入:

#header file1
foo
bar

spam
eggs

python
file
文件1:

#header file1
foo
bar
文件2:

#header file2
spam
eggs
文件3:

#header file3
python
file
输出:

#header file1
foo
bar

spam
eggs

python
file
试试这个:

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
#header2
body2
file2.txt

#header2
body2
#header2
body2
result.txt

body2body

使用GNU awk

awk '
    ARGIND == 1 { print; next } 
    /^[[:space:]]*#/ { next }
    { print }
' *.txt

您可以调用一个shell管道,将
shell=True
传递到
subprocess.Popen

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,在上面的示例中)

(cat file1; sed '/^#/d' file2 file3 file4) > newFile

另一个
awk
版本:

awk '!flag && /#/ { print; flag=1; next } flag && /#/ { next } 1' f1 f2 f3

请发布一些示例(例如,文件1和文件2各有标题)。@AnsgarWiechers文件标题行以
#
开头。是的,我刚刚注意到。对不起,噪音太大。您知道模式
'a'
允许在文件末尾写入吗?我这样问是因为如果你不想让第一个文件保持不变,你可以在第一个文件中一个接一个地写所有其他文件。其中一个文件可能很大吗?文件的处理取决于文件是否可以在RAM中一块轻松地完全读取。谢谢!这会很快吗?根据我的经验,有时python需要很长时间来读取长文件。您认为有更快的选项吗?或者这应该可以吗?除了标题行之外,文件中还有其他可以以“#”开头的行吗?@Dnaiel我已经使用
fileinput
模块添加了另一个解决方案。您的正则表达式需要是
/^[:space:]*#/
——那些命名字符必须放在括号内。@Birei
argid
仅为
gawk