It is pleasure to ask you about something I cannot solve myself. I am working with big text files with many similar lines. The format of the files is defined and I want to replace repetitively some characters appearing in some columns of the files. For example I would like to substitute in a file similar to this below, when the last number 3.0e-4 is, from the 5th to the 7th character by aaa, in this case yyy will be substitute by aaa:

ASDEFxxx          3.0E+12ERT          SAA      1.0e-4
ASEEFyyy          4.0E+12EDT          ABA      3.0e-4
ASAEFzzz          5.0E+12ERF          ADA      2.0e-4

In this example I would like to use replace regexp to replace in this case only the second line. The result should be:

ASDEFxxx          3.0E+12ERT          SAA      1.0e-4
ASEEFaaa          4.0E+12EDT          ABA      3.0e-4
ASAEFzzz          5.0E+12ERF          ADA      2.0e-4

There are many lines in the file to be substituted...

Does anybody write the command with replace regexp to do that?

Thanks and regards, Manuel

  • You could write a macro. – Ista Jul 22 '15 at 22:02
  • 2
    (@Ista means keyboard macro, presumably.) – Drew Jul 22 '15 at 23:06
  • Yes, I meant record a keyboard macro. Thanks @Drew – Ista Jul 22 '15 at 23:49
  • Our of curiosity, how many lines is "big"? A keyboard macro won't really scale on the order of 100,000, and another method I'm thinking of won't scale beyond 1,000. (replace-regexp is the fastest method I know from a performance perspective) – PythonNut Jul 23 '15 at 3:55
  • Yes the file is about 500000 lines. I was exactly looking for this replace-regexp command. Thanks!!! – mnlmrn Jul 23 '15 at 14:11

C-x h C-M-% \(.\{5\}\).\{3\}\(.*3.0e-4\) RET \1aaa\2 RET


  • C-x h select the whole file
  • C-M-% run query-replace-regexp (you may want M-x replace-regexp instead)
  • \(.\{5\}\) capture 5 characters into the group \1.
  • .\{3\} match three characters (will discard)
  • \(.*3.0e-4\) match the rest of the line, ensuring 3.0e-4 and capture in \2
| improve this answer | |
  • Hi PythonNut, Many thanks for your answer. That is exactly what I was looking for. Thanks and regards Manuel – mnlmrn Jul 23 '15 at 14:09

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.