You are not logged in.

#1 2012-01-04 07:03:43

awayand
Member
Registered: 2009-09-25
Posts: 398

[SOLVED] merging two text files

I have one text file with the following content:

a
b
c
d

I have another text file with the following content:

A
B
C
D

How can I merge these two, such that the result is:

aA
bB
cC
dD

I could just open up excel and copy-paste, but that seems inelegant. I have both files open using gvim, but I am stumped as to how to go about this from there...

Any pointers would be highly appreciated!

Last edited by awayand (2012-01-04 07:20:35)

Offline

#2 2012-01-04 07:13:14

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,334

Re: [SOLVED] merging two text files

man join


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#3 2012-01-04 07:14:20

skunktrader
Member
From: Brisbane, Australia
Registered: 2010-02-14
Posts: 1,584

Re: [SOLVED] merging two text files

man join

edit: too slow wink

Last edited by skunktrader (2012-01-04 07:14:40)

Offline

#4 2012-01-04 07:20:18

awayand
Member
Registered: 2009-09-25
Posts: 398

Re: [SOLVED] merging two text files

sweet! thanks!

Offline

#5 2012-01-04 07:29:56

headkase
Member
Registered: 2011-12-06
Posts: 1,983

Re: [SOLVED] merging two text files

In Python (named combine.py):

import sys


file1 = sys.argv[1]
file2 = sys.argv[2]
outfile = sys.argv[3]

f1 = open(file1,'r')
f2 = open(file2,'r')

lines1 = f1.readlines()
lines2 = f2.readlines()

f1.close()
f2.close()

outlines = zip(lines1, lines2)

of = open(outfile,'w')
for line in outlines:
    of.write((line[0].strip('\n'))+(line[1].strip('\n'))+"\n")
of.close()

Usage:

python2 combine.py inputfile1 inputfile2 outputfile

Offline

#6 2012-01-04 20:28:02

zorro
Member
Registered: 2011-11-18
Posts: 47

Re: [SOLVED] merging two text files

I would have suggested

paste -d# file_1 file_2 | sed s/#//

Offline

#7 2012-01-04 20:49:00

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED] merging two text files

zorro wrote:

I would have suggested

paste -d# file_1 file_2 | sed s/#//
paste -d "" file_1 file_2

is enough.

Offline

Board footer

Powered by FluxBB