Wednesday, May 13, 2020

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

On Thu, May 14, 2020 at 5:38 AM boB Stepp <robertvstepp@gmail.com> wrote:
>
> 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

I would do that with a macro, as follows (starting from Normal mode): (untested)
0. Go to top of file:
gg
1. Start recording into register q:
qq
2. Search for "pobox" as a full word starting in column 1:
/^pobox\><CR>
(where ^ is a real spacing circumflex and <CR> means "Hit Enter")
3. Go to previous line
k
4. Visually mark characterwise until but not including "addr"
v/.\zeaddr
5. Yank the current visual selection (cursor goes back to column 1)
y
6. Go to next line
j
7. Put before
P
8. End recording
q
9. Repeat what we just recorded until we cannot:
9999@q

(Use a higher count if you have more than 10,000 sections in the file.)


Best regards,
Tony.

--
--
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/CAJkCKXtwrzjpy7nDFvPM7FW5BqoRb2ku7sah5CJuhr2Fsk5eDA%40mail.gmail.com.

No comments: