假设我有一个如下的文本文件。
create table "kevin".tb1 {
col1,
col2
}
create table "jhone".tb2 {
col1,
col2
}
create table "jake".tb3 {
col1,
col2
}
我需要通过将出现的每个表所有者名称替换为名为“informix”的相同名称来获取该文本文件,如下所示。
输出应该像
create table "informix".tb1 {
col1,
col2
}
create table "informix".tb2 {
col1,
col2
}
create table "informix".tb3 {
col1,
col2
}
在 vi 编辑器中,
:%s/"kevin"/"informix"/g
我可以单独替换它们,但我需要一次全部替换。
最佳答案
%s/\(create table\) "\i\+"/\1 "informix"/
说明:
% — 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"
关于regex - 在 VI 编辑器中将每个出现的正则表达式匹配替换为特定的正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53704308/