Question: Merging multiple tsv files
0
gravatar for Sar
6 weeks ago by
Sar0
Sar0 wrote:

Hi!

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

sequencing next-gen genome • 232 views
ADD COMMENTlink modified 6 weeks ago by devarora320 • written 6 weeks ago by Sar0
1
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 REPLYlink written 6 weeks ago by Pierre Lindenbaum134k

i want to do it with python

ADD REPLYlink written 6 weeks ago by Sar0
1

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 REPLYlink written 6 weeks ago by cpad011215k

it works with python ?

ADD REPLYlink written 6 weeks ago by Sar0

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

ADD REPLYlink written 6 weeks ago by JC12k

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 REPLYlink written 6 weeks ago by lieven.sterck10k
1
gravatar for devarora
6 weeks ago by
devarora320
SouthKorea
devarora320 wrote:

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 COMMENTlink written 6 weeks ago by devarora320

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

ADD REPLYlink written 6 weeks ago by Sar0
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1647 users visited in the last hour
_