6

I am trying to increment the numbers in a column of a file by a constant value using:

:s/\d\+/\=submatch(0)+8192

where 8192 is the constant I want to add.

My file has the below structure:

ATOM  32760  MW4 wate 8190       6.520  15.778  26.020  0.00  0.00          Mw4  
ATOM  32761  OW1 wate 8191       6.550  20.128  25.950  0.00  0.00           O  
ATOM  32762  HW2 wate 8191       6.660  21.078  25.990  0.00  0.00           H  
ATOM  32763  HW3 wate 8191       7.440  19.778  25.870  0.00  0.00           H  
ATOM  32764  MW4 wate 8191       6.680  20.208  25.950  0.00  0.00          Mw4  
ATOM  32765  OW1 wate 8192       1.280  16.158  26.060  0.00  0.00           O  
ATOM  32766  HW2 wate 8192       1.350  17.108  26.160  0.00  0.00           H  
ATOM  32767  HW3 wate 8192       1.800  15.958  25.280  0.00  0.00           H  
ATOM  32768  MW4 wate 8192       1.360  16.258  25.970  0.00  0.00          Mw4  
ATOM  32769  OW1 wate    1      17.610   8.049  75.290  0.00  0.00           O  
ATOM  32770  HW2 wate    1      17.860   7.549  76.060  0.00  0.00           H  
ATOM  32771  HW3 wate    1      16.680   8.249  75.420  0.00  0.00           H  
ATOM  32772  MW4 wate    1      17.520   8.009  75.410  0.00  0.00          Mw4  
ATOM  32773  OW1 wate    2      16.200   7.639  85.230  0.00  0.00           O  
ATOM  32774  HW2 wate    2      15.720   8.139  85.890  0.00  0.00           H  
ATOM  32775  HW3 wate    2      17.060   8.049  85.200  0.00  0.00           H  
ATOM  32776  MW4 wate    2      16.250   7.759  85.320  0.00  0.00          Mw4  
ATOM  32777  OW1 wate    3       5.190  10.149  77.870  0.00  0.00           O  
ATOM  32778  HW2 wate    3       6.070  10.009  77.530  0.00  0.00           H  
ATOM  32779  HW3 wate    3       5.020  11.079  77.730  0.00  0.00           H 

At present, my command selects the first integer column which is next to ATOM column and I am visually selecting the values from 1 to end of the file (which is 5th column). Meaning, not able to select the (5th column) by matching patterns since my above command selects only the second column (after ATOM).

However, what I want to do is increment the values starting from 1 to the end of the column with 8192, essentially continuing the numbering from 8192. Could someone suggest what I need to do to achieve this?

3
  • Have you thought about recording a macro to do the increment using the built-in number increment, i.e., 8192<C-a>? Commented Aug 8 at 5:24
  • Yes. Indeed, I have a macro slightly different from what yo have posted. I wanted to do it using string matching. Appreciated your efforts. Many thanks. Commented Aug 8 at 8:27
  • This is a solved question that's not really about programming, and should've been asked at vi.stackexchange.com or superuser.com to begin with. What would be the purpose of reopening it here? Commented Oct 26 at 10:48

2 Answers 2

4

Your original command was almost there, the only tiny detail missing is \%V to constrain the :substitute command to the Visual-block selected area. Like most Ex commands, :substitute works on whole lines by default, even in Visual-block mode.

Let's put a \%V in your original command to make it work on the selected parts only:

:'<,'>s/\%V\d\+/\=submatch(0)+8192/

The respective documentation can be found in :help /\%V. There's also a sentence below :help pattern-delimiter which nicely sums it up:

In Visual block mode, use /%V in the pattern to have the substitute work in the block only. Otherwise it works on whole lines anyway.

This will get you almost there but suffers from the same indentation problems as the macro-based answer. To keep the columns properly indented, you can match and discard the leading whitespace using a capture group. Just make sure the Visual-block selection is four characters wide:

:'<,'>s/\%V\s*\(\d\+\)/\=submatch(1)+8192/
Sign up to request clarification or add additional context in comments.

Comments

3

You should be able increment the values of the 5th column using a macro:

  1. Navigate to the first line you want to perform the action on.
  2. Press qq to start recording the macro in register q (or another register of your choice).
  3. Press ^ to ensure your cursor is at the start of the line.
  4. Press e five times to skip forward to the fifth column (assuming columns contain only one "word").
  5. Press 8192<C-a> to increment the number by 8192.
  6. Press j to move down to the next line.
  7. Press q to end the macro recording.
  8. Run the macro 10 times by pressing 10@q.

Output example

ATOM  32760  MW4 wate 8190       6.520  15.778  26.020  0.00  0.00          Mw4  
ATOM  32761  OW1 wate 8191       6.550  20.128  25.950  0.00  0.00           O  
ATOM  32762  HW2 wate 8191       6.660  21.078  25.990  0.00  0.00           H  
ATOM  32763  HW3 wate 8191       7.440  19.778  25.870  0.00  0.00           H  
ATOM  32764  MW4 wate 8191       6.680  20.208  25.950  0.00  0.00          Mw4  
ATOM  32765  OW1 wate 8192       1.280  16.158  26.060  0.00  0.00           O  
ATOM  32766  HW2 wate 8192       1.350  17.108  26.160  0.00  0.00           H  
ATOM  32767  HW3 wate 8192       1.800  15.958  25.280  0.00  0.00           H  
ATOM  32768  MW4 wate 8192       1.360  16.258  25.970  0.00  0.00          Mw4  
ATOM  32769  OW1 wate    8193      17.610   8.049  75.290  0.00  0.00           O  
ATOM  32770  HW2 wate    8193      17.860   7.549  76.060  0.00  0.00           H  
ATOM  32771  HW3 wate    8193      16.680   8.249  75.420  0.00  0.00           H  
ATOM  32772  MW4 wate    8193      17.520   8.009  75.410  0.00  0.00          Mw4  
ATOM  32773  OW1 wate    8194      16.200   7.639  85.230  0.00  0.00           O  
ATOM  32774  HW2 wate    8194      15.720   8.139  85.890  0.00  0.00           H  
ATOM  32775  HW3 wate    8194      17.060   8.049  85.200  0.00  0.00           H  
ATOM  32776  MW4 wate    8194      16.250   7.759  85.320  0.00  0.00          Mw4  
ATOM  32777  OW1 wate    8195       5.190  10.149  77.870  0.00  0.00           O  
ATOM  32778  HW2 wate    8195       6.070  10.009  77.530  0.00  0.00           H  
ATOM  32779  HW3 wate    8195       5.020  11.079  77.730  0.00  0.00           H 

Note that this misaligns the columns slightly, however, it's pretty easy to record a macro that also fixes alignment. Let me know if you need me to update the answer to do that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.