Replace specific columns in one file with columns in another file

HELLO! This is my first post here! By the way, I think it is great that people do this.

My question:

I have two files, one is a .dilm and one is a .txt. It is my understanding that the .dilm file can be treated as a .txt file. I wrote another program where I was able to manipulate it as if it was a text file. Anyways...

I want to manipulate file1 so that certain columns in it (delineated by quotes-- columns $2 $6 and &16, for example) are replaced by columns in file2 (columns $2 $3 and $1 [in that order]). The things I am replacing in file1 and what will replace them from file2 are all numbers.

I don't mind if a new file is created or the original file (file1) is simply altered.
I was thinking of using awk but I really have no idea. I usually program in matlab but since I am trying to manipulate a file, awk seems better suited.

Thank you so much for your help!

---------- Post updated at 10:41 AM ---------- Previous update was at 10:16 AM ----------

Actually, the output file would have to be a .dilm file. thanks.

Not sure about the format of these .dilm files, but here is an awk script that works on space/tab seperated files.
If you can post an example of the .dilm files, I'll try and update it to work with them.

$ cat file1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 
$ cat file2
one two three four five six seven eight nine ten eleven twelve thirteen fourteen
ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE TEN ELEVEN TWELVE THIRTEEN FOURTEEN
 
$ cat mehdib.awk
awk -v "D=$1" -v "S=$2" '
   BEGIN {
       split(D,DA, ",");
       split(S,SA, ",");
   }
   FNR==NR {
      for(P in SA) A[NR]=A[NR]$SA[P]FS; next }
   { i=1; 
     split(A[FNR], NEW, FS);
     for(P in DA) $DA[P]=NEW[i++] }
   1 ' file2 file1
 
$ ./mehdib.awk 2,6,16 2,3,1
1 two 3 4 5 three 7 8 9 10 11 12 13 14 15 one 17 18 19 20
1 TWO 3 4 5 THREE 7 8 9 10 11 12 13 14 15 ONE 17 18 19 20

Hey Chubler_XL that looks nice actually. Thanks so much! I'm not at my computer right now but I'll try it out first thing tomorrow.

It is easiest to separate segments in.dilm files by using quotes ( like "\"" ) as the separator (instead of tabs or spaces). Here is an example of what .dilm files look like. I am trying to replace just the numbers in the file. The numbers are always surrounded by quotes which is why I was thinking quotes would be best. (There are not spaces in-between the numbers and the quotes.)

< Landmark "something" "y="0.999999" something" ... >
< Landmark "something" "y="0.999999" something" ... >
< Landmark "something" "y="0.999999" something" ... >
...

The file then will be called by another program that interprets files of this kind. Only the numbers must change.

Thanks again. :slight_smile:

Your welcome, just use a -F'"' argument to awk and you'll be right to go.