Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!convex!news From: tchrist@convex.COM (Tom Christiansen) Newsgroups: comp.unix.questions Subject: Re: How to merge two files in awk?? Message-ID: <1991Jan19.194124.2335@convex.com> Date: 19 Jan 91 19:41:24 GMT References: <3404@d75.UUCP> Sender: news@convex.com (news access account) Reply-To: tchrist@convex.COM (Tom Christiansen) Organization: CONVEX Software Development, Richardson, TX Lines: 76 Nntp-Posting-Host: pixel.convex.com From the keyboard of @xlab1.uucp (): : I am not sure if this question has been asked before... : : Supposing I have two files with three collumns in each. How do : I merge the files and generate a single file with six or more : collumns using shell script? for example if File A has collumns a, c, e : and File B has collumns b, d, f. I want to generate File C : with collumns a,b,c,d,e,f. Also it would be nice to be able to : using the arithematic feature in awk... This originally went also to comp.unix.internals. I sure wouldn't say an awk question is a unix internal. Someone out there may have as paste solution, but I didn't see one. In old, standard awk, it's really quite cumbersome, as you have to read in all the first file, then all the second file. I find this to be a pretty cumbersome solution. #!/bin/awk -f { a[NR] = $1; b[NR] = $2; c[NR] = $3; } END { count = NR/2; for (i = 1; i <= count; i++) { print a[i], a[i+count], b[i], b[i+count], c[i], c[i+count]; } } In gawk (and nawk if you're rich), it's a little easier because you can redirect getilne from a file, effectively reading two lines and writing one line each iteration. #!/usr/gnu/bin/gawk -f BEGIN { for (;;) { if ((getline < ARGV[1]) <= 0) break; a = $1; c = $2; e = $3; if ((getline < ARGV[2]) <= 0) break; b = $1; d = $2; f = $3; print a, b, c, d, e, f; } } It's also pretty easy in perl: #!/usr/bin/perl $[ = 1; $, = " "; $\ = "\n"; # awk emulation open(F1, $ARGV[1]); open(F2, $ARGV[2]); while ( (@a = split(' ',)) && (@b = split(' ', )) ) { print $a[1], $b[1], $a[2], $b[2], $a[3], $b[3]; } Other advantages of perl are: 1) you get better error messages for syntax errors 2) you can symbolically debug your program 3) no limits on lines/fields (gawk is better than nawk at this) 4) can often be made to run faster than awk 5) better usage and i/o failure error messages (i didn't do this here) If you only have awk and not gawk and perl, you should get them, because they are both free and compile on a vast array (list? :-) of platforms. Find them wherever GNUware is stored. : Finally, how do u specify the "rest of the line" in awk?? I'm not really sure what you mean. The whole line is $0. What's the rest of the line? You mean fields past the third one? --tom -- "Hey, did you hear Stallman has replaced /vmunix with /vmunix.el? Now he can finally have the whole O/S built-in to his editor like he always wanted!" --me (Tom Christiansen )