Merging multiple tsv files
1
0
Entering edit mode
3.2 years ago
Nobody ▴ 30

Hi!

i have a multiple tsv files, do you have any methods to merge them in a single file with only header ?

genome sequencing next-gen • 6.7k views
ADD COMMENT
1
Entering edit mode
head -n 1 file1.txt > merged.txt
for F in file*.txt ; do tail -n +2 ${F} >> merged.txt ; done

or if your header is at the top after a sort

cat file*.txt |sort | uniq > merged.txt

ADD REPLY
0
Entering edit mode

i want to do it with python

ADD REPLY
1
Entering edit mode

use tsv-append and a simple header aware concatenation is (copy/pasted):

$ tsv-append -H file1.tsv file2.tsv file3.tsv

All the available options can be read from here: https://github.com/eBay/tsv-utils/blob/master/docs/tool_reference/tsv-append.md

ADD REPLY
0
Entering edit mode

it works with python ?

ADD REPLY
0
Entering edit mode

hard to know what exactly you have and what you want, you can use cat to concatenate files

ADD REPLY
0
Entering edit mode

if you need to merged them (column-wise , we don't have enough info to figure that out), have a look at the linux paste command

ADD REPLY
2
Entering edit mode
3.2 years ago
tothepoint ▴ 800

I think this will work for you. Python code to merge tsv files in to one with only header.

#merge_tsv_files
from glob import glob

filename = 'merge.tsv'

with open(filename, 'a') as singleFile:
    first_tsv = True
    for tsv in glob('*.tsv'):
        if tsv == filename:
            pass
        else:
            header = True
            for line in open(tsv, 'r'):
                if first_tsv and header:
                    singleFile.write(line)
                    first_tsv = False
                    header = False
                elif header:
                    header = False
                else:
                    singleFile.write(line)
    singleFile.close()
ADD COMMENT
0
Entering edit mode

thank you so much this is what i wanted to do exactly

ADD REPLY

Login before adding your answer.

Traffic: 2180 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6