Replace specific columns in one file with columns in another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace specific columns in one file with columns in another file
# 1  
Old 12-09-2010
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.
# 2  
Old 12-09-2010
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.

Code:
$ 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


Last edited by Chubler_XL; 12-09-2010 at 07:33 PM..
# 3  
Old 12-09-2010
Computer

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. Smilie
# 4  
Old 12-09-2010
Your welcome, just use a -F'"' argument to awk and you'll be right to go.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing a file and pulling out specific columns

Hi, I am having some difficulty pulling out specific columns using awk. I think what I am doing is iterating through the various columns looking for a match and asking awk to print if a match is found. Here are a few lines from my input: NC_015011.2 Gnomon gene 18691 26481 . ... (1 Reply)
Discussion started by: bioBob
1 Replies

2. Shell Programming and Scripting

Need specific columns in a log file as excel.

Hi All... I am in need of few columns from a log file.. in .xls file... below is what i have tried. my log file has 16 colums with " ; " as delimiter, but i need randomn columns 1 2 3 4 5 6 10 11 16 in an excel. I tried to awk the columns with delimiter ; and it worked, below is the log... (5 Replies)
Discussion started by: nanz143
5 Replies

3. Shell Programming and Scripting

Replace text in column1 of a file matching columns of another file

Hi all, I have 2 files: species-names.txt Abaca-bunchy-top-virus ((((Abaca-bunchy-top-virus((Babuvirus((Unassigned((Nanoviridae((Unassigned)))) Abutilon-mosaic-virus ((((Abutilon-mosaic-virus((Begomovirus((Unassigned((Geminiviridae((Unassigned))))... (2 Replies)
Discussion started by: thienxho
2 Replies

4. Shell Programming and Scripting

Transpose whole file and specific columns

Hi, I have a file like this a b c d e f g h i j k l Case1: I want to transpose the whole file Output1 a d g j b e h k c f i l Case2 Transpose a specific column - Say 3rd (6 Replies)
Discussion started by: jacobs.smith
6 Replies

5. Shell Programming and Scripting

Replace columns of one file into another

Hi, I have two files that are different in size (column #'s differ). Each file has the exact same 3 starting columns. File 1 has 240 columns while File 2 has 45 columns. So if the first 3 columns are the same, I want to replace columns 83 to 163 from File 1 with columns 18 to 33 from File... (7 Replies)
Discussion started by: kylle345
7 Replies

6. UNIX for Dummies Questions & Answers

Switch and replace columns in a file

HP-UX I have a fixed length file like this 9921190625797AE2560 20091001US20091001@@NEWSITE @@ 20091013001X X 01NEW00DNA00007081 @@ SPRINGFIELD @@ ... (2 Replies)
Discussion started by: rs1969
2 Replies

7. UNIX for Dummies Questions & Answers

Displaying specific columns in a file

Hi, I'm just wondering how you display a specific set of columns of a specified file in Unix. For example, if you had an AddressBook file that stores the Names, Phone numbers, and Addresses of people the user entered in the following format (the numbers are just to give an idea of what column... (1 Reply)
Discussion started by: logorob
1 Replies

8. Shell Programming and Scripting

Replace specific columns

hi All, Thi sis very urgent. I have large files with pipe delimited. For example: 1.txt 1001024|120|9|-0.0|#| 1001025|120|9|#| 1001026|120|9|#| 1001032|120|2|-0.0|#| 1002026|110|9|#| 1002027|110|9|-0.0|#| 1002028|120|1|1.0|#| I need to replace the 4th filed if it is # by |-| my... (2 Replies)
Discussion started by: jisha
2 Replies

9. Shell Programming and Scripting

How can i replace certain columns in the file

Can i use sed command to replace certain column in the file let say i hav D1254215221542 MANA3DS2OOR454 C1254815221121 MDGA4GH4OOR454 A1254215221522 AFFA4DF4OODS54 S3454815221121 TDTA4GH465R454 I wanted to change only at postition 21 and 22 which is DS,GH,DF and GH i want find that if... (6 Replies)
Discussion started by: mani_um
6 Replies

10. Shell Programming and Scripting

Deleting specific columns from a file

Hi Friends, I want to delete specific columns from a file. Say my file content is as follows: "1","a","ww1",1234" "2","b","wwr3","2222" "3","c","erre","3333" Now i want to delete the column 2 and 4 from this file. That is I want the file content to be: "1","ww1" "2","wwr3"... (11 Replies)
Discussion started by: premar
11 Replies
Login or Register to Ask a Question