如何在 PL/SQL 文件中插入注释行
How to insert a commentary line in a PL/SQL file
这是我的问题,我想在 PL/SQL 代码注释中加入 SVN 的关键字。
问题是在生产中,CREATE OR REPLACE
上方的注释在编译时被删除。
然后,为此我必须在 CREATE OR REPLACE [name] [AS/IS]
行之后添加 注释行 。
我的问题是我完全不知道如何使用 Shell 脚本 进行处理。
我想我会使用 awk,但我根本不是这个工具的高手 :S
例如我有这样一个文件:
-- Comments here will be deleted when compiling
-- That's why i must had my comment after the CREATE OR REPLACE
CREATE OR REPLACE PACKAGE BODY example_file
IS
-------------------------------------------------------------
-- Version Date Person Comments
-------------------------------------------------------------
CODE
.
.
.
我想要这样的文件:
-- Comments here will be deleted when compiling
-- That's why i must had my comment after the CREATE OR REPLACE
CREATE OR REPLACE PACKAGE BODY example_file
IS
-- REVISION $Revision$
-- ID $Id$
-------------------------------------------------------------
-- Version Date Person Comments
-------------------------------------------------------------
CODE
.
.
.
当然我可以在每个文件中一个一个地写。但是有很多文件,这就是为什么我正在寻找一种真正更快地完成此操作的方法。
谢谢,
埃泽基尔。
awk
救援!
$ awk '/CREATE OR REPLACE/{n=NR}
/^IS/ && NR==n+1{print;
print " -- REVISION ...";
print " -- ID ...";
next
} 1' sql
标记第一个模式 "CREATE...",如果下一行以 "IS" 开头,则插入两个注释行。
更新:现在查找 AS 或 IS,不仅在下一行,而且在同一行和后续 3 行之间
$ awk '/CREATE OR REPLACE/{n=NR}
/\yIS|AS\y/ && n<=NR && NR<=n+3{
print;
print " -- REVISION ...\n -- ID ...";
next} 1' sql
当第一个模式匹配时,将变量 n 设置为行号,在 3 行邻域内搜索第二个模式,并在匹配后插入行。
处理AS
IS
排列的gawk
解决方案:
gawk '/^--/ && f < 2 {comment[++c]=[=10=];next}
/CREATE OR REPLACE/{++f;create=[=10=]}
/\y(IS|AS)\y/{
print create
if ([=10=]!=create) {
print [=10=]
}
++f
for (j=1;j<=c;j++){
print comment[j]
}
next
}
f==2 {print} ' source.sql
结果
CREATE OR REPLACE PACKAGE BODY example_file
IS
-- Comments here will be deleted when compiling
-- That's why i must had my comment after the CREATE OR REPLACE
-------------------------------------------------------------
-- Version Date Person Comments
-------------------------------------------------------------
CODE
.
.
.
这是我的问题,我想在 PL/SQL 代码注释中加入 SVN 的关键字。
问题是在生产中,CREATE OR REPLACE
上方的注释在编译时被删除。
然后,为此我必须在 CREATE OR REPLACE [name] [AS/IS]
行之后添加 注释行 。
我的问题是我完全不知道如何使用 Shell 脚本 进行处理。
我想我会使用 awk,但我根本不是这个工具的高手 :S
例如我有这样一个文件:
-- Comments here will be deleted when compiling
-- That's why i must had my comment after the CREATE OR REPLACE
CREATE OR REPLACE PACKAGE BODY example_file
IS
-------------------------------------------------------------
-- Version Date Person Comments
-------------------------------------------------------------
CODE
.
.
.
我想要这样的文件:
-- Comments here will be deleted when compiling
-- That's why i must had my comment after the CREATE OR REPLACE
CREATE OR REPLACE PACKAGE BODY example_file
IS
-- REVISION $Revision$
-- ID $Id$
-------------------------------------------------------------
-- Version Date Person Comments
-------------------------------------------------------------
CODE
.
.
.
当然我可以在每个文件中一个一个地写。但是有很多文件,这就是为什么我正在寻找一种真正更快地完成此操作的方法。
谢谢,
埃泽基尔。
awk
救援!
$ awk '/CREATE OR REPLACE/{n=NR}
/^IS/ && NR==n+1{print;
print " -- REVISION ...";
print " -- ID ...";
next
} 1' sql
标记第一个模式 "CREATE...",如果下一行以 "IS" 开头,则插入两个注释行。
更新:现在查找 AS 或 IS,不仅在下一行,而且在同一行和后续 3 行之间
$ awk '/CREATE OR REPLACE/{n=NR}
/\yIS|AS\y/ && n<=NR && NR<=n+3{
print;
print " -- REVISION ...\n -- ID ...";
next} 1' sql
当第一个模式匹配时,将变量 n 设置为行号,在 3 行邻域内搜索第二个模式,并在匹配后插入行。
处理AS
IS
排列的gawk
解决方案:
gawk '/^--/ && f < 2 {comment[++c]=[=10=];next}
/CREATE OR REPLACE/{++f;create=[=10=]}
/\y(IS|AS)\y/{
print create
if ([=10=]!=create) {
print [=10=]
}
++f
for (j=1;j<=c;j++){
print comment[j]
}
next
}
f==2 {print} ' source.sql
结果
CREATE OR REPLACE PACKAGE BODY example_file
IS
-- Comments here will be deleted when compiling
-- That's why i must had my comment after the CREATE OR REPLACE
-------------------------------------------------------------
-- Version Date Person Comments
-------------------------------------------------------------
CODE
.
.
.