3

I'm trying to figure out how to move a block of text from one file to a specific point in another. I have a large amount of files that looks like this:

H                 -9.92247800    1.33807800   -0.69208300  
S                 -9.74392800    0.01073000   -0.55448800   
C                 -7.98603700    0.04294200   -0.19355700   
C                 -7.45325900   -1.23715800    0.02112600
...  

And I want to move the columns of numbers to files that looks like this before:

...
0 1 0 1 0 1

H                0  H
S                0  H
C                0  H
C                0  H
...

And so should like this after:

...
0 1 0 1 0 1

H                0 -9.92247800    1.33807800   -0.69208300 H
S                0 -9.74392800    0.01073000   -0.55448800 H
C                0 -7.98603700    0.04294200   -0.19355700 H
C                0 -7.45325900   -1.23715800    0.02112600 H
...

Is there anyway to automate this for a large number of files? I'm aware that I can select a block, copy it, and paste it into the other file manually, but I have too many files for this to be feasible.

The number blocks always begins and ends at the same location (line and column) and the place I want to paste to always is at the same line and column.

  • 2
    Do you want to replace the existing H/S/C lines - or merge the colums? Can you provide an actual before-and-after example? – steeldriver Jun 11 '17 at 20:24
  • @steeldriver I would like to merge the columns. Basically, inserting three columns of numbers (from file1) in between 0 and H (in file2). – Tyberius Jun 11 '17 at 20:49
  • Are the H S C C occurring as shown in both files? Basically, you need to provide more of your data. – user218374 Jun 12 '17 at 11:01
1

Don't know if you are up for Python, and there is likely a pithy way using a different language, but Python can do this with something like:

Code:

# describe where the text block is located
f1_start = 2, 18
f1_size = 4, 40
f2_start = 4, 19

# open all three files
with open('file1', 'rU') as f1, open('file2', 'rU') as f2, open('file3', 'w') as f3:

    # skip some lines in file1
    for _ in range(f1_start[0] - 1):
        f1.readline()

    # write first block of file2 to file3
    for _ in range(f2_start[0] - 1):
        f3.write(f2.readline())

    # read from and merge lines in file1 and file 2
    for _ in range(f1_size[0]):
        l1 = f1.readline()[f1_start[1]:f1_start[1] + f1_size[1]].rstrip()
        l2 = f2.readline()
        l3 = l2[:f2_start[1]] + l1 + l2[f2_start[1]:]
        f3.write(l3)

    # write remaining lines from file2 to file3
    while True:
        l2 = f2.read()
        if not l2:
            break
        f3.write(l2)

Result:

...
0 1 0 1 0 1

H                0 -9.92247800    1.33807800   -0.69208300 H
S                0 -9.74392800    0.01073000   -0.55448800 H
C                0 -7.98603700    0.04294200   -0.19355700 H
C                0 -7.45325900   -1.23715800    0.02112600 H
...
  • Ideally I would like to find something pithier, since I think the functionality exists within Linux or Vi to do something like this. But I like this approach and will have to give it a try. Thanks! – Tyberius Jun 11 '17 at 21:19
  • Many (most?) Linux distributions have Python installed. – Stephen Rauch Jun 11 '17 at 21:20
  • I'm pretty new to Linux and didn't realize. It looks like mine does so I will have to try this out. – Tyberius Jun 11 '17 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.