Wednesday, May 13, 2020

How to search a match on a line and then make a change on the next line?

I am relatively new to using regex pattern matching techniques to search for and
replace text. Today I have been trying to get a better mastery of these
techniques. But I am currently stumped on the following text search and
replace:

# Home address fields:
# field address STANDARD FIELD -- CANNOT EDIT!
# field address2 STANDARD FIELD -- CANNOT EDIT!
pobox = "PO Box", string
# field city STANDARD FIELD -- CANNOT EDIT!
# field state STANDARD FIELD -- CANNOT EDIT!
# field zip STANDARD FIELD -- CANNOT EDIT!
# field country STANDARD FIELD -- CANNOT EDIT!

# Work address fields:
field work_address = Address, string
field work_address2 = Address2, string
pobox = "PO Box", string
field work_city = City, string
field work_state = State, string
field work_zip = Zipcode, string
field work_country = Country, string

# Other address fields:
field other_address = Address, string
field other_address2 = Address2, string
pobox = "PO Box", string
field other_city = City, string
field other_state = State, string
field other_zip = Zipcode, string
field other_country = Country, string

In the above I want to change the lines starting with "pobox" to match the
format of the line above it, e.g., "field pobox", "field work_pobox" and
"field other_pobox", respectively. My best effort so far to do this is:

:g/\(field [[:alnum:]_]*\)address2/+s/pobox/\1pobox/

My current understanding (flawed though it is) feels that this command
should do the trick, but it doesn't; instead, it highlights the three
instances of "pobox" and says that it has made three changes without
visibly changing anything. It is as if "\1" is not storing anything.
What I _think_ the above command is doing is:

1) Search globally and find each line that has the string "field <one or
more alphanumeric characters or underline>address2".

2) Store the above strings in "\1" which I should be able to use in the
replacement string to follow.

3) Advance to the next line with "+".

4) Search on this line for the string "pobox".

5) Replace that instance with "<contents of "\1">pobox".

What am I misunderstanding?

--
Wishing you only the best,

boB Stepp

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200514033806.GD7019%40Dream-Machine1.

No comments: