I know how to replace a word with sed. Using
我知道如何用sed替换一个单词。使用
sed -i 's/[old_text]\b/[new_text]/g'
could replace old_text with new_text.
可以用new_text替换old_text。
What I want to do is to replace nchar(10) or nchar (10) or nchar ( 10)... with char(10) in a DB schema.
我要做的是替换nchar(10)或nchar(10)或nchar(10)…在DB模式中使用char(10)。
CREATE TABLE Human
(
ID int,
Name nchar(10) --> should be replace with char(10)
);
The format is quite hard to be determined.
这种格式很难确定。
I don't know how to set the sed regular expression to archive this.
我不知道如何设置sed正则表达式来归档它。
3 个解决方案
#1
2
You can make spaces optional everywhere:
您可以让空间在任何地方都可选:
sed -i.bak 's/\<nchar *( *\([0-9]*\) *)/char(\1)/g' file.sql
-
\<
is used for word boundary. - \ <用于单词边界。< li>
#2
0
Or rather you can open the file in vi and use following in vi cmd mode
或者,您可以在vi中打开文件,并在vi cmd模式下使用。
%s/nchar/char/g
This replaces in entire file at once
这将立即替换整个文件
All occurrences of nchar will be replaced in one go.
所有出现的nchar都会一次替换掉。
And to check before replacing all
并在更换之前进行检查
%s/nchar/char/gc
y for yes
y为是的
n for not replacing
n没有更换
a for replacing rest all
a用来代替休息
#3
0
Using sed
and extended regex -r
(for clarity). Also -i
means "inplace" so no need to create a different output file
使用sed和扩展regex -r(为了清晰)。同样-i表示“inplace”,因此不需要创建不同的输出文件
sed -i sqlfile -r -e "s/nchar\s*\(\s*([0-9]+)\s*\)/char(\1)/g"
#1
2
You can make spaces optional everywhere:
您可以让空间在任何地方都可选:
sed -i.bak 's/\<nchar *( *\([0-9]*\) *)/char(\1)/g' file.sql
-
\<
is used for word boundary. - \ <用于单词边界。< li>
#2
0
Or rather you can open the file in vi and use following in vi cmd mode
或者,您可以在vi中打开文件,并在vi cmd模式下使用。
%s/nchar/char/g
This replaces in entire file at once
这将立即替换整个文件
All occurrences of nchar will be replaced in one go.
所有出现的nchar都会一次替换掉。
And to check before replacing all
并在更换之前进行检查
%s/nchar/char/gc
y for yes
y为是的
n for not replacing
n没有更换
a for replacing rest all
a用来代替休息
#3
0
Using sed
and extended regex -r
(for clarity). Also -i
means "inplace" so no need to create a different output file
使用sed和扩展regex -r(为了清晰)。同样-i表示“inplace”,因此不需要创建不同的输出文件
sed -i sqlfile -r -e "s/nchar\s*\(\s*([0-9]+)\s*\)/char(\1)/g"