1

I need to replace the first number that comes after AAA, with 0.05 So this:

AAA,1.25,2020-08-30
BB,0.1,2020-08-30
CCC,1.6,2020-08-30
DDDD,1.5,2020-08-31
EE,1.75,2020-08-31
FFF,0.05,2020-08-31
AAA,0.25,2021-01-31
GG,0.1,2021-01-31

would become

AAA,0.05,2020-08-30
BB,0.1,2020-08-30
CCC,1.6,2020-08-30
DDDD,1.5,2020-08-31
EE,1.75,2020-08-31
FFF,0.05,2020-08-31
AAA,0.05,2021-01-31
GG,0.1,2021-01-31

How do I do this?

1
3

This will do that for you:

:%s/\v^AAA,([^,]+)/AAA,0.05/

Explanation:

  • %s/{pattern}/{string}/: for each line in the whole file (%) substitute a match of {pattern} with {string} (:h :s)
  • \v: one of vim's four regex modes (:h /magic)
  • ^AAA,([^,]+): match lines starting with AAA, followed by one or more characters that are not a comma
  • AAA,0.05: the string to replace the match with
3
  • You can also use :%s/^AAA,\zs[^,]\+/0.05/, which avoids the need for grouping in the match and repeating the beginning of the match in the replacement. – filbranden Apr 3 at 4:45
  • 1
    @filbranden Thanks, I didn't know about \zs and \ze. – Andrew Ho-Lee Apr 3 at 8:48
  • 1
    Also :g/^AAA/norm! f,lct,0.05 – D. Ben Knoble Apr 3 at 13:52

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.