You are not logged in.

#1 2011-08-23 17:22:14

darkbeanies
Member
Registered: 2009-01-14
Posts: 142

How to merge two files line by line?

Hello, normally I use this:

awk 'NR==FNR{a[FNR]=$0;next} {print a[FNR],$0}' file1 file2 > file3

so if i joined a file containing:

aaa
bbb
ccc

and another file containing:

ddd
eee
fff

I get:

aaa ddd
bbb eee
ccc fff

But right now I need to get:

aaaddd
bbbeee
cccfff

Pray help me, I have been writing my crappy script for about 10 hours now and it still doesn't do what i want...

Last edited by darkbeanies (2011-08-23 17:22:38)

Offline

#2 2011-08-23 17:34:41

carlocci
Member
From: Padova - Italy
Registered: 2008-02-12
Posts: 368

Re: How to merge two files line by line?

paste --delimiters="" a.txt b.txt

or you can remove the comma from your awk print statement

Last edited by carlocci (2011-08-23 17:39:57)

Offline

#3 2011-08-23 17:39:59

darkbeanies
Member
Registered: 2009-01-14
Posts: 142

Re: How to merge two files line by line?

Looking good!  Many many thanks!

I really should learn awk i think...

Last edited by darkbeanies (2011-08-23 17:40:59)

Offline

#4 2011-08-23 17:43:54

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: How to merge two files line by line?

If you wanted to do it with awk, your solution works, just get rid of the comma in the print statement:

awk 'NR==FNR{a[FNR]=$0;next} {print a[FNR] $0}' file1 file2
aaabbb
aaabbb
aaabbb

bah... carlocci beat me to it.

Last edited by falconindy (2011-08-23 17:44:07)

Offline

#5 2011-08-23 19:15:10

darkbeanies
Member
Registered: 2009-01-14
Posts: 142

Re: How to merge two files line by line?

falconindy wrote:

bah... carlocci beat me to it.

I can give you some more jolly puzzles later if you'd like...

Offline

#6 2011-08-23 22:03:28

harryNID
Member
From: P3X-1971
Registered: 2009-06-12
Posts: 117

Re: How to merge two files line by line?

Another meaningless way:

join <(nl file_a) <(nl file_b) | mawk '{print $2$3}'

prints

aaaddd
bbbeee
cccfff

or

join <(nl file_a) <(nl file_b) | mawk '{print $2,$3}'

prints

aaa ddd
bbb eee
ccc fff

Using "nl" to make each line common so as to join them.


In solving a problem of this sort, the grand thing is to be able to reason backward. That is a very useful accomplishment, and a very easy one, but people do not practice it much. In the everyday affairs of life it is more useful to reason forward, and so the other comes to be neglected. There are fifty who can reason synthetically for one who can reason analytically.  --Sherlock Holmes

Offline

#7 2011-08-23 22:54:43

darkbeanies
Member
Registered: 2009-01-14
Posts: 142

Re: How to merge two files line by line?

Sage advice from Holmes as usual.

Offline

Board footer

Powered by FluxBB