DKangeyan
DKangeyan

Reputation: 543

merging data from two files to make a new data set

I have a file that contains a line with this script (((A:__ ,B:__ ):__ ,C:__ ):__ ,D:__ )

and I have six other text files with each containing 500 random numbers. I need to add those random numbers to the blanks in the first file.

I.e. If t1, t2, t3, t4, t5, t6 are the six files they have numbers such as

t1   t2   t3   t4   t5   t6
2    32   34   213  23   54
3    34   34   67   56   56 
5    45   78   78   89   32 
5    23   45   45   67   78
...  ...  ...  ...  ...  ...

After merging these together I should get a results like

(((A:2,B:32):34,C:213):23,D:54)
(((A:3,B:34):34,C:67):56,D:56)

etc.

I have tried paste function and loops to create this alignment but they don't put the text in the right spot. How do I make it work? I am doing this in Linux.

Upvotes: 0

Views: 87

Answers (2)

Barmar
Barmar

Reputation: 781741

I think this will do it:

paste t1 t2 t3 t4 t5 t6 |
    awk 'BEGIN { getline < "template"; gsub("__", "%f"); format = $0 }
         { printf(format"\n", $1, $2, $3, $4, $5, $6); }'

paste merges the 6 input files together. The first line of the awk script reads the first file (replace template with the filename) and changes each __ to %s, so that it an be used as a format string with printf.

Upvotes: 2

Sniggerfardimungus
Sniggerfardimungus

Reputation: 11782

A solution using bash file descriptors that worked for me... I open a file descriptor for each text file (you have to start with FD 3, since 0-2 are taken by stdin, stdout, stderr.) Then, as long as there is data in t1, I keep reading from t2-t6 and just print the output in the desired format.

#!/bin/bash    
exec 3<> t1.txt    
exec 4<> t2.txt    
exec 5<> t3.txt    
exec 6<> t4.txt    
exec 7<> t5.txt    
exec 8<> t6.txt    
while read one <&3     
  do    
    read two <&4    
    read three <&5    
    read four <&6    
    read five <&7    
    read six <&8    
    echo "(((A:$one,B:$two):$three,C:$four):$five,D:$six)"    
  done    

Upvotes: 1

Related Questions