0

There are lots of posts about how to search and replace a single column of a file, using \%Nc syntax. I want extend this syntax to replace multiple columns in all lines that meet some criterion. For example,

PWITHDRAWAL BY EFTPOS 0240331 BWS
PEFTPOS DEBIT 0000409 GROUNDS
PEFTPOS DEBIT 0074959 ASQ INTE
PWITHDRAWAL BY EFTPOS 0510811 COLES
PWITHDRAWAL BY EFTPOS 0043699 XIAN

I need to modify columns 23-29 (the columns with numbers) on each line containing WITHDRAWAL BY EFTPOS. Obviously there are lots of ad hoc ways to do this. For example, g/PWITHDRAWAL BY EFT/s/\%23c/X will change column 23 in the three selected lines to X. I could repeat the command for each column that needs changing, but there's presumably something like g/BY EFT/s/\%23c-\%29c/X/ that would do it in a single command.

Could anybody advise please?

1 Answer 1

2

there's presumably something like g/BY EFT/s/\%23c-\%29c/X/ that would do it in a single command.

23-29 is the same as 7 columns/chars starting from 23.

g/WITHDRAWAL BY EFT/s/\%23c.\{7\}/XXXXXXX

If it's guaranteed to be the first 7-digit number in the string "WITHDRAWAL" you can change it to:

g/WITHDRAWAL BY EFT/s/\d\{7\}/XXXXXXX
2
  • 1
    Exactly what I was hoping for, thanks very much
    – Leo Simon
    Mar 11, 2020 at 22:04
  • 2
    could also use something like this: /\%>22c.\%<30c to restrict matching from column 23 to 29. Mar 12, 2020 at 7:27

Your Answer

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

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