How to put columns of two files into one file (side by side)? 
Author Message
 How to put columns of two files into one file (side by side)?

Hi guys,
   Suppose I have two data files (a.dat and b.dat) each has two
columns. Now I need to take the first column of a.dat and the second
column of b.dat, then put them (side by side) into a third file c.dat.
How do I do that?
   Thanks a lot!

Pengcheng Song
Dept. of Physics
Brandeis Univ.



Mon, 02 May 2005 05:17:00 GMT  
 How to put columns of two files into one file (side by side)?

Quote:

> Hi guys,
>    Suppose I have two data files (a.dat and b.dat) each has two
> columns. Now I need to take the first column of a.dat and the second
> column of b.dat, then put them (side by side) into a third file c.dat.
> How do I do that?
>    Thanks a lot!

paste a.dat b.dat | awk '{print $1, $4}' > c.dat
or you could use cut instead of awk.

John.



Mon, 02 May 2005 05:25:47 GMT  
 How to put columns of two files into one file (side by side)?

Quote:



>> Hi guys,
>>    Suppose I have two data files (a.dat and b.dat) each has two
>> columns. Now I need to take the first column of a.dat and the second
>> column of b.dat, then put them (side by side) into a third file c.dat.
>> How do I do that?
>>    Thanks a lot!

>paste a.dat b.dat | awk '{print $1, $4}' > c.dat
>or you could use cut instead of awk.

With just awk:

awk 'NR=FNR {a[NR]= $1} NR!=FNR {print a[FNR], $2}' a.dat b.dat > c.dat

Chuck Demas

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Mon, 02 May 2005 05:47:49 GMT  
 How to put columns of two files into one file (side by side)?

Quote:




> >> Hi guys,
> >>    Suppose I have two data files (a.dat and b.dat) each has two
> >> columns. Now I need to take the first column of a.dat and the second
> >> column of b.dat, then put them (side by side) into a third file c.dat.
> >> How do I do that?
> >>    Thanks a lot!

> >paste a.dat b.dat | awk '{print $1, $4}' > c.dat
> >or you could use cut instead of awk.

> With just awk:

> awk 'NR=FNR {a[NR]= $1} NR!=FNR {print a[FNR], $2}' a.dat b.dat > c.dat

> Chuck Demas

> --
>   Eat Healthy    |   _ _   | Nothing would be done at all,

>   Die Anyway     |    v    | That no one could find fault with it.


Why would you want to assign NR equal to FNR every time?

You might consider avoiding the extra comparison with the "next" statement,
and fixing the accidental assignment:

    awk 'NR == FNR {a[NR] = $1; next} {print a[FNR], $2}' a.dat b.dat >
c.dat

If the data is quite large, consider not stashing it in an array.

    awk '{x = $1; getline < "b.dat"; print x, $2}' a.dat > c.dat

- Dan



Mon, 02 May 2005 14:45:26 GMT  
 How to put columns of two files into one file (side by side)?

Quote:







>> >> Hi guys,
>> >>    Suppose I have two data files (a.dat and b.dat) each has two
>> >> columns. Now I need to take the first column of a.dat and the second
>> >> column of b.dat, then put them (side by side) into a third file c.dat.
>> >> How do I do that?
>> >>    Thanks a lot!

>> >paste a.dat b.dat | awk '{print $1, $4}' > c.dat
>> >or you could use cut instead of awk.

>> With just awk:

>> awk 'NR=FNR {a[NR]= $1} NR!=FNR {print a[FNR], $2}' a.dat b.dat > c.dat

>Why would you want to assign NR equal to FNR every time?

A typo.  :-)

Quote:

>You might consider avoiding the extra comparison with the "next" statement,
>and fixing the accidental assignment:

>    awk 'NR == FNR {a[NR] = $1; next} {print a[FNR], $2}' a.dat b.dat >
>c.dat

I originally wrote it that way, but thought that it was clearer as
to what was happening with the second comparison.

Chuck Demas

Quote:

>If the data is quite large, consider not stashing it in an array.

>    awk '{x = $1; getline < "b.dat"; print x, $2}' a.dat > c.dat

>- Dan

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Tue, 03 May 2005 04:52:34 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Two text widgets side by side?

2. merging 2 columns from two files in one file

3. Lines Side by Side?

4. Class side vs Instance Side??

5. smalltalk - java side by side

6. smalltalk - java side by side

7. Printing group footers side-by-side

8. Report details side by side

9. New MVS REXX Utility: Compare partitioned datasets side-by-side - pdsmatch.zip (1/1)

10. client side or server side scripting- new web user

11. Is it possible to concatenate two text files side by side?

12. append the two sets of data side by side at different times using"write to spreadsheet file vi"

 

 
Powered by phpBB® Forum Software