Search code examples
regexlinuxvimviregular-language

Replace every occurrence of the regular expression matching with a particular in VI editor?


suppose I have a text file as follows.

create table "kevin".tb1 {
col1,
col2
}
create table "jhone".tb2 {
col1,
col2
}
create table "jake".tb3 {
col1,
col2

}

I need to obtain that text file as follows by replacing every table owner name occurrences replace witha same name called "informix".

out put should be like

create table "informix".tb1 {
col1,
col2
}
create table "informix".tb2 {
col1,
col2
}
create table "informix".tb3 {
col1,
col2
}

in vi editor ,

:%s/"kevin"/"informix"/g

I can replace them individually but I need to do all of them at once.


Solution

  • %s/\(create table\) "\i\+"/\1 "informix"/
    

    Explanation:

    % — run through every line in the file
    s/ — search and replace
    \(create table\) — match the text and store it in the backreference 1
    "\i\+" — match any number (more than 1) of identifier characters inside double quotes
    \1 "informix" — replace what is found with backreference 1 (text "create table"), a space and text "informix"