0

In my source code, it has the following for example:

menu = add_menu_item ((char **)HW_KIND, 21);
.
.
if(get_idx_by_str ((char **)HW_KIND, prev->column_section, 21, 0) < get_idx_by_str ((char **)HW_KIND, curr->column_section, 21, 0))
.
.
fprintf(fp, "# %d floor\n%d@%d\n\n", i, get_idx_by_str ((char **)HW_KIND, section->column_section, 21, 0), stop->stop_landing);
.
.
add_text_to_gtk_text_buffer (text_buffer, iter, NULL, -99, -99, "??????????Ե -- %s\n", HW_KIND[steel_col_data.sec_idx]);

I wanted to find all the sentences having exactly key HW_KIND then also 21 and then change 21 to 20. For example, the first three sentences above match the searching condition but the last one above doesn't.

If in a terminal in Linux, grep "\<HW_KIND\>" foo.c | grep 21 will find all occurrences. How to define the substitute command in Vi editor?

Thank you.

2 Answers 2

0

You can use the following regex:

:%s/\(.\{-}HW_KIND.\{-}\)21\(.\{-}\)/\120\2/gc

With c option, you can confirm each replacement. Try the above regex and watch it replaces the text as you want. It will replace 4 matches.

\( and \) for capturing group, and \{-} for non-greeding match.

8
  • Is it possible to know the occurrence of all 21 meeting the searching condition mentioned above not using s commander in Vi? Or use commander in Linux terminal? The grep commander I used in example only shows how many lines having the searching object.
    – warem
    Nov 24, 2012 at 1:06
  • You can just search with /pattern. If you just find matching pattern, you don't need capturing group, so /.\{-}HW_KIND.\{-}21.\{-} would be sufficient.
    – ntalbs
    Nov 24, 2012 at 14:35
  • I know / in Vi for searching. I want to know the total number of pattern. If using s, it will report how many substitutions occur. But I don't want to do the real substitution and want to know the total. Maybe it has nothing to do with this question. I am not sure if I should raise up a new question.
    – warem
    Nov 24, 2012 at 14:55
  • 1
    Perhaps you want to check the following URL: vim.wikia.com/wiki/Count_number_of_matches_of_a_pattern
    – ntalbs
    Nov 24, 2012 at 14:58
  • 1
    When I test :%s/\(.\{-}HW_KIND.\{-}\)21\(.\{-}\)/\120\2/gn to your example, vim says that 4 matches on 3 lines, which is correct. I wonder if you didn't miss the g option. If it still doesn't work, can I access your file and test it directly?
    – ntalbs
    Nov 25, 2012 at 0:37
0

You have to search for both HW_KIND and 21, while keeping all that's in between. For this, you define a group using braces \( and \) in the search text, and then use this group in the substitute with \1.

:%s/\(HW_KIND.*\)21/\120/

Such substitution can also be made on command line using sed.

1
  • If there are more than one HW_KIND...21 in one line, it only substitutes the last 21.
    – warem
    Nov 24, 2012 at 0:42

Your Answer

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

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